mysql标准创建表及操作索引

 1.建表语句

CREATE TABLE `user` (
  `id` bigint(11) NOT NULL AUTO_INCREMENT,
  `user_id` varchar(20) NOT NULL COMMENT '用户编号',
  `user_name` varchar(40) NOT NULL COMMENT '用户名称',
  `sex` varchar(1) NOT NULL DEFAULT '0' COMMENT '性别,不写默认0',
  `email` varchar(40) NOT NULL COMMENT '邮箱',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  `del_ind` varchar(1) NOT NULL COMMENT '删除标记,0未删除,1已删除',
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_user_id` (`user_id`),
  KEY `idx_user_name` (`user_name`)
) ENGINE=InnoDB AUTO_INCREMENT=45 DEFAULT CHARSET=utf8 COMMENT='用户信息表';

 2.表新增语句

insert into user(user_id,user_name,email,del_ind) values('1qa2sw3ed','张三','[email protected]','0');

3.删除表唯一或者普通索引

alter table user drop index idx_user_id;
alter table user drop index idx_user_name;

4.新增表唯一索引或者普通索引

alter table user add unique `idx_user_id` (`user_id`);
alter table user add KEY `idx_user_name` (`user_name`)

你可能感兴趣的:(sql,mysql)