• SQL Server语句创建数据库和表——并设置主外键关系


    简单的创建数据库的 SQL 语句:

    复制代码
     1 use master
     2 go
     3 
     4 if exists(select * from sysdatabases where name='Test')
     5 begin
     6     select '该数据库已存在'
     7     drop database Test        --如果该数据库已经存在,那么就删除它
     8 end
     9 else
    10 begin
    11     create database Test
    12     on  primary        --表示属于 primary 文件组
    13     (
    14         name='stuDB_data',        -- 主数据文件的逻辑名称
    15         filename='D:stuDB_data.mdf',    -- 主数据文件的物理名称
    16         size=5mb,    --主数据文件的初始大小
    17         maxsize=100mb,     -- 主数据文件增长的最大值
    18         filegrowth=15%        --主数据文件的增长率
    19     )
    20     log on
    21     (
    22         name='stuDB_log',        -- 日志文件的逻辑名称
    23         filename='D:stuDB_log.ldf',    -- 日志文件的物理名称
    24         size=2mb,            --日志文件的初始大小
    25         maxsize=20mb,        --日志文件增长的最大值
    26         filegrowth=1mb        --日志文件的增长率
    27     )
    28 end
    复制代码

    接下来是创建数据表的 SQL 语句:

    1 use Test    --表示设置为在该数据库(Test)执行下面的SQL语句
    2 go

    可以先执行一下以上语句。

    或者在这里选择数据库。

    复制代码
     1 use Test    --表示设置为在该数据库(Test)执行下面的SQL语句
     2 go
     3 
     4 if exists(select * from sysobjects where name='Student')
     5 begin
     6     select '该表已经存在'
     7     drop table Student        --删除表
     8 end
     9 else
    10 begin
    11     create table Student
    12     (
    13         S_Id        int        not null    identity(1,1)    primary key,    --设置为主键和自增长列,起始值为1,每次自增1
    14         S_StuNo        varchar(50)        not null,
    15         S_Name        varchar(20)        not null,
    16         S_Sex        varchar(10)        not null,
    17         S_Height    varchar(10)        null,
    18         S_BirthDate        varchar(30)        null
    19     )
    20 end
    21 
    22 --添加约束                        
    23 alter table Student add constraint 
    24 UQ_S_StuNo    --约束名
    25 unique        --约束类型(唯一约束)
    26 (S_StuNo)    --列名
    27 
    28 --删除约束
    29 alter table Student drop constraint
    30 UQ_S_StuNo    --约束名
    复制代码

    SQL语句创建表变量:

    复制代码
     1 declare @Score table
     2 (
     3     Id        int        not null,
     4     Name    varchar(50)  null
     5 ) 
     6 
     7 insert into @Score
     8 select '1','刘邦' union
     9 select '2','项羽'
    10 
    11 select * from @Score
    复制代码

    SQL语句创建临时表:

    复制代码
     1 -- ## 表示全局临时表
     2 create table ##temp
     3 (
     4     Id        int        not null,
     5     Name    varchar(10)        null
     6 )
     7 
     8 -- # 表示局部临时表
     9 create table #temp
    10 (
    11     Id        int        not null,
    12     Name    varchar(10)        null
    13 )
    复制代码

    SQL 语句创建表并设置主外键关系:

    复制代码
     1 if exists(select * from sysObjects where name='Course')
     2 begin
     3     select '该表已经存在'
     4     drop table Course
     5 end
     6 else
     7 begin
     8     create table Course
     9     (
    10       --列名    字段类型  是否为空   标识外键列(外键列名)         关联表的表名(关联的字段名)
    11          Stu_Id        int        null    foreign key(Stu_Id) references Student(S_Id),
    12          C_Id        int        not null    identity(1,1)    Primary key,
    13          C_Name        varchar(100)    not null
    14      )
    15 end
    复制代码

    注意:表变量和临时表都不能添加约束,具体的特征和适用场景请参见:

    http://www.cnblogs.com/kissdodog/archive/2013/07/03/3169470.html

  • 相关阅读:
    冒泡排序
    CFURLCreateStringByAddingPercentEscapes
    AESCrypt加密与解密
    关于Xcode 的SDK与系统版本理解
    nginx 安全稳定版本
    bcom 遇到的那些问题
    nginx 配置404错误页面
    AES 对称加密解密
    SpringCloud stream连接RabbitMQ收发信息
    springboot1.5 和 2.0 引入 redis 并封装工具类
  • 原文地址:https://www.cnblogs.com/asdyzh/p/9818743.html
Copyright © 2020-2023  润新知