实例1:创建一个名词为dome的数据库:Create Database Dome
Create Database Dome;
实例:在dome数据库中创建一个customer 表
Create Table Customer
(name varchar(225) not null,
P_id int not null,
sex char(3) not null,
age int not null);
用于规定表中的数据规则,如果存在违反规则的数据行为,行为会被约束终止,约束可以在创建表时规定,也可以在创建表后规定
Create Table Table_name
(Name varchar(10) not null,
Id char(6) not null);
Alter Table Table_name Modify id int not null;
Alter Table Table_name Modify Age int null;
Create Table Table_name
(Name varcher(10),
Age int not null,
Sex char(2) not null,
Id char(9) primary key);
以上语句,创建table_nane表,并且将id添加primary key约束
Create Table Table_name
(Name varchar(10) not null,
Id char(9) not null,
Proj_id varcher(11) not null,
Age int,
Constraint Pk_tablename Primary key(Id,Proj_id));
备注:以上语句中的Pk_tablename主键包含Id和Proj_id两个字段的值
3. 在创建表之后定义primary key 约束
alter table table_name add primary key(id);
alter table table_name
add constraint pk_ks primary key(id, proj_id);
alter table table_name
drop primary key;
Oralce/Sql server:
alter table table_name
drop constraint pk_ks;
对应主键的外键约束,建立的关系型数据库
create table table_name
(id int not null,
orderNo int not null,
p_id int,
primary key(id),
foreign key(p_id) references table_name1(p_id));
SQL server/Oracle/MS access:
create table table_name
(id int not null primary key,
OrderNo int null,
P_Id int foreign key references table_name1(P_Id));
create table table_name
(O_id int not null,
OrderNo int not null,
P_Id int,
primary key (O_id),
constraint fk_ddd foreign key(P_Id)
references table_name1(P_Id));
备注:以上语句中的fk_ddd外键包含P_id和O_id两个字段的值
3. 在创建表之后定义foreign key 约束
alter table table_name
add foreign key(P_id)
references table_name1(P_id);
alter table table_name
add constraint fk_kes
foreign key(p_id)
references table_name1(p_id);
alter table table_name
drop foreign key fk_kkk
Oralce/Sql server:
alter table table_name
drop constraint fk_kk ;
检查约束,用于限制列中的值的范围
create table table_name
(id int not null,
lastname varchar(225) not null,
check(id>0));
SQL server/Oracle/MS access:
create table table_name
(id int not null check(id>0),
lastname varcher(225) not null,
address varchar(225));
create table table_name
(id int not null,
lastname varchar(255) not null,
address varchar(225) not null,
city varchar(255),
constraint chk_kk check(id<0 and address='beijing')))
备注:以上语句中的chk_kk包含id<0 and address='beijing’的约束条件
3. 在创建表之后定义check 约束
alter table table_name
add check(p_id>0);
alter table table_name
add constraint chk_dd check(p_id>0 and address='beijing');
alter table table_name
drop check chk_kkk;
SQL server/Oracle/Ms access:
alter table table_name
drop constraint chk_kkk;
用于向表中插入默认值
create table student
(p_id int not null,
lastname varchar(225) not null,
City varchar(225) default 'sandnes');
将City的默认值设置为sandnes
alter table test
alter CIty set default'kkk';
将CIty的默认值设置为kkk
SQL server/MS access:
alter table test
add constraint ab_c default 'sandnes' for City
将City字段的默认值设置为sandnes
Oracle:
alter table test
modify City default'kkk';
将City字段的默认值设置为kkk
alter table test
alter CIty drop default;
SQLserver/Oracle/Ms access:
alter table test
alter column City drop default;