主键和外键举例_主键,外键等约束详解实例

1

--

创建表

create

table

tb_Dept

(

Deptid

char

(

2

)

Primary

key

,

DeptName

char

(

16

)

Not

Null

)

2

--

外键约束

create

table

tb_Student

(

Studid

char

(

10

)

Primary

key

,

Studname

char

(

8

)

Not

null,

Deptid

char

(

2

)

Not

null,

Constraint

FK_DeptID

Foreign

Key

(

Deptid

)

References

Tb_Dept

(

DeptID

)

)

3

--

外键约束简化形式,必须要求

tb_Dept

表中

DeptID

为主键,且数值类型相同

create

table

Tb_Student

(

StudId

char

(

10

)

Primary

key

,

StudName

char

(

8

)

Not

null,

DeptID

char

(

2

)

not

null

References

Tb_Dept

)

4

--

创建表,无主键

create

table

Tb_Class

(

ClassID

char

(

8

)

not

null,

ClassName

varchar

(

30

)

not

null,

DeptId

char

(

2

)

not

null,

ClassStuNumber

int

)

5

--

创建表,同时定义主键

create

table

Tb_Class

(

classid

char

(

8

)

not

null,

ClassName

varchar

(

30

)

not

null,

DeptID

char

(

2

)

not

null,

ClassStuNumber

int

constraint

PK_ClassID

Primary

key

(

ClassID

,

ClassName

)

)

6

--

新增主键

Alter

table

Tb_class

ADD

Constraint

PK_ClassID

primary

key

(

Classid

)

7

--

删除主键

Alter

table

tb_Class

Delete

Constraint

PK_ClassID

Primary

key

(

ClassID

)

8

--

外键级联更新,删除,简化形式

Create

table

tb_student

(

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值