常用到的四个数据库表格

本文详细介绍了四个关键数据库表格的设计:Students、Classes、Scores和Subjects。包括每个表的结构、字段类型、约束条件以及创建和修改表格的SQL代码。适合数据库初学者和需要复习SQL表设计的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

常用到的四个数据库表格

 

在我们的学习过程中经常用到的几个表格:Students表,Classes表,Scores表,Subjects表

Students表

表的结构及约束:

  1. 学生编号StudentNo(PK not null, )
  2. 学生姓名StudentName(nchar(10) ,)
  3. 登录密码LoginPwd(nchar(10) not null,)
  4. 年龄Age(int 0~100,)
  5. 性别Sex(bit,)
  6. 班级编号CLassId(FK int ,)
  7. 电话Phone(int(10),)
  8. 地址Address(nvarchar(50),)
  9. 生日Birthdays(date,)
  10. 电子邮箱Email(nvarchar(10),)
  11. 是否删除IsDel(default('False'))

常用到的四个数据库表格

 

表格创建代码如下:

use TextSchool
--Create table Students
if exists(select * from sysobjects where name = 'Students')
drop table Students
go
Create table Students
 (
 StudentNo int not null Primary Key ,
 StudentName nvarchar(50),
 LoginPwd nchar(10) not null,
 Age int,
 Sex bit,
 Classid int,
 Phone int,
 Adress nvarchar(50),
 Birthday date,
 Email nchar(10),
 IsDel bit default('False')
 )

约束创建如下:

--add constraint
if exists(select * from sysobjects where name='CK_Students_Age')
alter table Students
drop constraint CK_Students_Age
alter table Students
add constraint CK_Students_Age check(0<Age and Age<100)
go
if exists(select * from sysobjects where name ='FK_Students_Classid')
alter table Students 
drop constraint FK_Studnets_Classid
alter table Students
with nocheck
add constraint FK_Students_Classid foreign key(Classid) references Classes(Cid)
on	delete set null

Classes表

表的结构及约束:

  1. 班级编号CLassId( int ,)
  2. 班级名称ClassName (nchar(10))

常用到的四个数据库表格

 

表格创建代码如下:

use TextSchool

if exists(select * from sysobjects where name = 'Classes')

drop table Classes

create table Classes

(

Classid int primary key ,

Classname nchar(10)

)

go

Scores表

表的结构及约束:

  1. 标识列Id(int identity(1,1),)
  2. 学生编号StudentId(int,)
  3. 课程编号SubjectId(int, FK)
  4. 学生分数StudentScores(int ,)
  5. 考试时间ExamDate(date)

常用到的四个数据库表格

 

表格创建代码如下:

use TextSchool
if exists(select * from sysobjects where name ='Scores')
drop table Scores
create table Scores
(
Id int identity(1,1),
StudentNo int ,
SubjectId int ,
StudentScores int,
ExamDate date
)

Subjects表

表的结构及约束:

  1. 课程编号SubjectId (int not null PK,)
  2. 课程名称SubjectName(nvarchar(10) ,)
  3. 课程课时ClassHour(int,)
  4. 课程班级编号ClassId(int )

常用到的四个数据库表格

 

表格创建代码如下:

use TextSchool
if exists(select * from sysobjects where name ='Subjects')
drop table Subjects
create table Subjects
(
SubjectId int not null ,
SubjectName nvarchar(10),
ClassHour int,
ClassId int 
)
go

约束创建如下:

	--add constraint **table Subjects
	if exists(select * from sysobjects where name='PK_Subjects_SubjectId')
	alter table Subjects
	drop constraint PK_Subjects_SubjectId
	alter table Subjects
	add constraint PK_Subjects_SubjectId primary key (SubjectId)
	go

详细的如何创建数据库和表格请参见:

SQL语句创建和删除数据库

SQL语句创建表单table

SQL数据完整性介绍和SQL语句创建约束

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值