SQLServer数据表的创建

本文详细介绍了SQLServer中创建数据表的方法,包括基本语法、标识列的使用及建表实例。涵盖主键、默认值、标识列等概念,并通过具体案例演示如何创建学员信息、班级、成绩等表。

1 SQLServer数据表的创建

1.1 创建数据表的语法

create table 表名
{
	字段1 数据类型 列的特征,
	字段2 数据类型 列的特征,
	...
}
go

列的特征包含的内容如下:

  • 是否为空(NULL):在输入数据时,数据库的列允许为空时,可以不输入数据,否则必须输入。列是否为空要根据数据库设计的具体要求决定,对于关键列必须禁止为空。
  • 是否是标识列(自动编号)。
  • 是否有默认值:如果数据表的某列在用户不输入数据的时候,希望提供一个默认的内容,比如用户如果不输入学员地址,则默认‘地址不详’。
  • 是否为主键:主键是实体的唯一标识,保证实体不被重复。一个数据表必须有主键才有意义,否则更新和删除实体可能会出异常。

1.2 标识列的特殊说明

标识列使用的意义: 有时候,一个数据表存储的实体,很难找到不重复的列作为主键列,比如学员成绩表中存储着学生的多次考试成绩,则学号也很容易重复,其他列更无法做到不重复。SQLServer提供了一个“标识列”,也叫“自动增长列”或“自动编号”,它本身没有什么具体意义,但我们也可以让它表示特殊意义。比如学生成绩表中的自动表示列Id,不表示实体属性;但学生信息表中的StudentId也是标识列,但它表示学生实体属性(学号)。

标识列的使用方法:

  • 该列必须是整数类型,或没有小数位数的精确类型。
  • 标识种子:标识列的起始大小。
  • 表示增量:标识列每次递增(自动增加)的值。

注意问题:

  • 有标识列的数据被删除某一行时,数据库会将该行空缺,而不会填补。
  • 标识列由系统自动维护,用户既不能自己输入数据,也不能修改数值。
  • 标识列可以同时定义为主键,也可以不定义为主键,根据需要决定。

1.3 建表举例

建表举例1:


--创建学员信息数据表
use StudentManageDB
go
 if exists(select * from sysobjects where name='Students')
drop table Students
go
create table Students
(
	StudentId int identity(10000,1),--学号
	StudentName varchar(20) not null,--姓名
	Gender char(2) not null,--性别
	Birthday datetime not null,--出生日期
	StudentIdNo numeric(18,0) not null,--身份证号
	Age int not null,--年龄
	PhoneNumber varchar(50),
	StudentAddress varchar(500),
	ClassId int  not null   --班级外键
)
go
--创建班级表
if exists(select * from sysobjects where name='StudentClass')
drop table StudentClass
go
create table StudentClass
(
	ClassId int primary key,--班级编号
	ClassName varchar(20) not null
)
go
--创建成绩表
if exists(select * from sysobjects where name='ScoreList')
drop table ScoreList
go
create table ScoreList
(
	Id int identity(1,1) primary key,
	StudentId int not null,--学号外键
	CSharp int null,
	SQLServer int null,
	UpdateTime datetime not null--更新时间
)
go
--创建管理员表
if exists(select * from sysobjects where name='Admins')
drop table Admins
go
create table Admins
(
	LoignId int identity(1000,1) primary key,
	LoginPwd varchar(20) not null,--登录密码
	AdminName varchar(20) not null
)
go

建表举例2:

--指向要操作的数据库
use CourseManageDB
go
--创建讲师表
if exists(select * from sysobjects where name='Teacher')
drop table Teacher
go
create table Teacher
(
     TeacherId int identity(1000,1) primary key , --讲师编号,主键
	 LoginAccount varchar(50) not null, --登录账号
	 LoginPwd varchar(18)  not null,  
	 TeacherName varchar(20) not null,
	 Phonenumber char(11) not null,    --电话
	 NowAddress nvarchar(100) not null --住址
)
go
--课程分类表
if exists (select * from sysobjects where name='CourseCategory')
drop table CourseCategory
go
create table CourseCategory
(
     CategoryId  int identity(10,1) primary key,
     CategoryName varchar(20) not null    
)
go
--课程表
if exists (select * from sysobjects where name='Course')
drop table Course
go
create table Course
(
     CourseId  int identity(1000,1)  primary key,
     CourseName nvarchar(50) not null ,
	 CourseContent nvarchar(500) not null,
	 ClassHour int not null,--课时
	 Credit int  not null, --学分
	 CategoryId int  not null,  --外键约束
	 TeacherId int not null 
)
go

参考资料:

  1. .NET/C#工控上位机VIP系统学习班【喜科堂互联教育】
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值