SQL核心語句,非常實用的小技巧

2021-10-07 17:33:14 字數 1716 閱讀 2158

向表中新增乙個新記錄。

例 :

insert student(people1)

values

('some data'

)

以上語句是把字串』some data』插入表student的people1欄位中。將要被插入資料的字段的名字在第乙個括號中指定,實際的資料在第二個括號中給出

insert 語句的完整句法如下:

insert

[into

] [

(column_list)

]

如果乙個表有多個字段,通過逗號把欄位名和字段值隔開,我們可以向所有的字段中插入資料。

如下:假設表student有三個欄位people1,people2,和people3。

insert student(people1,people2,people3)

values

('some data'

,'some more data'

,'yet more data'

)

注意:

當我們向乙個表中插入一條新記錄,但有乙個字段沒有提供資料。在這種情況下,有下面的四種可能:

the people in

table student may not be null

.

要從表中刪除乙個或多個記錄。

可以給delete 語句提供where 子句,where子句用來選擇要刪除的記錄:
delete

[from

] [

where clause]

下面的這個delete語句只刪除那些people1欄位的值為』li』或people2欄位的值為』zhang』的記錄:

delete mytable where people1=

'li'

or people2=

'zhang'

要修改表中已經存在的一條或多條記錄。

同delete語句一樣,update語句可以使用where子句來選擇更新特定的記錄:
update student set people1=

'updated!'

where people2 =

'update me!'

看到這裡, 也許你已經注意到,insert語句與delete語句和update語句有一點不同,它一次只操作乙個記錄。然而,有乙個方法可以使insert語句一次新增多個記錄。要作到這一點,你需要把insert 語句與select 語句結合起來。

如下:

insert student(people1,people2)

select another_people1,another_people2

from anotherstudent

where another_people1=

'copy me!'

拷貝整個表:

select

*into newtable from student

SQL核心語句 非常實用的幾個技巧

sql核心語句 非常實用的幾個技巧 插入資料 向表中新增乙個新記錄,你要使用sql insert 語句。這裡有乙個如何使用這種語句的例子 insert mytable mycolumn values some data 這個語句把字串 some data 插入表mytable的mycolumn欄位中...

SQL核心語句 非常實用的幾個技巧

articlecontent1 lblcontent 插入資料 向表中新增乙個新記錄,你要使用sql insert 語句。這裡有乙個如何使用這種語句的例子 insert mytable mycolumn values some data 這個語句把字串 some data 插入表mytable的my...

SQL核心語句 非常實用的幾個技巧

插入資料 向表中新增乙個新記錄,你要使用sql insert 語句。這裡有乙個如何使用這種語句的例子 insert mytable mycolumn values some data 這個語句把字串 some data 插入表mytable的mycolumn欄位中。將要被插入資料的字段的名字在第乙個...