T-SQL数据库规则约束及默认值设置学习笔记

时间:2022-03-02 09:51:59
create database test1
go
use test1
create table table1
(
   book_ID int primary key not null,
   book_name varchar(50) not null,
   book_price varchar(100) not null,
   book_type varchar(50) not null,
   master_phone varchar(100) not null
)
go
create default master_phone as '(000)00000000'
go
exec sp_bindefault master_phone,'table1.master_phone'
insert into table1 values('1','仙剑奇侠传','50','科幻小说','(028)81776327')
insert into table1 (book_ID,book_name,book_price,book_type) values('2','网络经济学','80','网络教材')
select * from table1
go
--默认值设置成功!
create rule book_type_rule as @book_type in ('科幻小说','网络教材','文学读本','恐怖小说','言情小说')
go
exec sp_bindefault book_type_rule,'table1.book_type'
go
--创建规则约束成功!
--打完收功....
drop table table1
drop database test1