数据库小结(一)

建表:

  create table 表名(字段名1  字段类型(长度)  约束,......);

  约束类型:

      主键:(1)primary key  (2)constraint 表名_字段名_pk   priamry key(字段名)

      不为空:(1) not null      (2)constraint 表名_字段名_nn  not null

      检查 : check

      唯一:(1)unique   (2)constraint 表名_字段名_un  unique(字段名)

      外键:constraint 表名_字段名_fk foreign key(字段名) references 参照表名(字段名)

插入:

  insert into 表名(字段名1,字段名2) values ('值1',.....);

  将表1 的部分字段取出新建表2(表2不存在),复制出的表没有主键但可以自增

  select 字段1,字段2  into 表2 from 表1;

  将表1字段中的数据插入表2(表2存在)

  insert into 表2(字段1,字段2) select value1,value2 from 表1;

更新:

  update  表名 set 列名1='值',...;

  update  表名1 set 表名1.列名1 = 表名2.列名2 from 表1,表2 where 表名1.列名3 = 表名2.列名4;

删除:

  delete from 表名 where 列名= ‘值’;

  truncate table 表名;

  truncate:不记录日志即不能从日志中恢复数据,不进行列表扫描,速度更快

注:

   select  newid();    GUID:全球唯一标识符

  

原文地址:https://www.cnblogs.com/Yida-Tingting/p/4397670.html