数据库

发布时间 2023-03-24 17:43:43作者: 春哥博客
字符串类型的区别:
 
--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个汉字。

 

1、创建数据库:

create database DBTEST

2、创建数据表:

--切换数据库
use DBTEST

--建表(部门、职级、员工)
create table Department --部门表
(
    --部门编号
    --primary key:设置主键
    -- identity(1,1):自动增长,初始值1,增长步长1
    DeparementId int primary key identity(1,1),
    --部门名称
    DeparementName nvarchar(50) not null,
    --部门描述
    DeparementRemark text
)
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),--员工编号
    DeparementId int references Department(DeparementId) not null,--引用部门表的主键作为我的外键
    RankId int references [Rank](RankId) not null,--引用职级表的主键作为我的外键
    PeopleName nvarchar(50) not null,--姓名
    PeopleSex nvarchar(1) default('') check(PeopleSex='' or PeopleSex=''),--性别
    PeopleBirth smalldatetime not null,--生日
    PeopleSalary decimal(12,2) check(PeopleSalary>=1000 and PeopleSalary<=100000),--月薪,12位数,小数点后2位。
    PeoplePhone varchar(20) unique not null,--电话,unique:唯一约束
    PeopleAddress varchar(300),--地址
    PeopleAddTime smalldatetime default(getdate())--添加时间,默认时间方便用户。
)