数据库的实现

来源:互联网 发布:licecap for mac 编辑:程序博客网 时间:2024/06/16 18:31

一.创建数据库:

    1.语法: create database 表名

              on (

              <数据文件参数>[,·······] [<文件组参数>]

                    )

                 log on

                    (

                   <日志文件参数>[,········]

                     )

       例子:

  create database stuDB   on  primary  -- 默认就属于primary文件组,可省略  (  /*--数据文件的具体描述--*/      name='stuDB_data',  -- 主数据文件的逻辑名称      filename='D:\stuDB_data.mdf', -- 主数据文件的物理名称      size=5mb, --主数据文件的初始大小      maxsize=100mb, -- 主数据文件增长的最大值      filegrowth=15%--主数据文件的增长率 ) log on ( /*--日志文件的具体描述,各参数含义同上--*/     name='stuDB_log',    filename='D:\stuDB_log.ldf',    size=2mb,    filegrowth=1mb )

    2.删除数据库:

        语法:

          drop database 数据库名

        例子:

           drop database  bankDB

二.创建表:

 1.语法:

   create table 表名

  (

   列1   数据类型   列的特征,

   列2   数据类型   列的特征,

    .......

  )

  例子:

 create table student

(

studentno int  not  null,

name nvarchar(20) not  null,

age int not null

)

  2.删除表:

    语法:

    drop table 表名

    例子:

    drop table student


三.添加约束:

   1. 常用的五大约束的语法示例
     1.—-添加主键约束(将stuNo作为主键)
         alter table stuInfo
         add constraint PK_stuNo primary key (stuNo)
     2.—-添加唯一约束(身份证号唯一,因为每个人的都不一样)
         alter table stuInfo
         add constraint UQ_stuID unique(stuID)
     3.—-添加默认约束(如果地址不填 默认为“地址不详”)
        alter table stuInfo
        add constraint DF_stuAddress default (‘地址不详’) for stuAddress
     4.—-添加检查约束 (对年龄加以限定 15-40岁之间)
        alter table stuInfo
        add constraint CK_stuAge check (stuAge between 15 and 40)

        alter table stuInfo
        add constraint CK_stuSex check (stuSex=’男’ or stuSex=’女′)

     5.—-添加外键约束 (主表stuInfo和从表stuMarks建立关系,关联字段stuNo)

        alter table stuInfo
        add constraint FK_stuNo foreign key(stuNo)references stuinfo(stuNo)

        2.删除约束:

     alter table 表名 drop constraint  约束名
       例子:
   alter table student drop constraint  FK_stuNo
     




0 0
原创粉丝点击