首页 > 其他分享 >01.建库建表

01.建库建表

时间:2022-10-17 12:34:21浏览次数:51  
标签:01 建表 name -- key DBTEST table alter 建库

建库建表

一 、 创建数据库

--创建数据库
create database DBTEST
on    --数据文件
(
	name = 'DBTEST',     --逻辑名称
	filename = 'D:\SQL Sever\DATA\DBTEST.mdf', --物理路径和名称
	size = 5MB,					--文件的初始大小
	filegrowth = 1MB			--文件的增长方式,可以写大小,可以写百分比
)
log on   --日志文件 
(
	name = 'DBTEST_log',		 --逻辑名称
	filename = 'D:\SQL Sever\DATA\DBTEST_log.ldf' ,--物理路径和名称
	size = 5MB,					--文件的初始大小
	filegrowth = 1MB			--文件的增长方式,可以写大小,可以写百分比
)

sql 字符串用单引号表示,如果字符串本身也包括单引号,则使用两个单引号。

如果DBTEST这个数据库名已经被占用,则需要删除该数据库

if exists(select * from sys.databases where name= 'DBTEST')  --判断DBTEST是否存在
      drop database DBTEST  --删除数据库

上面的代码仅适用于学习阶段,删除数据库可能会丢失重要数据。

创建库总结的代码

if exists(select * from sys.databases where name= 'DBTEST')  --判断DBTEST是否存在
      drop database DBTEST  --删除数据库
      --创建数据库
create database DBTEST
on    --数据文件
(
	name = 'DBTEST',     --逻辑名称
	filename = 'D:\SQL Sever\DATA\DBTEST.mdf', --物理路径和名称
	size = 5MB,					--文件的初始大小
	filegrowth = 1MB			--文件的增长方式,可以写大小,可以写百分比
)
log on   --日志文件 
(
	name = 'DBTEST_log',		 --逻辑名称
	filename = 'D:\SQL Sever\DATA\DBTEST_log.ldf' ,--物理路径和名称
	size = 5MB,					--文件的初始大小
	filegrowth = 1MB			--文件的增长方式,可以写大小,可以写百分比
)

创建数据库的简写

--创建数据库的简写
create database DBTEST

如果按照上述的方式创建,数据库的数据文件和日志文件的相关信息,都会采取默认值。

二、创建表

创建表前需要将数据库切换到DBTEST数据库进行操作,可以使用鼠标进行操作也可以使用如下代码。

--切换数据库
use DBTEST

创建表的基本语法:

create table 表名
(
	字段1 数据类型,
	字段2 数据类型,
	.....
)

下面进行建表
建表(部门,职级,员工)

部门

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

如果已经有该表,则需要将其删除

--判断表是否存在                                               type = 'U'代表是用户创建的表
if exists(select * from sys.objects where name = 'Department' and type = 'U')
	drop table Department

上述的代码也只能在学习中使用,使用该代码可能会删除重要数据。

字符串类型:

char : 定长,char(10),存储空间必定占用10字节。
varchar : 变长,varchar(10),可以根据输入的字节数更改占用的字节大小,最多占用10个字节。
text : 长文本
char,varchar,text : 前面加n,存储unicode字符,对中文友好
  varchar(100) : 存储100个字母或50个中文
  nvarchar(100) : 存储100个字母或100个中文

创建职级表

--判断表是否存在                                               type = 'U'代表是用户创建的表
if exists(select * from sys.objects where name = 'Rank' and type = 'U')
	drop table [Rank]
	--因为Rank在SQL语言中属于关键字,所以加上[]
create table [Rank]
(
--职级编号    primary key:主键   identity(1,1):自动增长,初始值1,增长步长1
	RankId int primary key identity(1,1),
	--职级名称
	RankName nvarchar(50) not null, 
	--职级描述
	RankRemark text
)	

创建员工表

--判断表是否存在                                               type = 'U'代表是用户创建的表
if exists(select * from sys.objects where name = 'People' and type = 'U')
	drop table People
--员工
create table People
(
    PeopleId int primary key identity(1,1),    		--员工编号
    --references:约定,必须是另一个表内存在的属性 (引用外键)
    DepartmentId int references Department(DepartmentId) not null 		--部门
    RankId int references [Rank](RankId) not null 				--职级
    PeopleName nvarchar(50) not null,				--姓名
    --					default:设置默认值 check:进行约束  
    PeopleSex nvarchar(1) default('男') check(PeopleSex='男'orPeopleSex='女'),		--性别
    --datetime 存储年月日时分秒   date 存储年月日   smalldatetime 表示的范围更小,表示最近的时间
    PeopleBirth smalldatetime not null,					--生日
    --decimal 精度高 总长度12,小数点后2位
    PeopleSalary decimal(12,2) check(PeopleSalary>=1000 and PeopleSalary<=100000) not null,		--月薪
    --unique:约束,数据只能是唯一的
    PeoplePhone varchar(20) unique not null,		--手机号
    PeopleAddr nvarchar(100),				--地址
    PeopleAddTime smalldatetime default(getdate())   --添加时间
)

创建表总结的代码

--    部门    职级    员工

--判断表是否存在                                               type = '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
)

--判断表是否存在                                               type = 'U'代表是用户创建的表
if exists(select * from sys.objects where name = 'Rank' and type = 'U')
	drop table [Rank]
	--因为Rank在SQL语言中属于关键字,所以加上[]
create table [Rank]
(
--职级编号    primary key:主键   identity(1,1):自动增长,初始值1,增长步长1
	RankId int primary key identity(1,1),
	--职级名称
	RankName nvarchar(50) not null, 
	--职级描述
	RankRemark text
)	

--判断表是否存在                                               type = 'U'代表是用户创建的表
if exists(select * from sys.objects where name = 'People' and type = 'U')
	drop table People
--员工
create table People
(
    PeopleId int primary key identity(1,1),    		--员工编号
    --references:约定,必须是另一个表内存在的属性 (引用外键)
    DepartmentId int references Department(DepartmentId) not null, 		--部门
    RankId int references [Rank](RankId) not null,			--职级
    PeopleName nvarchar(50) not null,				--姓名
    --					default:设置默认值 check:进行约束  
    PeopleSex nvarchar(1) default('男') check(PeopleSex='男' or PeopleSex='女'),		--性别
    --datetime 存储年月日时分秒   date 存储年月日   smalldatetime 表示的范围更小,表示最近的时间
    PeopleBirth smalldatetime not null,					--生日
    --decimal 精度高 总长度12,小数点后2位
    PeopleSalary decimal(12,2) check(PeopleSalary>=1000 and PeopleSalary<=100000) not null,		--月薪
    --unique:约束,数据只能是唯一的
    PeoplePhone varchar(20) unique not null,		--手机号
    PeopleAddr nvarchar(100),				--地址
    PeopleAddTime smalldatetime default(getdate())   --添加时间
)

三、修改表结构

--添加列
--alter table 表名 add 新列名 数据类型
--增加邮箱一列
alter table People add PeopleMail varchar(50)

--删除列
--alter table 表名 drop column 列名
--删除邮箱列
alter table People drop column PeopleMail

--修改列
--alter table 表名 alter column 列名 数据类型
--修改地址这一列
alter table People alter column PeopleAddr varchar(200)
--维护约束

--删除约束
--alter table 表名 drop constraint 约束名
--添加约束(check约束)
--alter table 表名 add constraint 约束名 check(表达式)
--添加约束(主键)
--alter table 表名 add constraint 约束名 primary key(列名)
--添加约束(唯一)
--alter table 表名 add constraint 约束名 unique(列名)
--添加约束(默认值)
--alter table 表名 add constraint 约束名 default 默认值 for 列名
--添加约束(外键)
--alter table 表名 add constraint 约束名 foreign key(列名) references 关联表名(列名)

标签:01,建表,name,--,key,DBTEST,table,alter,建库
From: https://www.cnblogs.com/Starry-blog/p/16797254.html

相关文章