四时宝库

程序员的知识宝库

零基础转型C#软件工程师-62数据表的创建

一、建表的语法规范

规范如下:

create table 数据表名称

字段1名称 数据类型 列的特征,

字段2名词 数据类型 列的特征,

...

go

注意:最后的go批处理语句必须要有

二、列的特征包括的内容

2.1是否为(NULL)

在输入数据的时候,数据库的列为空时,可以不输入数据,否则必须输入

not null不能为空的意思

2.2是否是列标识

数据库根据一定的规范自动生成标号,不需要我们输入

identity(10,1)标识列定义,从10开始,每增加一个增加1

2.3是否有默认值

如果某列用户不输入的时候,提供一个默认的内容

default(‘地址不详’) ,注意单引号

2.4是否为主键

主键是实体的唯一标识,保证实体不被重复,一个数据表必须有主键才有意义,如果没有,则更新和删除实体都可能会出现异常

primary key 设置主键

三、举例说明

打开SQL-新建查询-把我的代码复制到编辑区域-点击执行

use master --表示下面的操作是针对master完成的
go --加的时候要注意,该加加,不该加不加

--判断当前数据库是否在master数据库中存在
if exists(select*from sysdatabases where name='Student')
--删除数据库
drop database Student
go


--创建数据库
create database Student
on primary --相当于主文件夹
(
    name='Student_Mdata',  --数据库文件的逻辑名称(数据库内部使用的,我们看不到)
	filename='D:\SQLData\Student_Mdata.mdf',--数据库物理文件名(绝对路径)
	size=20MB,   --数据库文件初始大小
	filegrowth=1MB  --当数据超过文件大小的时候自动增长量
),
(
--次要数据文件(可以有多个,也可以没有)
  name='Student_ndata',  
	filename='D:\SQLData\Student_ndata.ndf',
	size=20MB,   
	filegrowth=1MB  
)
log on
(
   name='Student_log1',  
	filename='D:\SQLData\Student_log1.ldf',
	size=20MB,   
	filegrowth=1MB  
),
(
   name='Student_log2',  
	filename='D:\SQLData\Student_log2.ldf',
	size=20MB,   
	filegrowth=1MB  
)
go

use Student --注意创建数据表,必须要指向当前的数据库
go
if exists (select*from sysobjects where name='School')--查找系统是否有这个表
drop table School --有这个表则删除
go
create table School --创建机构表格
(
SchoolID int identity(10,1) primary key,--标识列 identity(10,1)意思是:从10开始,每增加1个,primary key主键不能重复
SchoolName varchar(50) not null

)
go

if exists (select*from sysobjects where name='Grade')--查找系统是否有这个表
drop table Grade --有这个表则删除
go
create table Grade --创建机构表格
(

GradeID int identity(20,1) primary key,--标识列 identity(10,1)意思是:从10开始,每增加1个,primary key主键不能重复
Grade1 varchar(20) not null,
Grade2 varchar(20) not null,
Grade3 varchar(20) not null,
Grade4 varchar(20) not null,
Grade5 varchar(20) not null,
Grade6 varchar(20) not null,
Grade7 varchar(20) not null,
Grade8 varchar(20) not null,
Grade9 varchar(20) not null
)
go
if exists (select*from sysobjects where name='StudentInformation')--查找系统是否有这个表
drop table StudentInformation --有这个表则删除
go

create table StudentInformation --创建机构表格
(

StudentID int identity(100,1) primary key,--标识列 identity(10,1)意思是:从10开始,每增加1个,primary key主键不能重复
EnterDate Datetime not null,  --日期类型
StudentName varchar(20) not null,
StudentSex  char(2)not null,--一个汉字站2个字符,所以多输入会报错
HomeAddress nvarchar(100) default('地址不详'), --默认约束
ParentsName varchar(20) not null,
PhoneNumber char(11) not null check(len(PhoneNumber)=11),--检查约束,手机号码必须11位
ProblemPoint varchar(300)not null,
BigTarget varchar(300)not null,
SmallGoal1  varchar(300)not null,
SmallGoal2 varchar(300)not null,
SmallGoal3 varchar(300)not null,
SmallGoal4 varchar(300)not null,
SmallGoal5 varchar(300)not null,
Notes varchar(300) null,
Planner  varchar(20)not null,
CompletionStatus varchar(300)not null,
FinishProject  varchar(300)not null,
EffectiveMethods varchar(300)not null,
FinishResult varchar(300)not null,
NextStep varchar(300)not null,
SpecificSoperation varchar(300)not null,
StudentGrade int references Grade(GradeID),--外键约束年级
StudentSchools int references School(SchoolID) --外键约束学校

)
go

四、注意要点

4.1数据类型的选择 根据实际需求设定大小

4.2约束的使用

常见的约束有主键约束、默认约束、检查约束、外键约束,而外键约束会让人认为很繁琐,但是在实际应该中,防止操作失误而误删数据库

4.3标识列

4.3.1标识列要求必须是整数类型或没有小数位的精准类型。

4.3.2标识列设计SchoolID int identity(10,1) primary key identity标识列,(10,1)标识从10开始生成编号,每次增加1,这里的10叫做标识种子,种子不能是负数,

4.3.3有标识列的数据被除某一行时,数据库会将该行空缺,而不会自动填补。

4.3.4标识列有系统自带维护,用户不能自己输入数据,也不能修改数值

4.3.5标识列可以同时定义为主键,也可以不定义主键,根据需求定义

5go批量处理语句

go是批量处理的标识,表示SQL Server将这些SQL语句编译为一个执行单元,

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接