SQL Server数据库基础:使用T-SQL语言创建数据库、创建表、修改表结构、创建约束。

本文介绍了如何使用T-SQL语言在SQL Server中创建数据库、设计表结构、修改表以及设置约束,涵盖了数据库管理和表操作的基础知识。
一、 使用T-SQL语言创建数据库。

    创建新的数据库,为什么要选择master数据库?
    master数据库: 
        系统数据库,它记录了SQL Server系统的所有系统级信息,还记录了数据库文件的位置信息,以及SQL Server的初始化信息。


    使用T-SQL语言创建数据库:

    use master    -- 切换到master数据库
    go            -- 批处理命令

    create database StudentManagementDB                         -- 数据库名称
    on primary    -- 设置主要数据文件的基本信息
    (
        name = 'StudentManagementDB',                           -- 数据库主要数据文件的逻辑名称
        filename = 'D:\users\DBase\StudentManagementDB.mdf',    -- 数据库主要数据文件的路径(绝对路径)
        size = 5MB,                                             -- 数据库主要数据文件的初始大小
        filegrowth = 1MB                                        -- 数据库主要数据文件的增量
    )
    log on    -- 设置事务日志文件的基本信息
    (
        name = 'StudentManagementDB_log',                         -- 数据库事务日志文件的逻辑名称
        filename = 'D:\users\DBase\StudentManagementDB_log.ldf',  -- 数据库事务日志文件的路径(绝对路径)
        size = 1MB,                                               -- 数据库事务日志文件的初始大小
        filegrowth = 10%                                          -- 数据库事务日志文件的增量
    )
    go


    使用T-SQL语言删除数据库:

    drop database StudentManagementDB                             -- 数据库名称
    go



二、 使用T-SQL语言创建表。

    产品信息表(ProductInfo):
        标识列       Id                int 
        产品编号     ProductNo         varchar(50)
        产品名称     ProductName       nvarchar(100)
        产品类型编号  ProductTypeId     int 
        产品价格     ProductPrice      decimal(18, 2)
        产品数量     ProductCount      int 
        产品备注     ProductRemark     nvarchar(150) 

    产品类型表(ProductTypeInfo):
        产品类型编号  ProductTypeId    int 
        产品类型名称  ProductTypeName  nvarchar(50)  


    注意:在同一个数据库下不能有相同名称的表,在不同数据库下可以有相同名称的表。    

    
    使用T-SQL语言创建表:


    use ProductManagementDB
    go 


    create table ProductInfo
    (
        Id int identity(10001, 1) primary key not null,   -- 标识种子,自增量
        ProductNo varchar(50) not null,
        ProductName varchar(50) null,
        ProductTypeId int not null,
        ProductPrice decimal(18, 2) null,
        Count int not null
    )
    go


    create table ProductTypeInfo
    (
        ProductTypeId int identity(1, 1) primary key not null,
        ProductTypeName nvarchar(50) not null
    )
    go 


    使用T-SQL语言删除表:

    drop table ProductInfo
    go 


    drop table ProductTypeInfo
    go 



三、 使用T-SQL语言修改表结构。

    1、 当表为空表或者表中的数据不重要时(一般不推荐,后果很严重,会把整个表结构以及该表的所有数据都删掉)。
        先修改创建表的SQL脚本代码,再删掉该表,重新执行创建该表的SQL脚本代码。

    2、 使用 alter table 的SQL语句进行修改。 
        2.1 添加新的一列:
            alter table 表名 add 列名 数据类型 default值 是否为空

        2.2 删除指定的某一列:
            alter table 表名 drop column 列名

        2.3 修改指定的某一列:
            alter table 表名 alter column 列名 数据类型 default值 是否为空

        2.4 修改列名:
            -- 参数1:表名.旧列名,参数2:新列名,参数3:column
            exec sp_rename  '表名.旧列名', '新列名', 'column' 


    使用T-SQL语言修改表结构:

        -- 添加一列
        alter table ProductInfo add ProductRemark nvarchar(150) null
        go 


        -- 删除一列
        alter table ProductInfo drop column ProductRemark
        go 


        -- 修改列的数据类型和是否为null属性
        alter table ProductInfo alter column ProductName nvarchar(100) not null
        go 

        
        -- 修改列名(慎用,执行系统存储过程后会有提示:更改对象名的任一部分都可能会破坏脚本和存储过程)
        exec sp_rename 'ProductInfo.Count', 'ProductCount', 'column'
        go



四、 使用T-SQL语言创建约束。

    1、 使用T-SQL语言在创建表的过程中创建约束:


        use ProductManagementDB
        go 


        -- 先创建好主表,指定主键
        create table ProductTypeInfo
        (
            ProductTypeId int identity(1, 1) primary key not null,
            ProductTypeName nvarchar(50) not null
        )
        go 


        -- 再创建好从表,指定外键
        create table ProductInfo
        (
            Id int identity(10001, 1) primary key not null,    -- 标识种子,自增量
            ProductNo varchar(50) unique not null,              -- 指定unique约束
            ProductName nvarchar(100) not null,
            ProductTypeId int not null foreign key references ProductTypeInfo(ProductTypeId),   -- 指定ProductTypeInfo表中的ProductTypeId为外键
            ProductPrice decimal(18, 2) default(0.00) check(ProductPrice > 9.99 and ProductPrice < 499.99) not null,  -- 指定default约束和check约束
            ProductCount int default(0) not null,
            ProductRemark nvarchar(150) null
        )
        go


        约束命名规范:
        
        主键约束名称:PK_xx
        外键约束名称:FK_xx
        唯一约束名称:UQ_xx
        检查约束名称:CK_xx
        默认约束名称:DF_xx
        索引约束名称:IX_xx



    2、 使用T-SQL语言在表创建完成后创建约束:

        use ProductManagementDB
        go 

        drop table ProductInfo
        go 

        -- 创建新表,不设置任何约束
        create table ProductInfo
        (
            Id int identity(10001, 1) not null,    
            ProductNo varchar(50) not null,              
            ProductName nvarchar(100) not null,
            ProductTypeId int not null,   
            ProductPrice decimal(18, 2) not null, 
            ProductCount int not null,
            ProductRemark nvarchar(150) null
        )
        go


        -- 添加各种约束
        -- 1. 创建主键约束
            alter table ProductInfo add constraint PK_ProductInfo_Id primary key(Id)
            go 

        -- 2. 创建外键约束
            alter table ProductInfo add constraint FK_ProductInfo_ProductTypeId foreign key(ProductTypeId) references ProductTypeInfo(ProductTypeId)
            go

        -- 3. 创建unique约束
            alter table ProductInfo add constraint UQ_ProductInfo_ProductNo unique(ProductNo)
            go 

        -- 4. 创建check约束
            alter table ProductInfo add constraint CK_ProductInfo_ProductPrice check(ProductPrice > 9.99 and ProductPrice < 499.99)
            go 

        -- 5. 创建default约束
            alter table ProductInfo add constraint DF_ProductInfo_ProductPrice default(0.00) for ProductPrice
            go

            alter table ProductInfo add constraint DF_ProductInfo_ProductCount default(0) for ProductCount
            go 
        


        创建约束的T-SQL语法:

            alter table 表名 add constraint 约束名 
            primary key (列名) /
            foreign key (从表的列名) references 主表(列名) /
            unique(列名) /
            check(逻辑表达式) /
            default(缺省值) for 列名 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值