zoukankan      html  css  js  c++  java
  • 数据库定义语言

    DDL(Data Description Language),是用于描述数据库中要存储的现实世界实体的语言。一个数据库模式包含该数据库中所有实体的描述定义。这些定义包括结构定义、操作方法定义等。
    DDL描述的模式,必须由计算机软件进行编译,转换为便于计算机存储、查询和操纵的格式,完成这个转换工作的程序称为模式编译器。
    基本对象 操 作
    创建 修改 删除
    数据库 create database   drop database
    表 create table alter table drop table
    视图 create view   drop view
    索引 reate index   drop index

     
    一、数据库
    1 创建数据库语法:
    create database 数据库名
     on [primary]
     {
        <数据库文件参数>
     }
    [log on]
    {
        <日志文件参数>
    }

    文件的具体参数的语法:
    [name=逻辑文件名]
    [filename=物理文件名]
    [size=初始容量]
    [maxsize=最大容量 | unlimited]
    [filegrowth=增长量]
    各参数说明如下:
    1 primary:指定主文件组中的文件。
    2 log on:指明事务日志文件的明确定义。
    3 name:指定数据库的逻辑名称,这是在sql servler系统中中使用的名称。
    4 filename :指定数据库文件.mdf或.ndf的名称和路径。
    5 size:数据库初始容量。
    6 maxsize:数据库最大容量。
    7 filegrowth:指定文件每次增加容量的大小,当指定为0时,表示文件不增长。可以按百分比增长、数值增长。
     
    示例1:一个数据库文件和一个日志文件
      use master
    execute xp_cmdshell 'mkdir d:stu',no_output
    go
    /**//*建立数据库stuDB*/
    if exists(select * from sysdatabases where name='stuDB')
        drop database stuDB
    create database stuDB
    on primary
    (
        name='stuDB_data',
        filename='d:stustuDB_data.mdf',
        size=3mb,
        maxsize=100mb,
        filegrowth=2%
    )
    log on
    (
        name='stuDB_log',
        filename='d:stustuDB_log.ldf',
        size=1mb,
        maxsize=50mb,
        filegrowth=1
    )
     
    示例2:多个数据库文件和多个日志文件
    use master
    execute xp_cmdshell 'mkdir d:shop',no_output
    go

    --建立数据库shopDB--
    if exists(select * from sysdatabases where name='shopDB')
        drop database shopDB
    create database shopDB
    on primary
    (
        /**//*--主数据库文件的具体描述--*/
        name='shopDB_data1',                  
        filename='d:shopshopDB_data1.mdf',       
        size=6mb,                    
        maxsize=800mb,                    
        filegrowth=15%                   
    ),/**//*--一定要加逗号--*/
    (
        /**//*--次要数据库文件的具体描述--*/
        name='shopDB_data2',                  
        filename='d:shopshopDB_data2.ndf',       
        size=6mb,                    
        maxsize=800mb,                    
        filegrowth=1                   
    )
    log on
    (
        /**//*--日志文件的具体描述--*/
        name='shopDB_log1',
        filename='d:shopshopDB_log1.ldf',
        size=2mb,
        filegrowth=1mb
    )
    log on
    (
        /**//*--日志文件的具体描述--*/
        name='shopDB_log2',
        filename='d:shopshopDB_log2.ldf',
        size=2mb,
        filegrowth=1mb
    )

    go

     

    2 删除数据库
    语法:
    drop database 数据库名
    如:
    user master --设置当前数据库为master,以便访问sysdatabases表--
    go

    if exists(select * from sysdatabases where name='stuDB')
       drop database stuDB

     

     二、表
    1 创建表
    语法:
    create table 表名
    (
        字段名1,数据类型,列的特征,
        字段名1,数据类型,列的特征,
            ...
    )
     
    示例:创建表stuInfo
    use stuDB
    go
    if exists(select * from sysobjects where name=stuInfo')
        drop table stuInfo'
    create table stuInfo
    (
        stuName varchar(20) not null,
        stuNo char(6) not null,
        stuAge int not null,
        stuId numeric(18,0),
        stuSeat smallint identity(1,1),
        stuAddress text
    )
    go

     
     
    2 修改表
    (1) 添加字段
    alter table 表名
    [add 字段名,数据类型,列的特征]
    如:
    alter table stuInfo
    add tel varchar(20) not null
     
    (2)添加约束
    alter table 表名
    add constraint 约束名约束类型具体约束说明
    如:
    alter table stuInfo add
        constraint PK_stuNo primary key(stuNo),        --主键约束--
           constraint UQ_stuID unique(stuID),            --唯一约束--
        constraint DF_stuAddress default('地址不详') for stuAddress,    --默认约束--
        constraint CK_stuAge check(stuAge between 15 and 40),        --检查约束--
        constraint FK_stuNo foreign key(stuNo) references stuInfo(stuNo) --参照约束--


     
    (3)删除约束
    alter table 表名
        drop constraint 约束名
    如:
    alter table stuInfo
        drop constraint PK_stuNo

     

     
     
    3删除表
    drop table 表名
    if exists(select * from sysobjects where name='stuInfo')
        drop table stuInfo

     

    三、视图
    1 创建视图
    语法:
    create view 视图名
    [with encryption]
    as
        SQL语句体
    其中:
    with encryption:表示对视图文本加密
    /**//*建立视图*/
    if exists(select * from sysobjects where name='stuInfo_view')
        drop view stuInfo_view
    create view stuInfo_view
    with encryption
    as
        select * from stuInfo

    /**//*查询视图*/
    select * from stuInfo_view

     
    2 删除视图
     drop view 视图名   drop view 视图名
    if exists(select * from sysobjects where name='stuInfo_view')
        drop view stuInfo_view

     
     
     
    四、索引
    索引:是SQL Server编排数据的内部方法.
    索引页:数据库中存储索引的数据页.索引页存放检索数据行的关键字及该数据行的地址指针.索引页类似于汉语字典中按拼音或笔画排序的目录页.
     
    索引作用:提高数据库检索速度,改善数据库性能.
     
    1 创建索引:
    语法:
    create [unique][clustered|nonclustered] index 索引名
        on 表名(列名[asc|desc])
        [with fillfactor=x]
    其中:
    unique:指定唯一索引
    clustered nonclustered:指定是聚集还是非聚集索引,一个表只能创建一个聚集索引,但可以有多个非聚集索引,设置某列为主键,则此列默认为聚集索引.
    fillfactor:填充因子,指定一个-100的值,该值指示索引页填满的空间所占的百分比.

    use stuDB
    go
    if exists(select name from sysindexes where name='stuInfo_index')
        drop index stuInfo.stuInfo_index
    create nonclustered index stuInfo_index
        on stuInfo(stuNo desc)
        with fillfactor=30
    go

    select * from stuInfo
        stuInfo_index where stuAge>18


     
     
    2 删除索引
     drop index 表名.索引名
    if exists(select name form sysindexes where name='stuInfo_index')
        drop index stuInfo.stuInfo_index

  • 相关阅读:
    36、基于TCP、UDP协议的嵌套字通信
    34、异常以及网络编程
    作业4月15号
    31、反射与内置方法、元类
    30、多态与鸭子类型以及内置函数
    作业4月9号
    29、继承
    作业4月8号
    28、封装
    27、面向对象
  • 原文地址:https://www.cnblogs.com/nygfcn1234/p/3401973.html
Copyright © 2011-2022 走看看