数据库操作语法

发布 2021-05-11 06:52:28 阅读 2822

语法:insert [into] 《表名》 [列名] values 《列值》

例:insert into strdents (姓名,性别,出生日期) values ('开心朋朋','男','1980/6/15')

注意:into可以省略;列名列值用逗号分开;列值用单引号因上;如果省略表名,将依次插入所有列。

语法:insert into 《已有的新表》 《列名》

select 《原表列名》 from 《原表名》

例:insert into tongxunlu ('姓名','地址','电子邮件')

select name,address,email

from students

注意:into不可省略;查询得到的数据个数、顺序、数据类型等,必须与插入的项保持一致。

语法:select 《新建表列名》 into 《新建表名》 from 《源表名》

例:select name,address,email into tongxunlu from students

注意:新表是在执行查询语句的时候创建的,不能够预先存在。

在新表中插入标识列(关键字‘identity’):

语法:select identity (数据类型,标识种子,标识增长量) as 列名。

into 新表 from 原表名。

例:select identity(int,1,1) as 标识列,dengluid,password into tongxunlu from struents

注意:关键字‘identity’

语法:insert 《表名》 《列名》 select 《列值》 tnion select 《列值》

例:insert students (姓名,性别,出生日期)

select '开心朋朋','男','1980/6/15' union(union表示下一行)

select '蓝色小明','男','19**/

注意:插入的列值必须和插入的列名个数、顺序、数据类型一致。

语法:delete from 《表名》 [where 《删除条件》]

例:delete from a where name='开心朋朋'(删除表a中列值为开心朋朋的行)

注意:删除整行不是删除单个字段,所以在delete后面不能出现字段名。

语法:truncate table 《表名》

例:truncate table tongxunlu

注意:删除表的所有行,但表的结构、列、约束、索引等不会被删除;不能用语有外建约束引用的表,并且不可恢复。

语法:update 《表名》 set 《列名=更新值》 [where 《更新条件》]

例:update tongxunlu set 年龄=18 where 姓名='蓝色小名'

注意:set后面可以紧随多个数据列的更新值;where子句是可选的,用来限制条件,如果不选则整个表的所有行都被更新。

语法:select 《列名》 from 《表名》 [where 《查询条件表达试》] order by 《排序的列名》[asc或desc]]

例:select * from a

说明:查询a表中所有行和列。

例:select i,j,k from a where f=5

说明:查询表a中f=5的所有行,并显示i,j,k3列。

例:select name as 姓名 from a where xingbie='男'

说明:查询a表中性别为男的所有行,显示name列,并将name列改名为(姓名)显示。

例:select name from a where email is null

说明:查询表a中email为空的所有行,并显示name列;sql语句中用is null或者is not null来判断是否为空行。

例:select name '唐山' as 地址 from a

说明:查询表a,显示name列,并添加地址列,其列值都为'唐山'

例1:select top 6 name from a

说明:查询表a,显示列name的前6行,top为关键字。

例2:select top 60 percent name from a

说明:查询表a,显示列name的60%,percent为关键字。

例:select name

from a

where chengji>=60

order by desc

说明:查询表中chengji大于等于60的所有行,并按降序显示name列;默认为asc升序。

注意:like运算副只用语字符串,所以仅与char和varchar数据类型联合使用。

例:select * from a where name like '赵%'

说明:查询显示表a中,name字段第一个字为赵的记录。

例:select * from a where nianling between 18 and 20

说明:查询显示表a中nianling在18到20之间的记录。

例:select name from a where address in ('北京','上海','唐山')

说明:查询表a中address值为北京或者上海或者唐山的记录,显示name字段。

例:select studentid as 学员编号**g(score) as 平均成绩 (注释:这里的score是列名)

from score (注释:这里的score是表名)

group by studentid

说明:在表score中查询,按strdentid字段分组,显示strdentid字段和score字段的平均值;select语句中只允许被分组的列和为每个分组返回的一个值的表达试,例如用一个列名作为参数的聚合函数。

例:select studentid as 学员编号**g(score) as 平均成绩 (注释:这里的score是列名)

from score (注释:这里的score是表名)

group by studentid

h**ing count(score)>1

说明:接上面例子,显示分组后count(score)>1的行,由于where只能在没有分组时使用,分组后只能使用h**ing来限制条件,在where子句中指定联接条件。

例:select

from a,b

where

说明:查询表a和表b中name字段相等的记录,并显示表a中的name字段和表b中的chengji字段。

在from子句中使用join…on

例:select

from a inner join b

on (说明:同上。

左外联接查询。

例:select

from strdents as s

left outer join score as c

on 说明:在strdents表和score表中查询满足on条件的行,条件为score表的strdentid与strdents表中的sconde相同。

例:select

from strdents as s

right outer join score as c

on 说明:在strdents表和score表中查询满足on条件的行,条件为strdents表中的sconde与score表的strdentid相同。

使用关键字 distinct

例:select distinct name from a;

说明:查询表a中 name字段的记录,并不显示表a中的重复的name字段。

使用关键字 distinct

例:select * from 表名 where rowed in (select min(rowid) from 表名 group by 列名);

说明:查询表a中多个字段的记录,并且不显示子查询中分组的字段。其中rowid为oracle中的虚拟column用来指向表中一个row的存放地址,改为其它的不能运行。

其中min方法若改成max时结果会有所不同(说明暂定)

使用函数 count(列名)

例:select name,count(name) from 表名 group by name;

说明:group by 那个字段只能查找哪个字段和该字段的数量。若要同事搜索其它字段就报错。

原因可能是:搜索的结果是一组组的数据。在每一组数据中,其它的列有很多,所以不能搜索出唯一的一个值。

表a字段。int tid = 0;//加班**。

float days = 0;//工日数。

表b字段。int tid = 0;//加班人员**。

int overtimeid = 0;//加班**。

int userid=0;//用户id

数据库的语法

添加列 alter table table name add column name column properties 查询表结构 desc table name 新建 窗口 命令窗口。删除列 alter table table name drop column column name 修改列属性...

数据库 altertable语法

一 修改表 增加,删除,修改类型 1 如需在表中添加列,请使用下列语法 alter table add datatype 2 要删除表中的列,请使用下列语法 alter table drop column 某些数据库系统不允许这种在数据库表中删除列的方式 drop column column nam...

数据库的高级语法

变量定义和赋值。全局变量 只读,由系统维护,作用域是单个连接。常见全局变量。error 返回执行的上一个语句的错误号,如果出错,错误号是大于0的整数,不出错则为0 print error select fro stuinfo print error select from stuinfo print...