SQL-DDL语句


DDL语句(操作数据库对象[table,constraint,view,index,function,procedure,trigger]的语句)
   创建表:
      create table 表名(columnName datatype[default expr]);
      create table test(test_id int,test_price decimal,test_name varchar(255) default'xxx',test_desc text,test_img blob,test_date datetime);
   子查询建表语句
      create table 表名 as 旧表名;
      create table test1 as select * from test;
   修改表结构
      alter table 表名 add 列名 列定义;(增加列)
      alter table test add test_aaa varchar(255) default'xxx';
      alter table 表名 modify 列名 列定义;(修改列定义)
      alter table test modify test_aaa varchar(10) default'yyy';
      alter table 表名 drop 列名;(删除列)
      alter table test drop test_aaa;
      alter table 表名 change 旧列名 新列名 列定义;(修改列名)
      alter table test change test_aaa test_bbb int(10);
      alter table 表名 rename to 新表名;(重命名表)
      alter table test rename to test1;
      drop table 表名;(删除表)
      drop table test;
   数据库约束
      not null 非空约束,指定某列不能为空
          create table hehe (hehe_id int not null,hehe_name varchar(255) default'xyz'not null,hehe_gender varchar(2) null);
          alter table hehe modify hehe_gender varchar(2) not null;(增加非空约束)
          alter table hehe modify hehe_name varchar(2) null;(取消非空约束)
      unique  唯一约束, 指定某列或者几列组合不能重复
          create table unique_test(test_id int not null,test_name varchar(255) unique);
          constraint 约束名 unique (被约束列)(表级约束语法,表级约束是指两列组合不允许重复)
          create table unique_test3(test_id int not null,test_name varchar(255),test_pass varchar(255),constraint test3_uk unique(test_name,test_pass));        
          alter table 表名 drop index 表名||约束名(删除约束)
          alter table unique_test3 drop index test3_uk;
          alter table unique_test drop index test_name;
      primary key 主键约束,指定该列的值可以唯一的标识该条记录
          create table primary_test(test_id int primary key,test_name varchar(255));
          primary key (被约束列)(以多列建立组合主键,只能使用表级约束语法,MySQL主键约束名不会变primary)
          create table primary_test3(test_name varchar(255),test_pass varchar(255),primary key(test_name,test_pass));
          alter table 表名 drop primary key;(删除主键约束)
          alter table primary_test3 drop primary key;
          alter table 表名 add primary key(列名,列名);(表级约束语法增加主键约束)
          alter table primary_test3 add primary key(test_name,test_pass);
          alter table 表名 modify 列名 列类型 primary key;
          alter table primary_test3 modify test_name varchar(255)primary key;
          建立主键约束,使用自增长
          create table primary_test4(test_id int auto_increment primary key,test_name varchar(255),test_pass varchar(255));
      foreign key(外键约束)
          列名 列类型 references 主表名(主表列名)(列级约束语法,对Mysql不生效)
          foreign key(列名) references 主表名(主表列名)(表级约束语法)
          create table teacher_table(teacher_id int auto_increment,teacher_name varchar(255),primary key(teacher_id));
          create table student_table(student_id int auto_increment primary key,student_name varchar(255),java_teacher int,foreign key(java_teacher) references teacher_table(teacher_id));
          alter table 表名 drop foreign key 约束名(约束名一般为外表列名_ibfk_n)(删除外键约束)
          alter table 表名 add foreign key(列名,列名)references 主表名(主列名,主列名);(增加外键约束)
          on delete cascade (删除主表记录时,把参照主表记录的从表记录全部删除)
          on delete set null(删除主表记录时,把参照主表记录的从表记录的外键设为null)
      check 条件约束(在MySQL中不起作用,所有不多做解释)
          create table check_test(emp_id int auto_increment,emp_name varchar(255),emp_salary decimal,primary key(emp_id),check(emp_salary>0));
   索引
      create index 索引名 on 表名 (表列名);(创建索引)
      drop index 索引名 on 表名 ;(删除索引)
   视图
      create or replace view 视图名 as select 属性 from 表名 (创建视图)
      create or replace view view_test as select * from teacher_table;
      with check option;指定不允许修改视图的数据
      drop view 视图名; (删除视图)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值