SQL操作

 基本操作

mysql -P 3306 -h 127.0.0.1 -uroot -p123456
show enginesG    可以用“;”结束,也可以用“g”或者“G”结束。“g”与“;”的作用是相同的,“G”可以让结果显示得更加美观。
show variables like 'have%';    使用SHOW语句查询MySQL中支持的存储引擎
show databases;
create database 数据库名称 default character set utf8 collate utf8_general_ci;
use database 数据库名称;
drop database 数据库名称;
##如果使用temporary表示是临时表
create [temporary] table [if not exists] 数据表名
(
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`hotel_id` int(11) unsigned NOT NULL COMMENT '酒店ID hotel_base的id',
`created_at` datetime NOT NULL DEFAULT now() COMMENT '创建时间',
`updated_at` datetime NOT NULL DEFAULT now() COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `index_hotel_id` (`hotel_id`)
) COMMENT='酒店服务承诺信息' ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_unicode_ci;
###查看表结构
show columns from 数据库名.数据表名;
desc 数据表名;
desc 数据表名 列名;
##修改表结构
alter table 原数据表名 rename 新数据表名;
alter table 数据表名 modify name varchar(30);
alter table 数据表名 change city city_new varchar(30);
alter table 数据表名 add manager int(10);
alter table 数据表名 drop manager;
rename table 原数据表名 to 新数据表名;
drop table if exists 数据库表名;
##插入数据
INSERT INTO `meta_code` (`code`,`name`,`parent`,`group`,`group_name`,`description`) VALUES 
('0', '无星', null, 'jdxj', '酒店星级', null), 
('1', '五星', null, 'jdxj', '酒店星级', null);
##查询语句
select [distinct] [concat(col 1,":",col 2)as col] selection_list//要查询的内容,选择哪些列
from 数据库表名s //指定数据表
where primary_constraint    //查询时需要满足的条件,行必须满足的条件
group by grouping_columns    //如何对结果进行分组
order by sorting_cloumns    //如何对结果进行排序
having secondary_constraint    //查询时满足的第二条件
limit count //限定输出的查询结果
##更新语句
update 数据库表名 set column_name=new_value1,column_name2=new_value2,…… where condition;
##删除语句
delete from 数据库表名 where condition
原文地址:https://www.cnblogs.com/wade-luffy/p/6033431.html