SQL Server基础SQL脚本之主外键约束

概要


本系列,几乎都是代码,记得当时写的时候用的是微软的官方实例数据库AdventureWorks_Data.mdf、AdventureWorks_Log.ldf来运行的。

下载链接:链接: https://pan.baidu.com/s/1pMdLz6N 密码: xvhu

或者回复“AdventureWorks”来获取链接。

代码我就放在文中了。

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

create table a1(
    aid int constraint PK_a primary key,
    age int check(age between 27 and 80),
    sex int check(sex in('M','F')),
    tel nvarchar(20) check(tel like'1[3,5,7,8][0-9][0-9][0-9][0-9][0-9][0-9][0-9]
        [0-9][0-9]')

)
create table b2(
    f nvarchar(10)
)

create rule R as @f  like 'Y%'
sp_bindrule 'R','b2.f'
insert into b2 values('you')
select * from b2
sp_unbindrule 'b2.f'
drop rule R

create type HAHA from int not null
create table F(hj    HAHA )
insert into F values(23)

create synonym HIHI for AdventureWorks.HumanResources.Department
    select * from HIHI

create partition function GG(int) as range left for values(30,70,90)
    create partition function JK(date) as range right for values('2001-09-09',
    '2003-06-09')

    ---------------------------------
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 HEIHEI add filegroup FG_New
alter database HEIHEI add file(name='S_DB_N_7',filename='D:\S_DB_N_7.ndf')to 
filegroup FG_New
alter partition scheme SC next used FG_New
alter partition function GG() split range(90)

原文发布于微信公众号 - 数据库SQL(SQLdba)

原文发表时间:2018-02-03

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

扫码关注云+社区