MySQL 修改、增加、删除字段 增加删除索引操作

版权声明:本文为博主原创文章,转载请注明出处 https://blog.csdn.net/vkingnew/article/details/89029756
create table user(
userid bigint unsigned,
username varchar(20),
password varchar(128),
gender char(1),
mobile varchar(11),
city_id int,
city_name varchar(30),
state tinyint,
logintime datetime not null DEFAULT CURRENT_TIMESTAMP,
birthday date,
amount decimal(22,6),
is_online bit,
LastModifyTime datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) COMMENT '末次更新时间');

--添加记录:
insert into user(userid,username,password,gender,mobile,city_id,city_name,state,birthday,amount,is_online)values
 (1,'xiaoyue',md5('china'),'F','13419534220',100,'wuhan',1,'19901231',667788.99,1);
--查询记录:
mysql> select * from user;
+--------+----------+----------------------------------+--------+-------------+---------+-----------+-------+---------------------+------------+---------------+-----------+----------------------------+
| userid | username | password                         | gender | mobile      | city_id | city_name | state | logintime           | birthday   | amount        | is_online | LastModifyTime             |
+--------+----------+----------------------------------+--------+-------------+---------+-----------+-------+---------------------+------------+---------------+-----------+----------------------------+
|      1 | xiaoyue  | 8a7d7ba288ca0f0ea1ecf975b026e8e1 | F      | 13419534220 |     100 | wuhan     |     1 | 2019-04-04 16:36:11 | 1990-12-31 | 667788.990000 |          | 2019-04-04 16:36:11.754098 |
+--------+----------+----------------------------------+--------+-------------+---------+-----------+-------+---------------------+------------+---------------+-----------+----------------------------+
1 row in set (0.00 sec)

alter table user add ID bigint unsigned default 0 not null auto_increment ,add primary key (ID);

alter table user add ID bigint unsigned  not null auto_increment ,add primary key (ID);
--删除列:
alter table user drop column id;
--修改列为主键:
alter table user modify userid int auto_increment primary key;
--增加字段:
 alter table user add column bizuserlevel tinyint not null default 0;
--设置字段的默认值: 
 alter table user alter column bizuserlevel set default 0;
--将NULL修改为not null:
 mysql> alter table user modify gender char not null;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0
--调整字段位置:
alter table user Add column email varchar(20)  not null default '' AFTER mobile;
 -- ALTER TABLE user  CHANGE email email varchar(30)  null default ''  before mobile;
--修改字段名称和类型:
ALTER TABLE user CHANGE  city_name location_city varchar(50) DEFAULT ''; 
-- 修改字段类型:
 alter table user change amount orderAmount decimal(24,6) default 0;
 
 -- 索引操作:
 ALTER TABLE user ADD PRIMARY KEY (userid);
 
 --添加唯一索引:
 alter table user add UNIQUE(mobile);
 
 --添加普通索引:
 ALTER TABLE user ADD INDEX ix_lastmodifytime(lastmodifytime );
 --添加组合索引:
  ALTER TABLE user ADD INDEX ix_state_cityid(state,city_id );
 --添加全文索引:
 ALTER TABLE user ADD FULLTEXT(username);
 
 --删除主键:(若这个主键是自增的,先取消自增长)
 mysql> alter table user drop primary key;
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
mysql> alter table user modify userid bigint;
Query OK, 1 row affected (0.14 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> alter table user drop primary key;
Query OK, 1 row affected (0.14 sec)
Records: 1  Duplicates: 0  Warnings: 0

猜你喜欢

转载自blog.csdn.net/vkingnew/article/details/89029756