在SQLserver数据库中创建不同类型的表

--use 数据库名称 选择数据库
use mydb
/*
	create table 表名
	(
		列名1 数据类型 [约束],
		列名2 数据类型 [约束]
	)
*/

create table userinfo
(
	userId int  primary key identity(1,1),--primary key:主键  identity(标示种子,标示增量)
	userName varchar(20) not null,--not null:非空约束
	userPass varchar(20) check (len(userPass)>6 and len(userPass)<16) --len(列名):该列的值的长度
)

use mydb
create table dept2
(
	did int primary key identity(1,1),
	dname varchar(20) not null,
	dnum int check (dnum>0)
)
create table emp2
(
	eid int primary key identity(1,1),
	ename varchar(20) not null,
	eage  smallint check (eage>0 and eage<100),
	eaddress varchar(20) default '周口', -- 默认值
	did int  foreign key references dept2(did) --外键 : foreign key  references 主表(主键)
)

猜你喜欢

转载自blog.csdn.net/weixin_48135624/article/details/115212152