当前位置:Gxlcms > 数据库问题 > mysql表操作之修改

mysql表操作之修改

时间:2021-07-01 10:21:17 帮助过:12人阅读

. 修改存储引擎 mysql> alter table t1 -> engine=innodb; 2. 添加字段 mysql> alter table t1 -> add name varchar(20) not null, -> add age int(3) not null default 22; mysql> alter table t1 -> add stu_num varchar(10) not null after name; //添加name字段之后 mysql> alter table t1 -> add sex enum(‘male‘,‘female‘) default ‘male‘ first; //添加到最前面 3. 删除字段 mysql> alter table t1 -> drop sex; mysql> alter table t1 -> drop mac; 4. 修改字段类型modify mysql> alter table t1 -> modify age int(3); mysql> alter table t1 -> modify id int(11) not null primary key auto_increment; //修改为主键 5. 增加约束(针对已有的主键增加auto_increment) mysql> alter table t1 modify id int(11) not null primary key auto_increment; ERROR 1068 (42000): Multiple primary key defined mysql> alter table t1 modify id int(11) not null auto_increment; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 6. 对已经存在的表增加复合主键 mysql> alter table service2 -> add primary key(host_ip,port); 7. 增加主键 mysql> alter table student1 -> modify name varchar(10) not null primary key; 8. 增加主键和自动增长 mysql> alter table student1 -> modify id int not null primary key auto_increment; 9. 删除主键 a. 删除自增约束 mysql> alter table student10 modify id int(11) not null; b. 删除主键 mysql> alter table student10 -> drop primary key; 示例 header 1 | header 2 ---|--- row 1 col 1 | row 1 col 2 row 2 col 1 | row 2 col 2

二、复制表

复制表结构+记录 (key不会复制: 主键、外键和索引)
mysql> create table new_service select * from service;

只复制表结构
mysql> select * from service where 1=2;        //条件为假,查不到任何记录
Empty set (0.00 sec)

mysql> create table new1_service select * from service where 1=2;  
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> create table t4 like employees;

mysql表操作之修改

标签:records   where   mysq   表结构   dup   删除   ade   数据类型   change   

人气教程排行