版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liangdeniu/article/details/80225791
建表命令:
CREATE TABLE `t_sold_order` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`dt` date DEFAULT NULL COMMENT '日期',
`hour` tinyint(2) DEFAULT '0' COMMENT '小时',
`hour_order` int(11) DEFAULT '0' COMMENT '小时订单数',
`total_order` int(11) DEFAULT '0' COMMENT '总的订单数',
`prediction` int(11) DEFAULT '0' COMMENT '预测订单数',
PRIMARY KEY (`id`),
UNIQUE KEY `dt_hour` (`dt`,`hour`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='实时订单数'
表操作命令:
复制表结构:create table table1 like table;
复制数据:insert into table1 select * from table
机器授权:
grant select on *.* to 'reader'@'%' identified by '123456' WITH GRANT OPTION
flush privileges
查询数据直接插入
insert into t_visual_user_domain(`user_id`,`domain`,`group`) select id,'www.baidu.com' as domain,`group` from t_visual_user;
修改表结构
CREATE TABLE `t_sold_order` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`dt` date DEFAULT NULL COMMENT '日期',
`hour` tinyint(2) DEFAULT '0' COMMENT '小时',
`hour_order` int(11) DEFAULT '0' COMMENT '小时订单数',
`total_order` int(11) DEFAULT '0' COMMENT '总的订单数',
`prediction` int(11) DEFAULT '0' COMMENT '预测订单数',
PRIMARY KEY (`id`),
UNIQUE KEY `dt_hour` (`dt`,`hour`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='实时订单数'
表操作命令:
复制表结构:create table table1 like table;
复制数据:insert into table1 select * from table
机器授权:
grant select on *.* to 'reader'@'%' identified by '123456' WITH GRANT OPTION
flush privileges
查询数据直接插入
insert into t_visual_user_domain(`user_id`,`domain`,`group`) select id,'www.baidu.com' as domain,`group` from t_visual_user;
修改表结构
alter table competitor_goods add sku_id bigint(20) unsigned DEFAULT NULL COMMENT '商品销售码';
MySQL 排序
SELECT * from runoob_tbl ORDER BY submission_date ASC;
补充:默认是升序,Desc是降序
普通索引的创建方式
create index index on tablenmame(i(10))、
添加索引
alert table tablename add index indexname(columName)
drop index indexName on table
唯一索引
创建索引
create unique index indexnname on tableName(username(length))
另一种模式:
alert table mytable add unique index indexname(username(length))
创建表的时候直接指定
unique indexname (username(length))
使用ALTER 命令添加和删除索引
alert tabel tablename add index(C)
alert table tablename drop index(C)
显示索引信息
show index from table;
获取服务器元数据
以下命令语句可以在 MySQL 的命令提示符使用,也可以在脚本中 使用,如PHP脚本。
命令 | 描述 |
---|---|
SELECT VERSION( ) | 服务器版本信息 |
SELECT DATABASE( ) | 当前数据库名 (或者返回空) |
SELECT USER( ) | 当前用户名 |
SHOW STATUS | 服务器状态 |
SHOW VARIABLES | 服务器配置变量 |
MySQL GROUP BY 语句
GROUP BY 语句根据一个或多个列对结果集进行分组。在分组的列上我们可以使用 COUNT, SUM, AVG,等函数。
举例说明
mysql> SELECT name, COUNT(*) FROM employee_tbl GROUP BY name; +--------+----------+ | name | COUNT(*) | +--------+----------+ | 小丽 | 1 | | 小明 | 3 | | 小王 | 2 | +--------+----------+ 3 rows in set (0.01 sec)