数据库的增删改查 insert delete update select

 

 

 

 

添加数据用:关键字insert

Insert into 表名(属性列1,属性列2)values(属性值,属性值)

用已有的数据表建立新的数据表

Create table 表名1 as select 属性列1,属性列2 from 表名

修改数据用:关键字update

Update  表名 set 属性列1=data1,属性列2=data2 [where  condition]

删除数据用:关键字delete

Delete from 表名[where condition ]

查询数据用:select

Select 属性列1  from table 表名 [where  condition]

   Select检索数据:简单检索,多表检索,where条件检索、子查询等等

Select

[distinct |all ]

属性列

From  table 表名

[where  condition]

[group by ]

[having  condition]

[order by ]

1.使用关键字as用来指定别名,select sno as 学号,sname as 姓名 from  student

2.使用表达式操作查询的字段,数据库中的数据不会被修改

  Select sname,2018-sage as 出生年月 from student

Select  sname, sage || '*' || 1.25 || '=' || sage*1.25 as new_age FROM STUDENT;

Select  sname,  sage*3 as new_age  FROM STUDENT;

3.使用函数操作查询的字段

1)对数据库中的字段进行截取操作substr()

  Select sname as 姓名,substr(sno,7,9)  as 截取的学号后三位  from  STUDENT;

4.去除检索数据中的重复数据

 Select distinct sno from student;

5.对检索出来的数据进行排序使用的关键字是order by

 1)按照年龄降序排列,不指定排序方式的话,默认是升序ASC

  Select * from student order by sage desc;

  ###排序时候对NULL值的处理:

  默认情况下,进行排序的时候将NULL值看成最大值。即升序的时候它在最后,降序的时候它在最首位。

指定的话, 用关键词NULLS  first和last

 Select * from student order by sage NULLS FIRST;

 Select * from student order by sage NULLS last;

2)使用别名作为排序字段:

 Select sname 姓名,sage 年龄 from student order by 年龄 NULLS FIRST;

3)使用表达式作为排序字段:

 Select sname 姓名,sage*3  from student order by sage*3 desc NULLS FIRST;

需要注意的是NULL*其他数值还是NULL

4)使用字段的位置作为排序字段:

Select * from student order by 4 NULLS FIRST;

这里字段的位置指的是属性列的位置,4指的是sage属性列的位置

5)使用多个字段混合排序:

多个字段排序的操作过程是:NO1.按照第一个字段排序;NO2.在此基础上,按照第二个字段排序;

Select * from student order by ssex  ASC, sage desc NULLS LAST;

6.使用where子句设置检索条件

 

猜你喜欢

转载自blog.csdn.net/weixin_42329945/article/details/83588776