SQL基本语句
1 数据库操作: 2 create database db_name charset utf8;#创建数据库 3 drop database db_name;#删除数据库 4 use db_name;#切换数据库 5 show databases;#查看所有数据库 6 7 表操作: 8 创建表: 9 数据类型: 10 整形: 11 tinyint smallint int bigint 12 浮点型: 13 float double 14 字符串: 15 char varchar text 16 日期类型: 17 date datetime timestamp 18 19 20 约束: 21 1、主键约束 唯一、非空 primary key 22 2、外键约束 foreign key 23 3、唯一约束 unique 24 4、非空约束 not null 25 5、默认值约束 default 26 6、自增长 auto_increment 27 DDL、DCL、DML语句: 28 DDL: 29 数据定义语句 create、drop、alter 30 DML: 31 数据操纵语句 select、insert、update、delete 32 DCL: 33 数据控制语句 grant 34 35 建表 36 create table student ( 37 id int primary key auto_increment, 38 name varchar(20) not null , 39 phone varchar(11) unique not null, 40 sex tinyint default 0, 41 addr varchar(50), 42 brith datetime default current_timestamp, 43 index(name) 44 ); 45 46 create table score ( 47 id int primary key auto_increment, 48 score float not null, 49 sid int not null 50 ); 51 create table student_new like student; -- 快速创建一个和student表结构一样的表 52 53 修改表: 54 alter table student add class2 int not null; -- 增加字段 55 alter table student drop addr; -- 删除字段 56 alter table student change name new_name varchar(20) not null; -- 修改字段 57 alter table student modify name varchar(30) ; 58 删除表: 59 drop table student; 60 清空表: 61 truncate table student; -- 自增长id会重新开始 62 其他操作: 63 show tables;-- 查看当前所有表 64 show create table student; -- 查看建表语句 65 desc student; -- 查看表结构 66 67 数据操作: 68 增: 69 insert into student values ('','python','11111111111',0,'北京','2019-01-03 18:39:23'); --写全 70 insert into student (name,phone) values ('mysql','12345678901'); -- 指定字段 71 insert into student (name,phone) values ('mysql1','12345678902'),('mysql2','22345678901'); --多条 72 insert into student_new select * from student; -- 把一个表的数据快速导出到另外一个表 73 修改: 74 update student set name='mysql3' ; --修改全表数据 75 update student set name'mysql2',sex=1; --修改多个字段 76 update student set name='mysql3' where id = 1; #指定修改某条数据 77 删除: 78 delete from student; --整表数据删除 79 delete from student where id = 3; --指定数据删除 80 81 查询: 82 基本查询 83 select * from student; 84 select id,name,addr from student; --指定字段 85 select id as 编号, addr 地址 , name 姓名 from student; --字段加别名 86 87 where条件 88 select * from student where id=1; --where条件 >,<,>=,<=,!=,<> 89 select * from student where id in (1,2,3) and id != 5; -- in和and条件 90 select * from student where id between 1 and 5; -- 范围 91 select * from student where id between 1 and 5 or id > 10; -- or 92 93 排序 94 select * from student where id between 1 and 5 order by id desc; -- 一起用的话,order by必须写在where条件后面 95 select * from student order by id desc ; -- 降序排序,按照id,升序的话是asc 96 select * from student order by id,name asc ; -- 升序,按照id和name排序,asc可以省略不写 97 98 分组 99 select * from student group by sex; -- 按照某个字段分组,可以写多个字段 100 select * from student group by sex having addr !='北京'; 101 select * from student where id >5 group by sex having addr !='北京'; -- 如果有where条件,必须写在group by前面,group by后面不能再写where条件,如果有条件必须用having子句 102 103 limit 104 select id as 编号, addr 地址 , name 姓名 from student limit 2; -- 前N条数据 105 select id as 编号, addr 地址 , name 姓名 from student limit 1,5; -- 从第一行开始,向后取5行,不包含第一行的数据 106 select * from student where id >0 group by sex having addr !='北京' limit 5; -- limit必须写在最后面 107 select * from student where id >0 group by sex having addr !='北京' order by id desc limit 5; -- limit必须写在最后面 108 #如果一个sql里面有where、group by、排序、limit,顺序一定是1、where 2、group by 3、order by 4、limit 109 110 聚合函数 111 select count(*) from student; -- 多少条数据 112 select count(addr) from student; -- 某个字段不为空的有多少条 113 select count(*) 人数 ,sex 性别 from student group by sex; -- 多少条数据 114 select avg(age) from student; -- 平均值 115 select sum(score) from score; -- 和 116 select min(score) from score; 117 select max(score) from score; 118 119 120 子查询 121 select * from student where id in (select sid from score where score >= 60); 122 123 多表查询 124 select * from student a ,score b where a.id = b.sid and a.score>90; 125 126 select a.name,b.score,a.class2 from student a ,score b where a.id = b.sid and a.score>90; 127 128 select a.name ,b.score,a.class2 from student a inner join score b on a.id = b.sid where a.score > 90; 129 130 select a.name ,b.score,a.class2 from student a left join score b on a.id = b.sid where a.score > 90; 131 132 授权 133 GRANT ALL privileges ON *.* TO 'root'@'localhost' IDENTIFIED BY '123456'; 134 GRANT ALL privileges ON byz.* TO 'byz'@'%' IDENTIFIED BY '123456'; 135 flush privileges;
下面是命令,不是sql语句,在命令行里面执行的。
1 mysql常用命令: 2 mysql -uroot -p123456 -h192.168.1.1 -P3306 #登录mysql 3 mysqldump -uroot -p123456 -h192.168.1.1 -P3306 -A > /tmp/all_data.sql #备份所有数据库 4 mysqldump -uroot -p123456 -h192.168.1.1 -P3306 --add-drop-table --add-drop -A > /tmp/all_data.sql #导出带有删除表和删除库的sql 5 mysqldump -uroot -p123456 -h192.168.1.1 -P3306 --no-data -d nhy_db > /tmp/all_data.sql #只导出表结构 6 mysql -uroot -p123456 -h192.168.1.1 -P3306 nhy_db < all_data.sql #恢复数据库
本文转自:牛牛杂货铺
欢迎大家学习参考!

浙公网安备 33010602011771号