MySQL 常用命令

1、添加外键
mysql> alter table teacher add constraint fk_class_id Foreign Key(class_id) Refe
rences classes(class_id) on delete cascade;

外键的名字不能叫FK;
如果要在父表中更新或者删除一行,并且在子表中也有一行或者多行匹配,此时子表的操作有5个选择:
(1)CASCADE: 从父表删除或更新且自动删除或更新子表中匹配的行。ON DELETE CASCADE和ON UPDATE CASCADE都可用。在两个表之间,你不应定义若干在父表或子表中的同一列采取动作的ON UPDATE CASCADE子句。
(2)SET NULL: 从父表删除或更新行,并设置子表中的外键列为NULL。如果外键列没有指定NOT NULL限定词,这就是唯一合法的。ON DELETE SET NULL和ON UPDATE SET NULL子句被支持。
(3)NO ACTION: 在ANSI SQL-92标准中,NO ACTION意味这不采取动作,就是如果有一个相关的外键值在被参考的表里,删除或更新主要键值的企图不被允许进行(Gruber, 掌握SQL, 2000:181)。 InnoDB拒绝对父表的删除或更新操作。
(4)RESTRICT: 拒绝对父表的删除或更新操作。NO ACTION和RESTRICT都一样,删除ON DELETE或ON UPDATE子句。(一些数据库系统有延期检查,并且NO ACTION是一个延期检查。在MySQL中,外键约束是被立即检查的,所以NO ACTION和RESTRICT是同样的)。
(5)SET DEFAULT: 这个动作被解析程序识别,但InnoDB拒绝包含ON DELETE SET DEFAULT或ON UPDATE SET DEFAULT子句的表定义。

MySQL添加外键时 error 150 问题
参考资料 http://www.cnblogs.com/youk/archive/2011/11/13/2247054.html
2、alter命令
测试用的数据库创建语句如下:
mysql> create table demo_alter(
    -> id int unsigned not null auto_increment,
    -> name varchar(20) not null,
    -> primary key(id)
    -> )engine = InnoDB;

查看表结构
mysql> desc demo_alter;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20)      | NO   |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

添加一个新的字段age,默认添加在最后面
mysql> alter table demo_alter add age int not null;
Query OK, 0 rows affected (0.14 sec)
Records: 0  Duplicates: 0  Warnings: 0

在name字段后添加一个新的字段pass,需要用到after关键字

mysql> alter table demo_alter add pass varchar(20) after name;
Query OK, 0 rows affected (0.11 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc demo_alter;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20)      | NO   |     | NULL    |                |
| pass  | varchar(20)      | YES  |     | NULL    |                |
| age   | int(11)          | NO   |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)


在字段的上面添加一个新的字段。需要关键字 first
alter table demo_alter add id int(10) not null auto_increment primary  key not null first ;


删除表结构
mysql> alter table demo_alter drop date ;

修改字段类型modify
mysql> desc demo_alter;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20)      | NO   |     | NULL    |                |
| pass  | varchar(20)      | YES  |     | NULL    |                |
| age   | int(11)          | NO   |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> alter table demo_alter modify age int(10) ;
Query OK, 0 rows affected (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc demo_alter;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20)      | NO   |     | NULL    |                |
| pass  | varchar(20)      | YES  |     | NULL    |                |
| age   | int(10)          | YES  |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+

修改字段名change
mysql> desc demo_alter;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20)      | NO   |     | NULL    |                |
| pass  | varchar(20)      | YES  |     | NULL    |                |
| age   | int(10)          | YES  |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> alter table demo_alter change age AGE int(10);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc demo_alter;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20)      | NO   |     | NULL    |                |
| pass  | varchar(20)      | YES  |     | NULL    |                |
| AGE   | int(10)          | YES  |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
4 rows in set (0.02 sec)

你可能感兴趣的:(mysql)