您的位置:首页 > 数据库 > MySQL

MySQL常用操作语句

2010-07-09 21:39 411 查看
查看innodb下的ddl语句错误信息

SHOW ENGINE INNODB STATUS

查看见表语句

show create table [table name]

增加主键,主键可以是一列,也可以是多列组合,但一个表中只能有一个主键。

alter table [tablename] add constraint [constrain name] primary key ([column1]{column2,...})

增加外键

alter table[tablename] add constraint [constraint name] foreign key([column]) reference [anthoer tablename][column]

删除主键约束

alter table[table name] drop contraint [constraint name]

修改列属性

alter table [table name] modify([ column][字段类型][是否为空])

添加字段:

alter table `user_movement_log`
Add column GatewayId int not null default 0 AFTER `Regionid` (在哪个字段后面添加)

删除字段:

alter table `user_movement_log` drop column Gatewayid

调整字段顺序:

ALTER TABLE `user_movement_log` CHANGE `GatewayId` `GatewayId` int not null default 0 AFTER RegionID

//主键

alter table tabelname add new_field_id int(5) unsigned default 0 not null auto_increment ,add primary key (new_field_id);//增加一个新列

alter table t2 add d timestamp;
alter table infos add ex tinyint not null default ‘0′;//删除列

alter table t2 drop column c;//重命名列

alter table t1 change a b integer;
//改变列的类型

alter table t1 change b b bigint not null;
alter table infos change list list tinyint not null default ‘0′;
//重命名表

alter table t1 rename t2;加索引

mysql> alter table tablename change depno depno int(5) not null;
mysql> alter table tablename add index 索引名 (字段名1[,字段名2 …]);
mysql> alter table tablename add index emp_name (name);加主关键字的索引

mysql> alter table tablename add primary key(id);加唯一限制条件的索引

mysql> alter table tablename add unique emp_name2(cardnumber);删除某个索引

mysql>alter table tablename drop index emp_name;修改表:

增加字段:

mysql> ALTER TABLE table_name ADD field_name field_type;修改原字段名称及类型:

mysql> ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;删除字段:

mysql> ALTER TABLE table_name DROP field_name;


1、创建表格时添加: create table table1(id int auto_increment primary key,…)

2、创建表格后添加: alter table table1 add id int auto_increment primary key 自增字段,一定要设置为primary key.

附:mysql 中的alter table mysql> alter table employee change depno depno int(5) not null;

加索引 mysql> alter table 表名 add index 索引名 (字段名1[,字段名2 …]);

例子: mysql> alter table employee add index emp_name (name);

加主关键字的索引 mysql> alter table 表名 add primary key (字段名);

例子: mysql> alter table employee add primary key(id);

加唯一限制条件的索引 mysql> alter table 表名 add unique 索引名 (字段名);

例子: mysql> alter table employee add unique emp_name2(cardnumber);

查看某个表的索引 mysql> show index from 表名; 例子: mysql> show index from employee;

删除某个索引 mysql> alter table 表名 drop index 索引名; 例子: mysql>alter table employee drop index emp_name;

修改表:增加字段:mysql> ALTER TABLE table_name ADD field_name field_type;

查看表:mysql> SELECT * FROM table_name;

修改原字段名称及类型:mysql> ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;

删除字段:ALTER TABLE table_name DROP field_name

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: