1. 程式人生 > >資料庫的增刪改查 insert delete update select

資料庫的增刪改查 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子句設定檢索條件