您要打印的文件是:SQL的详细语法介绍——对于学习数据库最基础知识六

SQL的详细语法介绍——对于学习数据库最基础知识六

作者:CLNARA    转贴自:CSDN    点击数:4297


Alias

用途:

可用在表、结果集或者列上,为它们取一个逻辑名称

语法:

给列取别名:

SELECT column AS column_alias FROM table

       给表取别名:

SELECT column FROM table AS table_alias

例:

“Persons”表中的原始数据:

LastName

FirstName

Address

City

Hansen

Ola

Timoteivn 10

Sandnes

Svendson

Tove

Borgvn 23

Sandnes

Pettersen

Kari

Storgt 20

Stavanger

运行下面的SQL

SELECT LastName AS Family, FirstName AS Name
FROM Persons

返回结果

Family

Name

Hansen

Ola

Svendson

Tove

Pettersen

Kari

运行下面的SQL

SELECT LastName, FirstName
FROM Persons AS Employees

返回结果

Employees中的数据有:

LastName

FirstName

Hansen

Ola

Svendson

Tove

Pettersen

Kari

 

 

Insert Into

用途:

       在表中插入新行

语法:

       插入一行数据

INSERT INTO table_name
VALUES (value1, value2,....)

       插入一行数据在指定的字段上

INSERT INTO table_name (column1, column2,...)
VALUES (value1, value2,....)   

例:

“Persons”表中的原始数据:

LastName

FirstName

Address

City

Pettersen

Kari

Storgt 20

Stavanger

运行下面的SQL插入一行数据:

INSERT INTO Persons 
VALUES ('Hetland', 'Camilla', 'Hagabakka 24', 'Sandnes')

插入后”Persons”表中的数据为

LastName

FirstName

Address

City

Pettersen

Kari

Storgt 20

Stavanger

Hetland

Camilla

Hagabakka 24

Sandnes

运行下面的SQL插入一行数据在指定的字段上:

INSERT INTO Persons (LastName, Address)
VALUES ('Rasmussen', 'Storgt 67')

插入后”Persons”表中的数据为

LastName

FirstName

Address

City

Pettersen

Kari

Storgt 20

Stavanger

Hetland

Camilla

Hagabakka 24

Sandnes

Rasmussen

 

Storgt 67

 

 

 

Update

用途:

       更新表中原有数据

语法:

UPDATE table_name SET column_name = new_value
WHERE column_name = some_value

例:

“Person”表中的原始数据:

LastName

FirstName

Address

City

Nilsen

Fred

Kirkegt 56

Stavanger

Rasmussen

 

Storgt 67

 

运行下面的SQLPerson表中LastName字段为”Rasmussen”FirstName更新为”Nina”

UPDATE Person SET FirstName = 'Nina'
WHERE LastName = 'Rasmussen'

更新后”Person”表中的数据为

LastName

FirstName

Address

City

Nilsen

Fred

Kirkegt 56

Stavanger

Rasmussen

Nina

Storgt 67

 

同样的,用UPDATE语句也可以同时更新多个字段:

UPDATE Person
SET Address = 'Stien 12', City = 'Stavanger'
WHERE LastName = 'Rasmussen'

更新后”Person”表中的数据为

LastName

FirstName

Address

City

Nilsen

Fred

Kirkegt 56

Stavanger

Rasmussen

Nina

Stien 12

Stavanger