数据库笔记1

use master
go

if exists(select * from sysdatabases where name=‘StudentManageDB’)
drop database StudentManageDB
go
–创建数据库
create database StudentManageDB
on primary
(
–数据库文件的逻辑名(数据库管理系统用的,必须唯一)
name=‘StudentManageDB_data’,
–数据库的物理文件名(绝对路径)
filename=‘D:\DB\StudentManageDB_data.mdf’,–主数据文件
–数据文件初始大小
size=20MB,–实际开发中,请根据需要设置合理的大小
–数据文件增长量 (建议不要指望这个增长)
filegrowth=5MB
)
,
(
name=‘StudentManageDB_data1’,
filename=‘D:\DB\StudentManageDB_data.ndf’,–次要数据文件
size=20MB,
filegrowth=5MB
)
log on
(
name=‘StudentManageDB_log’,
filename=‘D:\DB\StudentManageDB_log.ldf’,–主数据文件
size=20MB,
filegrowth=5MB
),
(
name=‘StudentManageDB_log1’,
filename=‘D:\DB\StudentManageDB_log1.ldf’,–主数据文件
size=20MB,
filegrowth=5MB
)
go
–创建数据表:建议,我们最好要给数据做统一的规范命名(建议大家使用Pascal命名法)
use StudentManageDB
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=‘Students’)
drop table Students
go
create table Students
(
StudentId int identity(100000,1) primary key,
StudentName varchar(20) not null,
Gender char(2) check(Gender=‘男’ or Gender=‘女’), --check检查约束
DateOfBirth datetime not null ,–(Birthday)
StudentIdNo numeric(18,0) check(len(StudentIdNo)=18) unique, --身份证要求唯一,
–StudentIdNo char(18) check(len(StudentIdNo)=18) unique, --实际应用中,使用char(18)
Age int check(age>18 and age<35),
PhoneNumber varchar(50) ,
StudentAddress nvarchar(200) default(‘地址不详’), --default是默认约束
ClassId int references StudentClass(ClassId) --外键约束,要求:列的数据类型和长度和主键表对应字段必须一致
)
go
–创建管理员用户表
if exists(select * from sysobjects where name=‘Admins’)
drop table Admins
go
create table Admins
(
LoginId int identity(1000,1) primary key,
LoginPwd varchar(20) not null,
AdminName 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 references Students(StudentId),
CSharp int null,
SQLServerDB int null,
UpdateTime datetime default(getdate())
)
go

–print getdate()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值