现在的位置: 首页 > 综合 > 正文

MSSQL建库建表键约束

2013年07月31日 ⁄ 综合 ⁄ 共 896字 ⁄ 字号 评论关闭

set nocount on

go

use master

go

if exists(select * from sysdatabases where name = 'SutdentDB')

drop database SutdentDB

go

create database SutdentDB

on

(

name = 'SutdentDB_data.mdf',

fileName = 'C:/SutdentDB_data.mdf',

size = 5mb,

maxSize = 10mb,

fileGrowth = 10%

)log on

(

name = 'SutdentDB_log.ldf',

fileName = 'C:/SutdentDB_log.ldf',

size = 5mb,

maxSize = 10mb,

fileGrowth = 10%

)

go

use SutdentDB

go

create table StuClass

(

Id int identity(1,1) constraint PK_StuClass_Id primary key not null,

Name varchar(6) constraint CK_StuClass_Name check(Name like 'y2t[0-9][0-9][0-9]')

)

go

insert into StuClass(Name)

select 'y2t034'

go

create table StudentInfo

(

Id int identity(1,1) constraint PK_StudentInfo_Id primary key not null,

Name varchar(20) not null,

Age tinyint constraint CK_StudentInfo_Id check(Age < 100 and Age > 0) not null,

StuCls int constraint FK_StuClass_StudentInfo_StuCls foreign key references StuClass not null

)

go

insert into StudentInfo(Name, Age,StuCls)

select '小明',15,1 union

select '小红',15,1 union

select '小张',15,1

go

抱歉!评论已关闭.