• SQL Server如何建表


    一、数据表的组成

    实现完整性的约束有:

    –6个约束
    –非空 not null
    –主键 primary key
    –唯一 unique
    –检查 check
    –默认 default
    主键自增 identity

    表约束
    主键约束:值不能为null,且不能重复
    非空约束:不能为null
    默认约束:默认为xx
    检查check约束:判断(男和女)
    唯一约束:不能重复,可以能为null

    –create 创建
    –table 表

    –建表语句
    create table student(
    –格式:列名 类型 约束
    );

    字符全部都是 单引号 ’
    或者 or , 并且 and
     

    方法二:使用T-SQL语句创建

            1、点击新建查询

    1. use DBAliSys--使用DBAliSys数据库库名可任意更改成存在的库名
    2. create table student( --创建名为student的表
    3. ID int primary key identity(1, 1), --primary key 主键,identity(起始值, 自增量)
    4. sno varchar(10) unique not null, --unique 唯一约束not nu11非空
    5. sname char(6) not null, --不为空
    6. Age int default 18, --default设置默认值
    7. sex char(2) default '男' --默认为男
    8. --age int check (age between 0 and 30)--范围在0到30之间
    9. )

    (1)主键约束

    关键字:primary key

    作用:用来标识这个表中唯一的列,这一列里面的数必须都不相同,就像我们的身份证号。

    用SQL Server语句创建一个名为 visits 的新表来跟踪客户的店内访问:

    1. CREATE TABLE sales.visits (
    2. visit_id INT PRIMARY KEY IDENTITY (1, 1),
    3. first_name VARCHAR (50) NOT NULL,
    4. last_name VARCHAR (50) NOT NULL,
    5. visited_at DATETIME,
    6. phone VARCHAR(20),
    7. store_id INT NOT NULL,
    8. FOREIGN KEY (store_id) REFERENCES sales.stores (store_id)
    9. );

    示例中的解析:
    (1)visit_id 列是表的主键列。 IDENTITY(1,1) 指示SQL Server自动生成从 1 开始的列的整数,并为每个新行递增 1 。
    (2)first_name 和 last_name 列是 VARCHAR 类型的字符串列。这些列最多可以存储 50 个字符
    (3)visited_at 是 DATETIME 数据类型的列,记录客户访问商店的日期和时间。
    (5)phone 列是一个接受 NULL 的 VARCHAR 字符串列。
    (6)store_id 列存储标识客户访问商店的标识号
    (7)表定义的末尾是 FOREIGN KEY 约束。此外键确保visit表的store_id列中的值必须在stores 表的store_id 列中可用。可在后续教程中了解有关 FOREIGN KEY 约束的更多信息。
     

    1)每个表应该有一个由一列或多列组成的主键。通常,首先列出主键列,然后列出其他列。如果主键只包含一列,则可以在列名后使用 PRIMARY KEY 关键字如果主键由两列或更多列组成,则需要将 PRIMARY KEY 约束指定为表约束。 每个列都在语句中的名称后面指定了关联的数据类型。 列可能具有一个或多个列约束,例如: NOT NULL 和 UNIQUE 。
    2)表可能在表约束部分中指定了一些约束,例如:FOREIGN KEY,PRIMARY KEY,UNIQUE和CHECK 。

    4.创建外键

    1. create table 表名(
    2. 列名1 参数,
    3. 列名2 参数,
    4. foreign key(列名) references 目标表名(目标列名)
    5. )

    5.添加外键

    1. 比如stuInfo(学生信息表)表是主表。他的主键是stuID,
    2. 另外还有一个stuExam表(学生考试成绩表)。在这个表中也有个列是stuID,但是要引用主表中的stuID.
    3. 那么在创建约束的时候:
    4. alter table stuExam
    5. add constraint fk_stuID foreign key(stuID) references stuInfo(stuID)
    6. go

    1. 约束
    2. 非空约束 --NN,ont null constraint
    3. 必须填写数据不能为空
    4. --指定表 Student 添加名为NN_Student_sClassId非空约束(指定列名sClassId),括号输入表达式
    5. alter table Student add constraint NN_Student_sClassId check(sClassId is not null)
    6. 主键约束 --PK,primary key constraint
    7. 唯一且不为空
    8. --指定表 Student 添加名为PK_Student_sId主键约束(指定列名sId)
    9. alter table Student add constraint PK_Student_sId primary key(sId)
    10. 唯一约束 --UQ,unique constraint
    11. 唯一,允许为空,但是同样的数据只能出现一次
    12. --指定表 Student 添加名为UQ_Student_sName唯一约束(指定列名sName)
    13. alter table Student add constraint UQ_Student_sName unique(sName)
    14. 默认约束 --DF,default constraint
    15. 设置默认值
    16. --指定表 Student 添加名为DF_Student_sName默认约束(指定列名sBirthday),获取当前日期
    17. alter table Student add constraint DF_Student_sName default(getdate()) for sBirthday
    18. --指定表 Student 添加名为DF_Student_sName默认约束(指定列名sBirthday),指定日期
    19. alter table Student add constraint DF_Student_sName default('1995-12-12') for sBirthday
    20. --指定表 Student 添加名为DF_Student_sName默认约束(指定列名sSex),指定性别
    21. alter table Student add constraint DF_Student_sSex default('男') for sSex
    22. 检查约束 --CK,check constraint
    23. 设置范围以及格式限制
    24. --指定表 Student 添加名为 CK_Student_sSex检查约束(指定列名sSex),限制为'男'或者'女'
    25. alter table Student add constraint CK_Student_sSex check(sSex='男' or sSex='女')
    26. --指定表 Student 添加名为 CK_Student_sSex检查约束(指定列名sAge),限制为0-100之间的数字
    27. alter table Student add constraint CK_Student_sAge check(sAge>=0 and sAge<=100)
    28. 外键约束 --FK,foreign key constraint
    29. 表关系
    30. alter table Student add constraint Fk_Student_sClassId foreign key(sClassId) references Class(cId)
    31. --指定表Student添加sClassId外键为Class的主键cId
    32. on delete cascade on update cascade --级联删除 --级联更新
    33. 删除约束
    34. alter table Student drop Constraint NN_Student_sClassId --删除指定表中的约束

    1. create table 仓库
    2. (
    3. 仓库编号 int primary key , --主键的关键字primary key--
    4. 仓库号 varchar(50) unique, --唯一索引关键字unique--
    5. 城市 varchar(50) default '青岛', --不能为空not null--
    6. 面积 int check (面积>=300 and 面积<=1800)
    7. )
    8. create table 职工表
    9. (
    10. 职工编号 int identity (1,1) primary key,
    11. 职工号 varchar(50) unique,
    12. 仓库号 varchar(50),
    13. 基本工资 int check(基本工资>=800 and 基本工资<=2100)
    14. )
    15. create table 订单表
    16. (
    17. 订单编号 int identity(1,1) primary key,
    18. 订单号 varchar(50) unique,
    19. 职工号 varchar(50) references 职工表(职工号),--references两张表通过“职工号”关联--
    20. 订购日期 datetime,
    21. 销售金额 int
    22. )
    23. create table 工资表
    24. (
    25. 职工编号 int identity (1,1) primary key,
    26. 职工号 varchar(50) unique,
    27. 仓库号 varchar(50),
    28. 基本工资 int check(基本工资>=800 and 基本工资<=2100),
    29. 加班工资 int,
    30. 奖金 int,
    31. 扣率 int,
    32. 应发工资 as (基本工资+加班工资+奖金-扣率) --as为自动计算字段,不能输入值--
    33. )

  • 相关阅读:
    学习正则表达式
    SQL中的约束
    Stacked Hourglass Networks for Human Pose Estimation 源码分析
    基于Flask的岗位就业可视化系统(三)
    《运营商劫持, 中间人攻击, 黑客入侵怎么办?》- HTTPS 技术反制
    工作摸鱼秘籍
    区块链实验室(23) - FISCO中PBFT耗时与流量特征
    设计模式之观察者模式
    【python学习小案例】提升兴趣之模拟系统入侵
    【云原生】-国产开源数据库openGauss容器部署
  • 原文地址:https://blog.csdn.net/zgscwxd/article/details/134455952