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中选择级联

  • 相关阅读:
    文件过滤驱动隐藏目标文件
    POJ 3345 Bribing FIPA(树形DP)
    POJ 1018 Communication System(分组背包DP)
    无用的,ring0暴力枚举进程模块
    HDOJ 3496 Watch The Movie(基本二维背包)
    栈回溯法的一个例子
    代码这样写奇丑无比...编码前期要做好规划工作啊
    多核发dpc安全inline hook
    纵我不往,知识不来学习Java第一周心得
    对“TD信息树”的使用体验
  • 原文地址:https://www.cnblogs.com/TuringShine/p/7843565.html
Copyright © 2011-2022 走看看