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
用途:
在数据库中移除一个数据表定义及该数据表中的所有资料、索引、触发程序、条件约束及权限指定。
语法:
Create Database
用途:
建立新的数据库.
语法:
CREATE DATABASE database_name |
Drop Database
用途:
移除原有的数据库
语法:
DROP DATABASE database_name |
|