Default 用法

-- 约束(constraints)用于限制加入表的数据的类型
----- 约束可以在建表的时候加,也可以在建表后加上。
---- 主要用 NOT NULL,UNIQUE,primary key ,foreign key , check ,default


--- 1. not null


---- 注意:强制列不接受空值,添加和更新都不能为空。

 

 

--- 2. unique

 

---- 注意:
-------------  UNIQUE 约束唯一标识数据库表中的每条记录。
-------------  UNIQUE 和 PRIMARY KEY 约束均为列或列集合提供了唯一性的保证。
-------------  PRIMARY KEY 拥有自动定义的 UNIQUE 约束。
-------------  每个表可以有多个 UNIQUE 约束,但是每个表只能有一个 PRIMARY KEY 约束。
-- SQL Server / Oracle / MS Access:

create table test(
  id int not null unique,
  name varchar(20),
);

create table test(
  id int not null,
  name varchar(20),
  unique(id)
);
--如果需要命名 UNIQUE 约束,以及为多个列定义 UNIQUE 约束,请使用下面的 SQL 语法:
---- MySQL / SQL Server / Oracle / MS Access:

create table testuuuy(
  id int not null,
  name varchar(20),
  constraint uc_TestID UNIQUE (id,name)
);

select * from testuuuy;
--表已经创建,如需添加约束,可以使用下面SQL语法
---MySQL / SQL Server / Oracle / MS Access:

alter table testuuuy add unique(id);


--如需命名 UNIQUE 约束,并定义多个列的 UNIQUE 约束,请使用下面的 SQL 语法:

----- MySQL / SQL Server / Oracle / MS Access:
alter table testuuuy
add constraint uc_PersonID UNIQUE (Id_P,LastName)

----  如需撤销 UNIQUE 约束,请使用下面的 SQL:

----   MySQL:
alter table Persons
DROP INDEX uc_PersonID
----   SQL Server / Oracle / MS Access:
ALTER TABLE Persons
DROP CONSTRAINT uc_PersonID

 

-- 3. primary key

 

----   SQL PRIMARY KEY 约束唯一标识数据库表中的每条记录。
----  主键必须包含唯一的值。
----  主键列不能包含 NULL 值。
----  每个表都应该有一个主键,并且每个表只能有一个主键。


------ 单个列定义约束
----------- SQL Server / Oracle / MS Access:
create table testTab(
id int not null primary key,
name varchar(30)
);

------ 多个列定义约束
----------- MySQL / SQL Server / Oracle / MS Access:

create table testTab(
 id int not null,
 name varchar(30) not null,
 constraint pk_testTabID primary key (id,name)
);

--- 表已经存在的话,创建约束

alter table testTab add primary key (id);

--- 表已经存在的话,创建多个约束

alter table testTab add constraint pk_testTab primary key (id,name)

--- 删除约束

alter table testTab drop constraint pk_testTab;

 


-- 4. foreign key


-------- 一个表中的foreign key 指向另的primary key

--- 单个列定义外键
-------  SQL Server / Oracle / MS Access:

create table test_tab(
  id int not null,
  userid char(32) not null,
  constraint FK_test_tab foreign key (userid) references base_dept(id)
);

--- 表已经存在,要建外键可以使用以下语法

alter table test_tab add foreign key(userid) references base_dept(id);

--- 表已经存在,如果需要命名外键名或者给多个列添加外键的话,使用以下语法

alter table test_tab add constraint FK_test_tab foreign key(userid) references base_dept(id);

--- 撤销foregin key 约束

alter table test_tab drop constraint FK_test_tab

 

--- 5. check


---- check约束用于限制列中的值的范围。
----- 对表中的单个列进行check约束,那么这个列只允许添加特定的值。
----- 对整个表进行check约束,那么此约束会在特定的列中对值进行限制.

---- 创建表的时候,添加check约束

create table test_tab(
 id int not null check(id>4),
 name varchar(35)
);

--- 如果需要命名check约束,以及为多个列定义check约束,使用下面语法。
create table test_tabdf(
 id int not null,
 name varchar(35)
);


---- 在表已经创建的情况下,想添加check约束,使用下面语法

alter table test_tab add check(id>4);


---- 在表已经创建的情况下,如果需要命名check约束,以及为多个列定义check约束,使用下面语法。

alter table test_tab add constraint check_test check(id>4 and name='我');

---  撤销check约束

alter table test_tab drop constraint check_test;

 

---- 6.default


----- 用于向列中插入默认值
----- 如果没有规定其他的值,会将默认值添加到所有的新记录。


--- 创建表的时候,添加default约束。

create table zhong(
 id int not null,
 username varchar(35) default(9)
);


--- 在表已经创建的情况下,为某个列设置default约束,或者修改default值。使用下面语法

-------    在oracle中要使用

alter table zhong modify username default '张三是是是';


--- 撤销约束
---在oracle中要使用
alter table zhong modify username default null;

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值