zoukankan      html  css  js  c++  java
  • 用SQL语句建库建表建约束(用SQl语句在指定盘符创建文件夹)

    一 :创建数据库

    创建一个数据文件和一个日志文件(MySchool)

    create database MySchool
    on primary      --默认属于primary主文件组,可省略
    (
    --数据文件的具体描述

    name = 'MySchool_data'    --主数据库文件的逻辑名称
    filename = 'D:projectMySchool_data.mdf', --主数据库文件的物理名称
    size = 5MB,     --主数据库文件的初始大小
    maxsize = 100MB,     --主数据库文件增长的最大值
    filegrowth = 15%     --主数据文件的增长率

    )
    log on
    (
    --日志文件的具体描述,各参数含义同上

    name = 'MySchool_log',    --主数据库文件的逻辑名称
    filename = 'D:projectMySchool_data.ldf', --主数据库文件的物理名称
    size=2MB,     --主数据库文件的初始大小
    filegrowth = 1MB    --主数据文件的增长速度

    )
    go

    创建多个数据文件和多个日志文件(employees)
    create database employees
    on primary
    (
    --主数据库文件的具体描述
    name='employee1',
    filename='D:projectemployee1.mdf',
    size=10,
    filegrowth=10%
    ),
    (
    --次要数据库文件的具体描述
    name='employee2',
    filename='D:projectemployee2.mdf',
    size=20,
    maxsize=100,
    filegrowth=1
    )          
    log on
    (
    --日志文件1的具体描述
    name='employeelog1',
    filename='D:projectemployee1_log.ldf',
    size=10,
    maxsize=50,
    filegrowth=1
    ),
    (
    --日志文件2的描述
    name='employeelog2',
    filename='D:projectempolyee2_log.ldf',
    size=10,
    maxsize=50,
    filegrowth=1
    )
    go

    二:删除数据库
    usr master
    if exists(select * from sysdatabases where name='....')
    drop database ......

     

    三:创建和删除表

    use MySchool    --在Myschool中创建表
    go

    create table Student
    (
    StudentNo int not null.   --学号,int 类型,不允许为空
    LoginPwd nvarchar(50) not null,  --密码 nvarchar类型,不允许为空
    StudentName nvarchar(50) not null, --名字,nvarchar类型,步允许为空
    Sex bit not null,   --性别,取值0或1
    Email nvarchar(20)   --邮箱,可为空
    )
    go

    删除表
    use MySchool
    go
    if exists(select * from sysobjects where naem='Student')
    drop table Student
      
                                                                                                                                                                                                                                                                                                                                                                                                      
    四:创建和删除约束
    主键约束(Primary Key Constraint)
    非空约束(Not Null)
    唯一约束(Unique Constaraint)
    检查约束(Check Constaraint)
    默认约束(Default Constaraint)
    外建约束(Foreign Key Constarint):用于在两表之间建立关系,需要指定引用主表的哪一列

    alter table 表名
    add constraint 约束名  约束类型  具体的约束说明

    例:
    --添加主键约束
    alter table Student
    add constraint PK_StudentNo Primary Key(StudentNo)

    --添加唯一约束
    alter table student
    add constraint UQ_IdentityCard unique(IdentityCard)

    --添加默认约束
    alter table Student
    add constraint DF_Address default('地址不详') for address

    --添加检查约束
    alter table Student
    add constraint CK_BornDate checke(BornDate>='1980-01-01')

    --添加外键约束(Result是从表,Student是主表)
    alter table Result
    add constraint FK_StudentNo
    foreign key(StudentNo) references Student(StudentNo)
    go


    删除约束
    alter table 表名
    drop constraint 约束名

    例:删除学生表中的默认约束
    alter tablte Student
    drop constraint DF_Address


    怎样向已存在数据的表中添加约束
    alter table Employee with nocheck ( whit nocheck不向已存在的数据约束)
    add constraint

    向已存在的数据表中插入一列
    alter table 表名
    add 列名 数据类型  null

    在数据库中用SQL语句创建文件夹(例:在E盘创建一个project文件夹)

    exec sp_configure 'show advanced option',1

    go

    reconfigure

    go

    exec sp_configure 'xp_cmdshell',1

    go

    reconfigure

    go

    exec xp_cmdshell 'mkdir D:project'

  • 相关阅读:
    Pro/Toolkit示例之一:异步启动ProE
    Formatted MessageBox/AfxMessageBox
    Pro/Toolkit示例之二:同步Dll程式
    模拟按钮控件BN_CLICKED消息事件
    详解ProToolkit注册文件
    C++函数指针
    Message Basic
    C++指针之间的赋值与转换规则总结
    CString&CStringA&CStringW之间的相互转换
    Devexpress组件之XtraBars.PopupMenu的使用
  • 原文地址:https://www.cnblogs.com/liujiayun/p/5120147.html
Copyright © 2011-2022 走看看