zoukankan      html  css  js  c++  java
  • SQL课堂笔记管理表

          2017.11.09

    1.使用T-SQL语句显示表的信息,调用系统的存储过程
     sp_help (student)表名
    2.修改表的结构
     增加列:
      alter table 表名
      add 列名 列的描述
     删除列:
      alter table 列名
      drop column 列名
     重命名表名:
      use 数据库名
      go
      sp_rename 'old_table name','new_table name',('object')可写可不写

     注释:
      单行注释 --
      多行注释/*     */

    3.SQL server约束机制
    可以通过create table语句在建表的时候添加约束,也可以用alter table语句来为已经存在的表添加约束
    SQL server2008中的约束主要有:
        主键 primary
        外键 foreign key
        默认值 deflaut
        检查check
     
     主键:

      使用alter table添加主键:
       alter table 表名
       add constraint 约束名
       primary key (列名[,...])
       
       约束名:为约束指定的名称
       列名: 表示创建primary key 约束所依据的列
     eg:
      use kecheng
      go
      alter table 课程
      add constraint pk_学号
      primary key(学号)

     外键 :
      1. create table grade
       (studentid char(10) foreign key references student(syudentid)
       courseid char(8) not null,
       term nvarchar(20),
       grade tinyint check (grade between 0 and 100)
       go
      2.
       primary key (studentid ,cno),
       foreign key (studentid) references student(studentid)
      语法:
       alter table 表名1
       add constraint 约束名
       foreign key (列名1)
       references 关联的表的列名(字段名)


      eg:
       use 机票预定信息管理系统
       go
       alter table 订票表
       add constraint FK_员工编号
       foreign key (员工编号) references 员工表(员工编号)


         设置的当前表为外键表,设为外键的是某某表的主键      cpno先行课


     设置默认值:
       在创建时,添加约束default
       alter table student
       add constraint sex default '男' for sex


     where子句:
      between  ... and  ...    
      not between ... and ....
      between  60 and 100包括60也包括100

     check约束:
      在创建时添加
      alter table 表名
      add constraint 约束名
      check(表达式)
      
      删除约束:
       alter table 表名
       drop constraint 约束名

    设置外键后如何设置级联删除:
     外键中的inset and update中选择级联

  • 相关阅读:
    mac上python3安装HTMLTestRunner
    双目深度估计传统算法流程及OpenCV的编译注意事项
    深度学习梯度反向传播出现Nan值的原因归类
    1394. Find Lucky Integer in an Array
    1399. Count Largest Group
    1200. Minimum Absolute Difference
    999. Available Captures for Rook
    509. Fibonacci Number
    1160. Find Words That Can Be Formed by Characters
    1122. Relative Sort Array
  • 原文地址:https://www.cnblogs.com/TuringShine/p/7843565.html
Copyright © 2011-2022 走看看