SQL语法手册(三)

SQL语法手册(三)

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

运行下面的SQL将Person表中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

Delete

用途:

删除表中的数据

语法:

DELETE FROM table_name WHERE column_name = some_value

例:

“Person”表中的原始数据:

LastName

FirstName

Address

City

Nilsen

Fred

Kirkegt 56

Stavanger

Rasmussen

Nina

Stien 12

Stavanger

删除Person表中LastName为”Rasmussen”的数据:

DELETE FROM Person WHERE LastName = 'Rasmussen'

执行删除语句后”Person”表中的数据为

LastName

FirstName

Address

City

Nilsen

Fred

Kirkegt 56

Stavanger

Create Table

用途:

建立新的资料表。

语法:

CREATE TABLE table_name

(

column_name1 data_type,

column_name2 data_type,

.......

)

例:

创建一张叫“Person”的表,该表有4个字段"LastName", "FirstName", "Address", "Age":

CREATE TABLE Person

(

LastName varchar,

FirstName varchar,

Address varchar,

Age int

)

如果想指定字段的最大存储长度,你可以这样:

CREATE TABLE Person

(

LastName varchar(30),

FirstName varchar(30),

Address varchar(120),

Age int(3)

)

下表中列出了在SQL的一些数据类型:

Data Type

Description

integer(size)

int(size)

smallint(size)

tinyint(size)

Hold integers only. The maximum number of digits are specified in parenthesis.

decimal(size,d)

numeric(size,d)

Hold numbers with fractions. The maximum number of digits are specified in "size". The maximum number of digits to the right of the decimal is specified in "d".

char(size)

Holds a fixed length string (can contain letters, numbers, and special characters). The fixed size is specified in parenthesis.

varchar(size)

Holds a variable length string (can contain letters, numbers, and special characters). The maximum size is specified in parenthesis.

date(yyyymmdd)

Holds a date

Alter Table

用途:

在已经存在的表中增加后者移除字段

语法:

ALTER TABLE table_name

ADD column_name datatype

ALTER TABLE table_name

DROP COLUMN column_name

注意:某些数据库管理系统不允许移除表中的字段

例:

“Person”表中的原始数据:

LastName

FirstName

Address

Pettersen

Kari

Storgt 20

在Person表中增加一个名为City的字段:

ALTER TABLE Person ADD City varchar(30)

增加后表中数据如下:

LastName

FirstName

Address

City

Pettersen

Kari

Storgt 20

移除Person表中原有的Address字段

ALTER TABLE Person DROP COLUMN Address

移除后表中数据如下:

LastName

FirstName

City

Pettersen

Kari

Drop Table

用途:

在数据库中移除一个数据表定义及该数据表中的所有资料、索引、触发程序、条件约束及权限指定。

语法:

DROP TABLE table_name

Create Database

用途:

建立新的数据库.

语法:

CREATE DATABASE database_name

Drop Database

用途:

移除原有的数据库

语法:

DROP DATABASE database_name