创建数据表-2

--奇幻数据库
use DBTEST
--创建表基本语法

--create table 表明
--(

-- 字段名1 数据类型,
-- 字段名2 数据类型

--)

--判断表是否存在
--U 用户自定义的表
if exists(select* from sys.objects where name = 'Department' and type = 'U')
drop table Department
--建表(部门 职级 员工)
create table Department
(

--部门编号 primary key主键 identity(1,1)自动增长 初始值1 增长步长1
DepartmentId int primary key identity(1,1),
--部门名称
DepartmentName nvarchar(50) not null,
--部门描述
DepartmentRemark text

)
--char定长 char(10)无论存储数据是否真的到了10个字节 都要占用10个字节
--char(10)存储ab 仍然占用10个字节
--varchar 变长 varchar(10)最多占用10个字节
--varchar(10)存储'ab',占用2个字节
--text:长文本

--char varchar text前面加n 存储unicode字符 对中文友好
--varchar(100) 存储100个字母或者50个汉字
--nvarchar(100)存储100个字母或者100个汉字

create table Rank
(

--部门编号 primary key主键 identity(1,1)自动增长 初始值1 增长步长1
RankId int primary key identity(1,1),
--部门名称
RankName nvarchar(50) not null,
--部门描述
RankRemark text

)

--员工
create table People
(

PeopleId int primary key identity(1,1),--员工编号
DepartmentId int references Department(DepartmentId) not null, --部门(引用外键)
RankId int references Rank not null, --职级(引用外键)
PeopleName nvarchar(50) not null,
PeopleSex nvarchar(1) default('男')check(PeopleSex='男' or PeopleSex='女') not null,
PeopleBirth smalldatetime not null,
PeoPleSalary decimal(12,2) check(PeopleSalary>=1000 and PeopleSalary<=1000000) not null,
PeoplePhone varchar(20) unique not null, --电话
PeopleAddress varchar(300),
PeopleAddTime smalldatetime default(getdate()) --添加时间

)

posted @ 2026-05-14 00:43  菜鸟的奋斗军  阅读(9)  评论(0)    收藏  举报