首页 > 资讯列表 > 编程/数据库 >> 数据库操作教程

sqlserver建库、建表、建约束技巧

数据库操作教程 2022-09-23 17:42:36 转载来源: 网络整理/侵权必删

下面给大家分享下sqlserver建库、建表、建约束技巧,下文介绍有文字有代码。--创建School数据库之前:首先判断数据库是否存在,若存在则删除后再创建,若不存在则创建----exists关键字:括号里边能查询到数据则返回‘true'否则返回‘false'ifexists(select*fromsysdatabaseswherename='School')--exists返回‘true'则执行删除数据库操作--dropdatabaseSchool--exists返回‘false'则表明数据库不存在,直接创建createdatabaseSchoolonprimary(--主数据库文件--name='School',--主数据文件逻辑名fileName='D:projectSchool.mdf',--主数据文件物理逻辑名size=5MB,--初始值大小maxsize=100MB,--最大大小filegrowth=15%--数据文件增长量)logon(--日志文件--name='School_log',filename='D:projectSchool_log.ldf',size=2MB

下面给大家分享下sql server建库建表、建约束技巧,下文介绍有文字有代码。

--创建School数据库之前:首先判断数据库是否存在,若存在则删除后再创建,若不存在则创建--
--exists关键字:括号里边能查询到数据则返回‘true' 否则返回‘false'

if exists(select * from sysdatabases where name = 'School')--exists返回‘true'则执行删除数据库操作--drop database School--exists返回‘false'则表明数据库不存在,直接创建 create database Schoolon primary(--主数据库文件--name = 'School', --主数据文件逻辑名fileName = 'D:projectSchool.mdf', --主数据文件物理逻辑名size = 5MB, --初始值大小maxsize = 100MB, --最大大小filegrowth = 15% --数据文件增长量)log on(--日志文件--name = 'School_log',filename = 'D:projectSchool_log.ldf',size = 2MB,filegrowth = 1MB)go

----------------------------------------使用T-SQL创建employee数据库------------------------------------

create database employeeon primary(--主要数据文件--name = 'employee1',filename = 'D:projectemployee1.mdf',size = 10MB,filegrowth = 10%),(--次要数据文件--name = 'employee2',filename = 'D:projectemployee2.ndf',size = 20MB,maxsize = 100MB,filegrowth = 1MB)log on(--第一个日志文件--name = 'employee_log1',filename = 'D:projectemployee_log1.ldf',size = 10MB,filegrowth = 1MB),(--第二个日志文件--name = 'employee_log2',filename = 'D:projectemployee_log2.ldf',size = 10MB,maxsize = 50MB,filegrowth = 1MB)

---------------------------------查询已存在的数据库信息---------------------------

select * from sysdatabases

---------------------------------删除数据库------------------------------------

复制代码 代码如下:

drop database School

---------------------------------创建Student数据库表----------------------------

复制代码 代码如下:

--1、选择操作的数据库--
use School
go

--判断表是否存在--

复制代码 代码如下:

if exists(select * from sysobjects where name = 'Student')
drop table Student

--2、创建表---

create table Student(--具体的列名 数据类型 列的特征(是否为空)--StudentNo int identity(2,1) not null,LoginPwd nvarchar(20) not null,StudentName nvarchar(20) not null,Sex int not null,GradeId int not null,phone nvarchar(50) not null,BornDate datetime not null,Address nvarchar(255),Email nvarchar(50),IDENTITYcard varchar(18))go

---查看所有数据库对象(数据库表)---

复制代码 代码如下:

select * from sysobjects
drop table Student

----------------------创建subject课程表-------------------

复制代码 代码如下:

-----1、判断表是否存在;若存在则删除再创建,若不存在则直接创建--------
if exists(select * from sysobjects where name = 'subject')
drop table subject

use School
go

---创建subject课程表--
create table subject
(
SubjectNo int not null identity(1,1),
SubjectName nvarchar(50),
ClassHour int,
GradeID int
)

----------------------------------------创建Result成绩表-------------------

复制代码 代码如下:

-----1、判断表是否存在;若存在则删除再创建,若不存在则直接创建--------
if exists(select * from sysobjects where name = 'Result')
drop table Result
use School
go

---创建Result成绩表--

复制代码 代码如下:

create table Result
(
StudentNo int not null,
SubjectNo int not null,
ExamDate Datetime not null,
StudentResult int not null
)

-----------------------------------------创建Grande年级表-------------------

复制代码 代码如下:

-----1、判断表是否存在;若存在则删除再创建,若不存在则直接创建--------
if exists(select * from sysobjects where name = 'Grade')
drop table Grade
use School
go

---创建Grande年级表--

复制代码 代码如下:

create table Grade
(
GradeId int not null,
GrandeName nvarchar(50)
)

-----------------------------------------T-SQL添加约束-------------------------

复制代码 代码如下:

--给StudentNo添加主键约束---
alter table Student
add constraint pk_StuNo primary key(StudentNo)

--给身份证添加唯一约束--

复制代码 代码如下:

alter table Student
add constraint uq_StuIdcard unique(IDENTITYcard)

---给地址address添加默认约束--

复制代码 代码如下:

alter table Student
add constraint df_stuaddress default('地址不详') for Address

---删除地址address默认约束---

复制代码 代码如下:

alter table Student
drop constraint df_stuaddress


----------出生日期添加检查约束--------

复制代码 代码如下:

alter table Student
add constraint ck_stuBorndate check(Borndate > '1980-01-01')

---------与Grand(年级表)建立主外键关系--------

--1、添加Grade主键(操作Grade)---

复制代码 代码如下:

alter table Grade
add constraint pk_graid primary key(GradeId)

--2、添加Grade外键(操作Student)--

复制代码 代码如下:

alter table Student
add constraint fk_stuGradeID foreign key(GradeId) references Grade(GradeId)

-------------------给subject课程表添加约束-----------------------

复制代码 代码如下:

----给subjectNo列添加主键约束------
alter table subject
add constraint pk_SubID primary key(SubjectNo)

------给课程名称subjectName添加非空约束;-----

复制代码 代码如下:

-----with nocheck:已经存在数据不通过check约束-------
alter table subject with nocheck
add constraint ck_subName check(SubjectName is not null)

-----学时必须大于0-----

复制代码 代码如下:

alter table subject with nocheck
add constraint ck_ClassHour check(ClassHour > 0)

-----与Grade年级表添加主外键约束----

复制代码 代码如下:

alter table subject with nocheck
add constraint fk_GradeID foreign key(GradeID)
references Grade(GradeID)


----------给result成绩表添加约束------------

-------添加多个约束---------

复制代码 代码如下:

alter table Result
add
constraint pk_No_subID_date primary key(StudentNo,SubjectNo,ExamDate),
constraint df_examdate default(getdate()) for ExamDate,
constraint ck_StudentResult check(StudentResult between 0 and 100),
constraint fk_StuNo foreign key(StudentNo) references Student(StudentNo),
constraint fk_subNo foreign key(SubjectNo) references Subject(SubjectNo)

--删除多个约束--

复制代码 代码如下:

alter table Result
drop constraint pk_No_subID_date,fk_subNo,fk_StuNo,ck_StudentResult,df_examdate

--------更改列的数据类型----------

复制代码 代码如下:

alter table Result
alter column StudentResult int

以上就是本文全部内容,希望大家喜欢。

标签: sqlserver 建库 建表 约束 技巧


声明:本文内容来源自网络,文字、图片等素材版权属于原作者,平台转载素材出于传递更多信息,文章内容仅供参考与学习,切勿作为商业目的使用。如果侵害了您的合法权益,请您及时与我们联系,我们会在第一时间进行处理!我们尊重版权,也致力于保护版权,站搜网感谢您的分享!

站长搜索

http://www.adminso.com

Copyright @ 2007~2024 All Rights Reserved.

Powered By 站长搜索

打开手机扫描上面的二维码打开手机版


使用手机软件扫描微信二维码

关注我们可获取更多热点资讯

站长搜索目录系统技术支持