use master;
go
if DB_ID('test') is not null
drop database test;
go
create database test;
go
use test;
CREATE TABLE MovieExec (
name CHAR(30),
address VARCHAR(255),
cert# INT PRIMARY KEY,--主键
netWorth INT
);
create table Studio(
name char(30) primary key ,
address char(255),
presC# int,
foreign key(presC#) references MovieExec(cert#)
ON delete set null --删除时设置空
on update cascade --更新时置空
--deferrble initially deferred
);
CREATE TABLE MovieStar (
name CHAR(30),
address VARCHAR(255),
gender CHAR(1) constraint NoAndro check(gender in('F','M')),
birthdate CHAR(10),
constraint NameIsKey PRIMARY KEY (name) --constraint设置约束名
);
alter table MovieStar drop constraint NoAndro; --删除约束
alter table MovieStar drop constraint NameIsKey;
alter table MovieStar add constraint NameIsKey primary Key(name); --加入约束
exec sp_help 'MovieStar'; --查看表中的约束数据库约束(1)学习
最新推荐文章于 2024-03-20 10:56:19 发布
本文介绍了使用SQL语句创建并修改数据库表的过程,包括创建MovieExec、Studio和MovieStar表,定义主键、外键及各种约束条件,并展示了如何通过ALTER TABLE语句来调整表结构。

5475

被折叠的 条评论
为什么被折叠?



