• 数据库、表的创建


    1.在建立数据库和数据库表前应该判断数据库和数据表是否存在

    2.建立数据表前应该切换到对应的数据库中,否则数据表可能添加到别的数据库中

    use master
    if exists(select * from sysdatabases where name='MySchool')
     drop database MySchool
    go 
    --创建文件夹
    exec sp_configure 'show advanced options',1
    go
    reconfigure
    go
    exec sp_configure 'xp_cmdshell',1
    go
    reconfigure
    go
    exec xp_cmdshell 'mkdir d:	emp'
    go--批处理结束的标记 。它会将语句块做为一个整体单元提交给服务器执行,可以提高效率,在以后,当多条语句是同一功能语句模块中的,那么就可以一起提交
    create database MySchool
    on --primary 如果不写,那么默认就是主文件组
    (
        size=3mb, --初始大小
        name='MySchool_data',--逻辑名称
        filegrowth=10mb,--文件增长
        maxsize=1000mb,--最大容量
        filename='d:	empMySchool_data.mdf' --文件物理路径
    )
    log on
    (
        size=3mb, --初始大小
        name='MySchool_log',--逻辑名称
        filegrowth=10mb,--文件增长
        --maxsize=1000mb,--最大容量
        filename='d:	empMySchool_log.ldf' --文件物理路径
    )
    go
    use MySchool --切换数据库
    if exists(select * from sysobjects where name='Grade')
     drop table Grade
    go
    create table Grade
    (
        --字段名称 字段类型 特征(标识,主键,唯一键,默认值,check约束,关系,非空)
        GradeId int primary key identity(1,1),
        GradeName nvarchar(50) not null
    )
    if exists(select * from sysobjects where name='Student')
     drop table Student
    go
    create table Student
    (
        StudentNo int identity(1,1),
        LoginPwd  varchar(30) not null, --字符串如果没有长度那么长度会默认为1
        StudentName nvarchar(50) not null,
        Gender nchar(1)  not null,
        GradeId int not null,
        Phone varchar(20) null,
        Address nvarchar(50),
        Birthday datetime not null,
        Email nvarchar(50)
    )
    --PK UQ CK DF FK
    --alter table 表名 add constraint 约束名称  约束类型  约束说明(值  字段名称  表达式)
    --    密码loginPwd的长度大于等于6位
    alter table student add constraint CK_loginPwd check(len(loginPwd)>=6)
    ----    studentNo学号是标识列 --标识列不能通过约束来添加
    alter table student 
    add constraint PK_StudentNo primary key(studentNo),
    --    Gender性别只能取1和0,1代表男,0代表女
    constraint CK_Gender check(gender='' or gender =''),
    --    GradeId是grade表的外键
    constraint FK_Student_GradeId foreign key(gradeId) references grade(gradeid)
    --on delete no action|cascade|set null|set default
    ,
    --    Address有默认值:“未填写”
    constraint DF_Address default(N'未填写') for address,
    --    Email:默认值 匿名@未知.com
    constraint DF_Email default(N'匿名@未知.com') for email
    数据库的建立
  • 相关阅读:
    湖水和岩石效应
    【转】正确理解MySQL中的where和having的区别
    Array类型及其常用的方法
    旧手机福音,桌面时间管理器
    集成knife4j
    IDEA装MyBatis Log插件,对于复杂的SQL进行分析
    springboot+logback日志规范
    使用 SecureRandom 产生随机数坑
    记录Lombok注解 @SneakyThrows的用法
    java解析svg
  • 原文地址:https://www.cnblogs.com/leiqun9497/p/3769559.html
Copyright © 2020-2023  润新知