数据库之创建数据库

数据库创建

1、创建数据库

关于配置,创建数据库的文件路径不能是sql sever的下载路径,否则会报错。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qSQVHtt7-1666789975947)(C:\Users\kerrla\AppData\Roaming\Typora\typora-user-images\image-20220926225545166.png)]

上图为数据库列表,每次创建的时候需要刷新才能出现创建的新表和新库

create database h1_19377215
on
(name=H1_dat, 
filename='D:\SQL\MSSM_work\H1_data.mdf',
size=5mb,
maxsize=100mb,
filegrowth=5mb
)
log on
(name=H1_log,
filename='D:\SQL\MSSM_work\H1_data.ldf',
size=5mb,
maxsize=100mb,
filegrowth=5mb
)
/*
注意需要使用该数据库,只有这样才能将表建在该数据库下
*/
use h1_19377215

on/on primary后跟的是主数据文件的参数,log on是日志文件的参数

一个数据库可以包含1个主要数据文件(mdf),多个次要数据文件(ndf),多个日志文件(ldf)

name指的是文件的逻辑名,也就是我们看到的名字;filename指的是物理名称,也就是文件的保存路径。

size是数据文件的初始容量大小,maxsize是文件的最大容量(如果等于unlimited则表示文件的最大容量没有限制),filegrowth表示的文件每次增加的容量大小,一般默认是增加10%。

2、创建department表

只不过被命名成了Htable1_19377215

create table Htable1_19377215
(detnumber int not null,
dettext varchar(10) not null default 'MIS',
primary key(dettext)
)
create table <数据库名>.<表名>
(<列名> <data type> limit,
 <列名> <data type> limit,
 table level integrity constraints
)
/*
char(n) --定长字符串
Vchar(n) --variable length string
text  --large string
int/ smallint/ float/ real(实数)/ numeric(a,b)\decimal(a,b)
a is total length,b is the length of decimal part
date/datetime/time
.etc
*/
3、创建student表
create table student_19377215
(sno char(8) not null Primary key,
sname char(20) not null unique,
sdept varchar(10) not null default('MIS'),
gender char(2) check(gender='男' or gender='女'),
foreign key(sdept) references Htable1_19377215(dettext)
	on delete no action
	on update set default
)
4、创建course表
create table course_19377215
(
cno char(4) not null primary key,
cname char(10) not null,
ccredit char(4) not null
)
5、创建sc表
create table sc_19377215
(
sno char(8) not null,
cno char(4) not null,
grade int check(grade>=0 and grade<=100),
primary key(sno,cno),
foreign key (sno) references student_19377215,
foreign key(cno) references course_19377215
)
integrity constraints

common cnstraints

  • not null: the specified column is not allowed to have null values.

  • unique: there aren’t duplicate values in specified column.

  • primary key: uniquely identified one row in the table,ane null value is not allowed.

  • foreign key:

    foreign key(sdept) references Htable1_19377215(dettext)
    

    foreign key’s type must be the same as the key type of the joined key.

  • check: conditional constraints

15(dettext)


foreign key's type must be the same as the key type of the joined key.

- check: conditional constraints

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值