sql server 外键-唯一值…

外键--foreign key reference(外键关联)

唯一值--unique
设置多个唯一值--constraint 名字 unique (属性:例如id,属性,属性,属性)

默认值--default

主键--primary key

限制--check 

非空--not null

下面具体应用-------------------------------------------
作业要求:创新班网站信息发布模块表格创建
实现功能:发布信息!功能ER图如下:
sql <wbr>server <wbr>外键-唯一值-默认值-主键-限制-非空

要求创建如下1-3个表格,具体要求见下表:
表1:类型表
字段名称字段描述数据类型长度其他要求
自己命名类型编号int主键
类型名称varchar(8)非空
首页是否显示char(2)非空,默认“是”,只能‘是’或‘否’
表2:信息表
字段名称字段描述数据类型长度其他要求
自己命名信息编号int主键
所属类型int外键
信息标题varchar(50)非空
信息类容text非空
发布人学号int外键
发布日期date默认值:自动填写当前日期
点击次数int默认值:0
表3:人员信息表
字段名称字段描述数据类型长度其他要求
自己命名学号int主键
姓名varchar(12)非空
性别char(2)非空默认“男”,只有“男”“女”两个值
出生年月date
家庭住址varchar(100)
政治面貌varchar(20)
班级职位varchar(20)
民族varchar(20)默认值:汉族
籍贯varchar(20)
联系电话varchar(11)
QQvarchar(16)
家庭电话varchar(11)

代码示例---------------------------------------------
create database books
use books
drop table bookType
create table bookType(
tId int not null primary key,
tName varchar(20),
tNumber int,
constraint bt_name unique (tid,tname)
)

drop database test
create database test
use test
create table Type(
id int primary key,
name varchar(8) not null,
show char(2) default '是' check (show = '是' or show = '否')
)

create table PersonInformation(
id int primary key,
name varchar(12) not null,
sex char(2) default '男' check (sex = '男' or sex = '女'),
birthday date,
homeAddress varchar(100),
political varchar(20),
position   varchar(20),
nation varchar(20)default '汉族',
origin varchar(20),
phone varchar(11),
qq varchar(16),
homephone varchar(11)
)
create table InformationTable(
id int primary key,
typ int foreign key references type,
title varchar(50) not null,
content text not null,
stuId int foreign key references PersonInformation,
dat date default 'getdate()',
times int default '0'
)

单独代码示例-------------------与上面的 作业要求无关,注意红色字体

create table bookType(
tId int not null primary key,
tName varchar(20),
tNumber int,
constraint bt_name unique (tid,tname)
)

create table books(
bId int not null primary key ,
bName varchar(200)not null,
bWriter varchar(50),
bPublisher varchar(50),
bPublishdate date,
bPrice float,
bDbdate date,
bNumber int,
tId int foreign key references bookType------外键关联
)
create table s2(
sno char(2) primary key,
sn char(8) not null,
age numeric(2) not null,constraint age_chk check(age between 15 and 45),
sex char(2) constraint sex_dft default'男' constraint sex_chk check (sex = '男' or sex = '女'),--限制
dept char(2) not null
)
create table dbUsers(
uId int not null primary key,
uName varchar(12),
uAge numeric(2) not null constraint ck_age check (uAge between 15 and 45),
uSex char(2) not null default '男' constraint ck_sex check (uSex = '男' or uSex = '女'),--限制
uDept varchar(10) not null
)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值