SQL中主键的约束
if exists(select * from sys.databases where name='SchDB')
drop database SchDB
create database SchDB on primary(
name='S_DB',filename='D:\S_DB.mdf',size=5mb,maxsize=100mb,filegrowth=10%
),( name='S_DB_N_1',filename='D:\S_DB_N_1.ndf',size=1mb,maxsize=50mb,filegrowth=1mb)
,( name='S_DB_N_2',filename='D:\S_DB_N_2.ndf',size=1mb,maxsize=50mb,filegrowth=1mb)
,filegroup XXX( name='S_DB_N_3',filename='D:\S_DB_N_3.ndf',size=1mb,maxsize=50mb,filegrowth=1mb)
,( name='S_DB_N_4',filename='D:\S_DB_N_4.ndf',size=1mb,maxsize=50mb,filegrowth=1mb)
,( name='S_DB_N_5',filename='D:\S_DB_N_5.ndf',size=1mb,maxsize=50mb,filegrowth=1mb)
go
alter database SchDB add filegroup YYY
go
alter database SchDb add file(
name='S_DB_N_6',filename='D:\S_DB_N_6.ndf',
size=1mb,maxsize=50mb,filegrowth=1mb
)to filegroup YYY
alter database SchDB modify filegroup YYY default
use SchDB
create table U3(
id int constraint PK_HAHA primary key,
age int constraint CK_HAHA check(age in(20,70,80)) default 80
)
insert into U3 output inserted.* values(default)
insert into U3 output inserted.* values(30)
sp_renameDB 'SchDB','HEIHEI'
create schema XIXI
create table XIXI.U(
id int foreign key(id) references U3(id)
)
create table Y(
id int ,
s_id int
constraint PK_id_s_id primary key(id,S_id)
)
--创建唯一约束
create table K1(name nvarchar(20) unique)
create table K3(
id int not null,--如果是主键必须添加not null
name nvarchar(20) constraint UQ_Name2 unique
)
--修改表创建约束
alter table K3 add constraint PK_id primary key(id)
create table a(--主表
aid int primary key
)
create table b(--从表
bid int foreign key(bid) references a(aid)
)
drop table b
drop table a