[数据库] 数据库之创建数据库

2971 0
Honkers 2025-3-12 23:06:42 | 显示全部楼层 |阅读模式

数据库创建

1、创建数据库

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

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

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

  1. create database h1_19377215
  2. on
  3. (name=H1_dat,
  4. filename='D:\SQL\MSSM_work\H1_data.mdf',
  5. size=5mb,
  6. maxsize=100mb,
  7. filegrowth=5mb
  8. )
  9. log on
  10. (name=H1_log,
  11. filename='D:\SQL\MSSM_work\H1_data.ldf',
  12. size=5mb,
  13. maxsize=100mb,
  14. filegrowth=5mb
  15. )
复制代码
  1. /*
  2. 注意需要使用该数据库,只有这样才能将表建在该数据库下
  3. */
  4. use h1_19377215
复制代码

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

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

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

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

2、创建department表

只不过被命名成了Htable1_19377215

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

    1. 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)

  1. foreign key's type must be the same as the key type of the joined key.
  2. - check: conditional constraints
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行