创建库
mysql
create database test default character set utf8mb4 collate utf8mb4_unicode_ci;
创建表
sql
CREATE TABLE `t_user` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(18) NOT NULL unique,
`password` varchar(18) NOT NULL,
PRIMARY KEY (`Id`) ,
UNIQUE INDEX (`username`)
) comment '用户表';
创建索引
mysql
-- 1.添加PRIMARY KEY(主键索引)
ALTER TABLE `table_name` ADD PRIMARY KEY (`column`) ;
-- 2.添加UNIQUE(唯一索引)
ALTER TABLE `table_name` ADD UNIQUE (`column`);
CREATE UNIQUE INDEX index_name ON `table_name` (`column`);
--
UNIQUE KEY `uk_maintenace_no` (`maintenance_no`) COMMENT '工单号唯一索引',
-- 3.添加INDEX(普通索引)
ALTER TABLE `table_name` ADD INDEX index_name (`column`);
CREATE INDEX index_name ON `table_name` (`column`);
-- 4.添加FULLTEXT(全文索引)
ALTER TABLE `table_name` ADD FULLTEXT (`column`);
-- 5.添加多列索引(组合索引)
ALTER TABLE `table_name` ADD INDEX index_name (`column1`, `column2`, `column3`);
CREATE INDEX index_name ON `table_name` (`column1`, `column2`, `column3`);
修改字段
mysql
-- 修改字段数据类型
ALTER TABLE `表名` MODIFY COLUMN `字段名` 数据类型 COMMENT '描述';
-- 向 t_user 表添加 user_age 字段
ALTER TABLE `t_user` ADD COLUMN `user_age` int(11) DEFAULT NULL COMMENT '年龄' AFTER user_email;
-- 修改字段名称
ALTER TABLE `<表名>` CHANGE `<字段名>` <字段新名称> <字段的类型> COMMENT '描述';
-- 删除某个字段
ALTER TABLE `tableName` DROP COLUMN `columeName`;
时间函数 DATE_SUB()
函数从日期减去指定的时间间隔。
mysql
-- 当年第一天到现在
BETWEEN (SELECT DATE_SUB(CURDATE(),INTERVAL dayofyear(now())-1 DAY)) AND NOW()
-- 上一年第一天到现在
BETWEEN (SELECT DATE_SUB((SELECT DATE_SUB(CURDATE(),INTERVAL DAYOFYEAR(now())-1 DAY)),INTERVAL 1 YEAR)) AND (SELECT DATE_SUB(NOW(),INTERVAL 1 YEAR))
-- 本月
BETWEEN (select DATE_ADD(CURDATE(),interval -(select day(NOW()))+1 DAY)) AND NOW();
-- 上月
BETWEEN (SELECT DATE_ADD(curdate()-day(curdate())+1,interval -1 MONTH)) AND (SELECT DATE_SUB(NOW(),INTERVAL 1 MONTH));
CURDATE()
-- 精确到天
NOW()
-- 精确到时分秒
-- 格式化时间
DATE_FORMAT(now(), '%Y-%m-%d %H:%i:%S');
-- 当月最后一天
select last_day(now())
修改表名称
mysql
alter table `oldTableName` rename to `newTableName`;
[修改表默认字符集][https://www.notion.so/MySQL-5f065ae95f904351a1635de3b82fda5b\]
mysql
ALTER TABLE tbl_name DEFAULT CHARACTER SET character_name [COLLATE...];
如:ALTER TABLE logtest DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
查询表占用空间
mysql
select
table_schema,
sum(data_length)/ 1024 / 1024 as data_length,
sum(index_length)/ 1024 / 1024 as index_length,
sum(data_length + index_length)/ 1024 / 1024 as sum
from
information_schema.tables
where
table_schema = 'midea_gls'
and table_name = 'go_shipment_notice';
select
concat(truncate(sum(data_length)/ 1024 / 1024, 2), 'mb') as data_size,
concat(truncate(sum(max_data_length)/ 1024 / 1024, 2), 'mb') as max_data_size,
concat(truncate(sum(data_free)/ 1024 / 1024, 2), 'mb') as data_free,
concat(truncate(sum(index_length)/ 1024 / 1024, 2), 'mb') as index_size
from
information_schema.tables
where
table_schema = 'midea_gls'
and table_name = 'go_shipment_notice';
比较函数
GREATEST
和LEAST
函数都使用N个参数,并分别返回最大和最小值。下面说明GREATEST和LEAST函数的语法:
mysql
GREATEST(value1, value2, ...);
LEAST(value1,value2,...);
值为null就会导致不准确,null是最小的,可以加上ifnull()
例如LEAST(ifNUll(gtyr.engage_date,'Y'),ifNUll(gtyr.dc_whouse_time,'Y'))
或者COALESCE()
返回第一个不为空的
添加唯一约束
1.建表时加上唯一性约束:
mysql
CREATE TABLE `t_user` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(18) NOT NULL unique,
`password` varchar(18) NOT NULL,
PRIMARY KEY (`Id`)
) comment '用户表';
2.给已经建好的表加上唯一性约束:
mysql
ALTER TABLE `t_user` ADD unique(`username`);
或者:
mysql
create unique index uni_username on `t_user` (`username`);
mysql建表&索引语句.md