zoukankan      html  css  js  c++  java
  • 数据表的操作

    数据表笔记

     一、范式理论

    1.第一范式(1NF)

    实体中某个属性不能有多个值或不能有重复的属性。

    2.第二范式(2NF)

     满足第一范式,而且任何一个非主键字段的数值都依赖于该数据的主键字段。

    3.第三范式(3NF)

    满足第二范式,而且该数据表中的任何两个非主键字段的数据值之间不存在函数依赖关系。比较数学计算。

     二、命令提示实用工具

    1,sqlcmd

    2.bcp

    3.dtexec

    4.dtutil

    5.osql

    6.rsconfig

    7.sqlwb

    8.tablediff

    三、SQL

    --重命名表名
    exec sp_rename 'teacher','teachers'
    --重命名数据库名
    exec sp_renamedb 'Test','Tests' --使用系统存储过程
    alter database Tests modify name=Test

    --删除表
    drop table teachers
    --删除数据库
    drop database Test

    --查看数据库状态
    exec sp_spaceused
    --查看所有数据库状态
    exec sp_helpdb Test

    --创建学生表
    create table student
    (
    st_id nchar(10) not null primary key,
    st_name nchar(10) not null,
    st_sex nchar(10) not null,
    st_age int not null,
    st_mz nchar(10) not null,
    st_tel nvarchar(50) not null
    )
    go

    --添加约束
    alter table teacher add check(te_sex in('男','女'))
    go
    --constraint ck_number,
    alter table score add constraint ck_number check (number>=0 and number<=100)
    go
    alter table teacher add constraint te_tel default(0) for te_tel
    go
    alter table score add constraint number default(0) for number
    --删除number的约束
    alter table score drop constraint ck_number
    --删除check的约束
    declare @check varchar(100);
    select @check=name from sysobjects where Parent_obj=object_id('score') and xtype='C';
    if @check is not null
    exec('alter table score drop ' + @check)
    go

    --删除约束默认值
    Declare @default varChar(256);
    Select @default=name from sysobjects where Parent_Obj=OBJECT_ID('score') and xtype='D';
    if @default is not null
    exec('Alter table score Drop '+ @default)
    go
    /*alter table score
    drop constraint df_score_number
    go*/
    --查看约束
    sp_helpconstraint score
    go

    --删除列
    alter table score
    drop column su_id
    --级联删除列????????????????????????????????????????
    alter table score
    drop column su_id cascade

    --添加列
    alter table score
    add su_id nchar(10) not null

    --增加表的列
    alter table subject
    add su_time date not null
    --加外键
    alter table score
    add constraint [fk_score_subject] foreign key (su_id) references subject (su_id)
    --删除外键
    alter table score
    drop constraint [su_id]
    alter table score
    drop constraint fk_score_subject

    --修改列类型
    alter table subject
    alter column su_id nchar(20) not null

    --创建唯一列,自增长类型
    create table super_teacher
    (
    sup_id int identity(0,1) not null ,
    sup_name nvarchar(50) unique not null,
    sup_tel nvarchar(50) unique null
    )
    --添加列为主键,前提该字段is not null,该怎样先判断是否为null????????????????????????
    alter table super_teacher
    add primary key(sup_id)

    --删除列为主键
    --在sysobjects查看表中有那些主键,xtype;注意exec('Alter table super_teacher Drop 空格'+ @Pk)
    Declare @Pk varChar(256);
    Select @Pk=Name from sysobjects where Parent_Obj=OBJECT_ID('super_teacher') and xtype='PK';
    if @Pk is not null
    exec('Alter table super_teacher Drop '+ @Pk)

    --创建用户定义数据类型,Programmability->types,用的是stored procedures
    exec sp_addtype name,'nchar(10)','null'

    --删除用户定义数据类型
    exec sp_droptype name

    --创建表时直接创建主键,外键
    create table class
    (
    cl_id nchar(10) not null,
    cl_name nchar(50) not null,
    cl_number int not null,
    te_id nchar(10) not null,
    primary key (cl_id),
    constraint fk_te_id
    foreign key (te_id) references teacher (te_id)
    )

    --创建规则
    create rule aa
    as
    @value>0

    --绑定规则
    exec sp_bindrule 'aa','class.cl_number'

    --查看规则
    exec sp_help aa
    exec sp_helptext aa

    --解除绑定-->删除规则
    exec sp_unbindrule 'class.cl_number'
    drop rule aa
    go


    --创建默认值
    create default date as getdate()
    go
    --绑定默认值
    exec sp_bindefault 'date','subject.su_time'
    go
    --查看默认
    exec sp_help date
    exec sp_helptext date
    --解除默认-->删除默认
    exec sp_unbindefault 'subject.su_time'
    drop default date
    go

  • 相关阅读:
    C#高级编程第11版
    做点字符串题
    Codeforces Round #681 (Div. 1, based on VK Cup 2019-2020
    Educational Codeforces Round 97 题解
    AtCoder Regular Contest 106 题解
    Kick Start Round G 2020 题解
    CCSP 2020题解
    Codeforces Round #675 (Div. 2) 题解
    AtCoder Regular Contest 104
    Kick Start Round F 2020 题解
  • 原文地址:https://www.cnblogs.com/fffywfn/p/4142224.html
Copyright © 2011-2022 走看看