MySQL常用SQL整理

MySQL常用SQL整理

添加字段

alter table `user` add `is_super` tinyint(1) unsigned not null default '0' after `is_sys` ;
alter table `article` add `url` varchar(200) not null default '' after `source` ;

修改字段名称及类型

alter table `article` change `isshow` `is_show` tinyint(1) unsigned not null default '0' ;

修改字段类型

alter table `article` modify `title` varchar(200) not null default '' ;

删除字段

alter table `article` drop column isshow,drop column gid,drop column pid ;

创建索引

CREATE UNIQUE INDEX category_code USING BTREE ON `article` (`categoryid`,`code`);
alter table `article` add unique category_code (`categoryid`,`code`) USING BTREE;

创建索引

alter table table_name add index index_name (column_list) ;
alter table table_name add unique index_name (column_list) ;
alter table table_name add primary key (id) ;

删除索引

drop index index_name on table_name ;
alter table table_name drop index index_name ;
alter table table_name drop primary key ;

删表

drop table `user` ;

清空表

truncate table `user` ;

mysql重命名表

alter table `article` rename `article1` ;
alter table `article` rename as `article1` ;
alter table `article` rename to `article1` ;
rename table `article` to `article1` ;

创建表

CREATE TABLE `uc_user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `citycode` varchar(20) NOT NULL DEFAULT '' COMMENT '城市',
  `username` varchar(100) NOT NULL DEFAULT '',
  `password` char(32) NOT NULL DEFAULT '',
  `status` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `nickname` varchar(100) NOT NULL DEFAULT '',
  `wcaid` varchar(20) NOT NULL DEFAULT '',
  `lastlogintime` bigint(10) unsigned NOT NULL DEFAULT '0',
  `lastloginip` varchar(20) NOT NULL DEFAULT '',
  `createtime` bigint(10) unsigned NOT NULL DEFAULT '0',
  `updatetime` bigint(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uni_username` (`username`) USING BTREE,
  UNIQUE KEY `uni_citycode_wcaid` (`citycode`,`wcaid`) USING BTREE,
  KEY `idx_username_password_status` (`username`,`password`,`status`),
  KEY `status` (`status`)
) ENGINE=MyISAM AUTO_INCREMENT=1001 DEFAULT CHARSET=utf8 COMMENT='用户表' ;
CREATE TABLE `adslots` (
  `id` varchar(50) NOT NULL,
  `alias` varchar(50) NOT NULL,
  `name` varchar(100) NOT NULL,
  `description` varchar(1000) NOT NULL,
  `price` bigint(20) NOT NULL,
  `width` bigint(20) NOT NULL,
  `height` bigint(20) NOT NULL,
  `num_slots` bigint(20) NOT NULL,
  `num_auto_fill` bigint(20) NOT NULL,
  `auto_fill` text NOT NULL,
  `created_at` bigint(20) NOT NULL,
  `updated_at` bigint(20) NOT NULL,
  `version` bigint(20) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uni_adslot_alias` (`alias`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ;

插入

insert into user (username,status) value ('u1',1) ;

批量插入

insert into user (username,status) values ('u1',1),('u2',1),('u3',1) ;
insert into stat_ip (source,citycode,tid,ip) values (2,'bj','10086','192.168.101.4'),(2,'bj','10086','192.168.101.2'),(2,'bj','10086','192.168.101.5') on duplicate key update source=values(source),citycode=values(citycode),tid=values(tid),ip=values(ip);

insert into stat_ip (source,citycode,tid,ip,ctime,num) values (2,'bj','10086','192.168.101.7',123456789,1),(2,'bj','10086','192.168.101.7',123456789,1),(2,'bj','10086','192.168.101.7',123456789,1) on duplicate key update num=num+1;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值