1、进入mysql
mysql -u root -p
2、列出库名
show databases;
3、建立一个名为stocks的数据库
create database stocks;
4、选择库
use stocks;
5、创建一张表
create table stocks(id int unsigned not null auto_increment,goods_num varchar(50) not null COMMENT '商品编码',bar_code varchar(100) not null COMMENT '条形码',amount int(4) unsigned not null COMMENT '库存量',add_time int(11) unsigned not null COMMENT '添加时间',PRIMARY KEY (`id`),KEY `bar_code` (`bar_code`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='库存表';
6、列出所有表名
show tables;
7、修改表名
alert table stockts rename stocks;
8、查看表结构
desc stocks;
show create table stocks;
9、插入数据
insert into stocks ('goods_num','bar_code','amount','add_time') values (236,23654,1,1477141318);
10、更新多条不同数据
UPDATE `stocks` SET `amount` = CASE `id` when 1 then 3 end, `add_time` = CASE `id` WHEN 1 THEN 1478349336 END where id in (1);
11、删除一条数据
DELETE FROM `stocks` WHERE `id` = 3;
12、添加普通索引
ALTER TABLE `stocks` ADD INDEX `goods_num` (`goods_num`) USING BTREE ;
13、重置表数据
TRUNCATE TABLE `stocks`;
14、修改字段类型
ALTER TABLE `stocks` MODIFY COLUMN `add_time` INT(10);
15、UNIX时间戳转换为日期
SELECT FROM_UNIXTIME(1466752586);
16、日期转换为UNIX时间戳
SELECT UNIX_TIMESTAMP('2016-06-24 15:16:26');
17、查询数据表中重复的值(显示重复数最多的10条)
SELECT bar_code,COUNT(1) FROM `stocks` GROUP BY bar_code ORDER BY COUNT(1) DESC LIMIT 10;
18、删除普通索引
alter table `sales` drop index sales_create_at_index;
19、修改字段名
alter table `stocks` change create_at created_at timestamp null default null;
20、删除表字段
alter table `sales` drop column add_time;