创建数据库、表以及表中字段相关约束

1.crate datebase语句(创建数据库)

实例1:创建一个名词为dome的数据库:Create Database Dome

Create Database Dome;

执行结果如下图所示:
在这里插入图片描述


2.crate table语句(创建数据库中的表)

2.1、创建表

实例:在dome数据库中创建一个customer 表

Create Table Customer
(name varchar(225) not null,
 P_id int not null,
 sex char(3) not null,
 age int not null);

执行结果如下图所示:
在这里插入图片描述


3、约束

用于规定表中的数据规则,如果存在违反规则的数据行为,行为会被约束终止,约束可以在创建表时规定,也可以在创建表后规定

3.1 、not null约束(非空约束)

  1. 在创建表时同时将Name和id列设置not null 约束
Create Table Table_name
(Name varchar(10) not null,
Id char(6) not null);
  1. 对已存在table_name表的id值设置not null约束
Alter Table Table_name Modify id int not null;
  1. 删除table_name表中age字段的not null约束
Alter Table Table_name Modify Age int null;

3.2、primary key约束(主键约束)

  1. 在创建表时添加主键约束primary key
  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约束

  1. 定义多个列的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);
  1. 创建表后,定义多个列的primary key 约束
   alter table table_name
   add constraint pk_ks primary key(id, proj_id);
  1. 撤销primary key约束
    Mysql:
          alter table table_name
          drop primary key;

Oralce/Sql server:

           alter table table_name
           drop constraint pk_ks;

3.3、foreign key约束(外键约束)

对应主键的外键约束,建立的关系型数据库
  1. 在创建表时时定义foreign key约束
    Mysql
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));
  1. 定义多个列的foreign key约束
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);
  1. 创建表后,定义多个foreign key约束
alter table table_name
add constraint fk_kes
foreign key(p_id)
references table_name1(p_id);
  1. 撤销foreign key约束
    Mysql:
alter table table_name
drop foreign key fk_kkk

Oralce/Sql server:

alter table table_name
drop constraint fk_kk ;

3.4、check约束(检查约束)

检查约束,用于限制列中的值的范围
  1. 在创建表时时定义check约束
    Mysql:
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));
  1. 定义多个列的check约束
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);
  1. 创建表后,定义多个check约束
alter table table_name
add constraint chk_dd check(p_id>0 and address='beijing');
  1. 撤销check约束
    Mysql:
alter table table_name
drop check chk_kkk;

SQL server/Oracle/Ms access:

alter table table_name
drop constraint chk_kkk;

3.5、default约束(默认值约束)

用于向表中插入默认值
  1. 在创建表时定义check约束
    Mysql/SQl server /Oracle/Ms access:
create table student
(p_id  int not null,
lastname varchar(225) not null,
City varchar(225) default 'sandnes');

将City的默认值设置为sandnes


  1. 在表已经创建成功后,定义default约束
    Mysql:将
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


  1. 撤销default约束
    Mysql:
alter table test
alter CIty drop default;

SQLserver/Oracle/Ms access:

alter table test
alter column City drop default;
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值