SQL Server基础(一)

本文介绍了SQLServer中如何创建数据库,包括设置逻辑名、物理路径和文件增长等参数。接着讲解了检查数据库是否存在并删除的语句。在表的创建部分,详细阐述了不同类型的数据字段,如char、varchar、text等,并展示了创建部门、职级和员工表的示例,以及外键约束的使用。最后,讨论了数据的增删改查操作,包括插入、修改表结构、维护约束以及更新数据的方法。
摘要由CSDN通过智能技术生成

SQL Server基础(一)

SQL Server(关系型数据库)

一、数据库创建

1.Create Database

create database DBtest on
(
	name = ‘DBtest’, --logic name
	filename = ‘D:\DATA\DBtest.mdf’,--physical path and name
	size = 5MB, --the size of file
	filegrowth = 2MB --the growth of the file, size or percentage will be ok 
)
log on --log file
(
	name = ‘DBtest_log’, --logic name
	filename = ‘D:\DATA\DBtest_log.ldf’, —physical path and name
	size = 5MB, --the size of file
	filegrowth = 2MB --the growth of the file, size or percentage will be ok 
)
--Simplified case:
create database DBtest -- The parameters are default values.

2. Check if database exists

if exists (select * from sys.databases where name = ’DBtest’)
Drop database DBtest

3. Change database

use DBtest1

二、表的创建

1.Creat table

Attachment:
The type of String:
(1) char: 定长,例:char(10):无论存储数据是否到了10字节,都要占用10字节;
(2) varchar: 变长,最多占用10个字节,真实占用字节数视情况而定;
(3) text: 长文本,效率较低;
(1)、(2)、(3)前面加’n’表示存储unicode字符,对中文友好,具体来说
varchar(100): 最大存储100个字母或50个汉字
nvarchar(100): 存储100个字母或100个汉字

--部门表
create table Department
(
	--The ID of the department, data type, primary key, auto increment (initial value, step)
	DepartmentId int primary key identity (1,1),
	--The name of the department, data type(maximum number of characters), constraint
	DepartmentName nvarchar(50) not null,
	--The remark of the department, datatype
	DepartmentRemark text
)
--职级表
create table [Rank]
(
	RankId int primary key identity (1,1),	
	RankName nvarchar(50) not null,	
	RankRemark text
)
--员工表
create table employee
(
	EmployeeId int primary key identity (1,1),
	--The key of 部门表  外键 reference: 引用外键,检查添加的ID是否在部门表中存在
	DepartmentId int references Department(DepartmentId) not null,
	--The key of 职级表  外键: 将其他表的主键作为该表的外键
	RankId int references [Rank](RankId) not null,
	EmployeeName nvarchar(50) not null,
	--the default sex of Employee is male, check the input is legal or not
	EmployeeSex nvarchar(1) default('男')check(EmployeeSex='男' or EmployeeSex = '女'),
	EmployeeRemark text,
	--Type 'date' only stores year,month and day.'datetime' can store  year,month,day and Hours, minutes, seconds.
	EmployeeBirth datetime not null,
	--float浮点数有误差;decimal(12,2) represents the length of this data is 12, and 2 decimal places down
	EmployeeSalary decimal(12,2) check(EmployeeSalary>=1000 and EmployeeSalary <= 10000000) not null,
	--unique:the unique key
	EmployeePhoneNum varchar(20) unique not null,
	EmployeeAddress varchar(300),
	--smalldatetime:from 1900-01-01 to 2079-06-06
	EmployeeAddTime smalldatetime default(getdate())
)

Attachment:
The type of String:
(1) char: 定长,例:char(10):无论存储数据是否到了10字节,都要占用10字节;
(2) varchar: 变长,最多占用10个字节,真实占用字节数视情况而定;
(3) text: 长文本,效率较低;
(1)、(2)、(3)前面加’n’表示存储unicode字符,对中文友好,具体来说
varchar(100): 最大存储100个字母或50个汉字
nvarchar(100): 存储100个字母或100个汉字

2. Check if table exists

--type=’U’ represents the user-defined fields
if exists(select * from sys.objects where name = ‘Department’ and type = ‘U’) 
drop table Department 

三、数据增删改查

*以部门表为例
1.数据插入

insert into Department(DepartmentName,DepartmentRemark)
values('Marketing Department''...')
insert into Department(DepartmentName,DepartmentRemark)
values('Software Department''...')
insert into Department(DepartmentName,DepartmentRemark)
values('Planning Department''...')
--insert Multirow data
insert into Department(DepartmentName,DepartmentRemark)
select 'Testing Department''...' union
select 'Product Department''...' union
select 'Security Department''...'

在这里插入图片描述

2.修改表
修改表的结构:
(1)添加列

--         table name   new column name data type
alter table employee add employeeMail varchar(200)

在这里插入图片描述

(2)删除列

alter table employee drop column employeeMail

在这里插入图片描述

(3)修改列

--修改地址的varchar(300) to varchar(200)
alter table employee alter column EmployeeAddress varchar (200) 

在这里插入图片描述
3.维护约束(删除、添加)
(1)删除约束

--删除约束
--                                   约束名
alter table employee drop constraint CK__employee__Employ__693CA210

(2)添加约束

--添加check约束
alter table employee add constraint constraint1 check(EmployeeSalary>=1000 and EmployeeSalary <= 10000000)
--添加主键约束
--alter table 表名 add constraint 约束名 primary key(列名)
--添加唯一约束
--alter table 表名 add constraint 约束名 unique(列名)
--添加默认约束
--alter table 表名 add constraint 约束名 default 默认值 for (列名)
--添加外键约束
--alter table 表名 add constraint 约束名 foreign key(列名)
--references 关联表名(列名(主键))

4. 修改数据
(1) 单字段更新

update employee set EmployeeSalary = EmployeeSalary + 1000
--有条件地更新
update employee set EmployeeSalary = EmployeeSalary + 500 
where EmployeeId = 7
--Exmp2
update employee set EmployeeSalary = 6666 
where DepartmentId = 1 and EmployeeSalary <1000

(2)多字段更新

update employee set EmployeeSalary =  EmployeeSalary*2,EmployeeAddress = '北京'
where DepartmentId =1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值