操作和创建库表之SQLServer

1.创建库SQL

if exists(select * from sys.databases where name='DBTEST') drop database DBTEST;
--创建数据库
create database DBTEST
on --数据文件
(
name='DBTEST',--逻辑名称
filename='D:\DATA\DBTEST.mdf',--物理路径和名称
size=5MB,--文件的初始大小
filegrowth=2MB --文件增长方式可以写大小,也可以写百分比
)
log on --日志文件
(
name='DBTEST_log',--逻辑名称
filename='D:\DATA\DBTEST_log.ldf',--物理路径和名称
size=5MB,--文件的初始大小
filegrowth=2MB --文件增长方式可以写大小,也可以写百分比
);
--简单创建数据库,数据文件和日志文件都是默认路径
--create database DBTEST;

2.创建表


--切换数据库
use DBTEST;
--判断表是否存在
if exists(select *from sys.objects where name='Department' and type='U')
drop table Department;
--创建表基本语法
create table [Department]
(
--部门编号
--primary key:主键
--identity自动增长,初始值1,增长步长:1
DepartmentId int primary key identity(1,1),
--部门名称
--not null 不能为空
DepartmentName nvarchar(50) not null,
--部门描述 text:长文本
DepartmentRemark text
);

if exists(select *from sys.objects where name='Rank' and type='U') drop table [Rank];
create table [Rank]
(
--职级编号
--primary key:主键
--identity自动增长,初始值1,增长步长:1
RankId int primary key identity(1,1),
--职级名称
RankName nvarchar(50) not null,
--职级描述 text:长文本
RankRemark text
);
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),
--部门,外键
DepartmentId int references [Department](DepartmentId) not null,
--职级,外键
RankId int references [Rank](RankId) 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<=100000) not null,
--电话
PeoplePhone varchar(11) unique not null,
--地址
PeopleAddress varchar(300),
--添加时间
PeopleAddTime smalldatetime default(getdate())
);

注:

char:定长类型,char(10),占用十个字节,最大只可存储10个字节

varchar:变长类型,varchar(10),占用字节自动缩减,最大存储10个字节

text:长文本

char,varchar,text前面加n:存储unicode字符,对中文友好

varchar(100):存储100个字母或者50个汉字,nvarchar(100):存储100个字母或者100个汉字

3.修改表结构

--添加列
--alter table 表名 add 新列名 数据类型
alter table [People] add PeopleMail varchar(200); 
--删除列
-- alter table 表名 drop column 列名
alter table [People] drop column PeopleMail;
--修改列
--alter table 表名 alter column 列名 数据类型
alter table [People] alter column PeopleAddress varchar(200);
--删除约束
--alter table 表名 drop constraint 约束名
--删除部门的外键约束
alter table [People] drop constraint fk_Department_DepartmentId;
--增加约束
--添加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 [关联表名](关联主键)
alter table [People] add constraint fk_Department_DepartmentId foreign key(DepartmentId) references [Department](DepartmentId)
 -- 确保修改外键引用字段的值时会同步更改外键字段的值
      ON DELETE CASCADE
      ON UPDATE CASCADE;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值