Chinaunix首页 | 论坛 | 博客
  • 博客访问: 408845
  • 博文数量: 121
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1393
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-11 12:17
个人简介

www.vibexie.com vibexie@qq.com

文章分类

全部博文(121)

文章存档

2015年(55)

2014年(66)

我的朋友

分类: Mysql/postgreSQL

2015-04-14 11:24:38



  1. use test;
  2. show tables;

  3. /*建立一张省份表作为测试*/
  4. create table province(pid tinyint unsigned primary key auto_increment,pname varchar(20) unique key not null default "jiangxi");

  5. show index from province;
  6. show create table province;
  7. desc province;

  8. insert into province(pname) values("shanghai");
  9. insert into province(pname) values("beijing");
  10. insert into province(pname) values("fujian");

  11. delete from province where pid=2;

  12. select * from province;

  13. drop table province;


  14. /*******************************修改数据表*****************************************************/

  15. /*增加单列语法 alter table table_name add [column] col_name column_definition [first|after col_name];*/
  16. alter table province add code varchar(10) not null after pname;

  17. /*增加多列语法 alter table table_name add [column] (col_name column_definition,...);注意没有位置选项*/
  18. alter table province add (code varchar(10) not null,population_sum bigint not null);

  19. /*删除列语法 alter table table_name drop [column] column_name*/
  20. alter table province drop code;

  21. /*添加约束 alter table table_name add [constraint[symbol]] primary key [index_type] (index_col_name,...);其它约束也是一样的*/
  22. create table province(pid tinyint unsigned,pname varchar(20));
  23. alter table province add constraint pk primary key (pid);/*pk是约束的名字,一般不用*/
  24. alter table province add foreign key (pid) references XXX (XXX);/*添加外键约束也是一样的*/

  25. /*删除约束就是drop*/

  26. /*修改列定义语法:alter table table_name modifies [column] column_name column_definition [first|after col_name]*/

  27. /*修改列名称语法: alter table table_name change [column] old_col_name new_col_name column_definition [first|after col_name]*/

  28. /*修改表名称语法: 1.alter table table_name rename [to|as] new _table_name;
  29.                 2.rename table table_name to new_table_name [,table_name2 to new_table_name2...];*/
  30.                 
  31. /********************************************************************************************/



  32. /*******************************外键**********************************************************/
  33. /*注意,1.建立外键时,子表和父表必须为innoDB引擎,这是默认的引擎。可以在my.cnf中配置
  34.      2.如果外键列的字段名为数值型,则必须与参考列的字段类型和有无符号一致
  35.        3.如果外键列的字段名为字符类型,则字符的长度可以不一致。
  36.        */
  37.        
  38. /*外键约束的参照操作*/

  39. /*1.cascade :cascade翻译过来时串联的意思。父表中删除或者更新时,对应的自动删除或者更新子表中匹配的行*/
  40. create table users(id smallint primary key auto_increment,username varchar(20) not null,pid tinyint unsigned,foreign key (pid) references province(pid) on delete cascade);
  41. insert into users(username,pid) values("xiebiao",1);
  42. insert into users(username,pid) values("vibexie",2);
  43. insert into users(username,pid) values("kobe",3);
  44. select * from users;
  45. desc users;
  46. show create table users;

  47. show index from users;
  48. drop table users;

  49. /*2.set null:父表中删除或者更新,并设置子表中的外键列为NULL.如果使用set null,必须保证子表列没有指定not null*/

  50. /*3.restrict: 拒绝父表的删除或者更新操作*/

  51. /*4.no action:标准的sql关键字,在mysql中与restrict一致*/
  52. /**************************************************************************************/

阅读(1321) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~