《深入浅出MySQL》 读书笔记一:SQL基础

一、SQL分类

1、create、drop、alter等,属于 数据定义语言

2、insert、delete、update、select 属于 数据操纵语句

3、grant等 定义访问权限和安全级别 属于 数据控制语句

二、数据定义语句

mysql -uroot -p  输入密码 登陆MySQL
show databases;  查看所有数据库
create database test;  创建test数据库
use test  将 数据库切换为 test
show tables;  查看所有的表
drop database test; 删除test数据库
CREATE TABLE test (
	id INT NOT NULL auto_increment PRIMARY KEY,
	NAME VARCHAR (10)
);  使用默认引擎创建 test表
show variables like '%engine%';  查看默认的存储引擎
desc test;  查看test表结构
show create table test \G;  查看创建test表的语句
drop table test;  删除test表
alter table test modify name varchar(20);  修改表字段类型
alter table test add column age int;  增加表字段
alter table test drop column age;  删除表字段
alter table test change name name1 varchar(20);  修改表的 名称和类型
alter table test rename test11;  修改表名称

三、数据操作语句

insert into test11 (id, name1) values (1, '测试');  插入数据
insert into test11 values (1, '测试');  不带列名 按列名顺序存储
select * from test11;   查询数据
update test11 set name1='更新测试' where id = 1;  按条件更新数据
update a,b set a.name = b.name where a.code = b.code;  根据一张表的数据更新另一张表的数据
delete from test11 where id = 1;  删除数据 不加where则删除全部
select * from a,b where a.code = b.code;   内连接查询  只显示相互匹配的
select * from a left join b where a.code = b.code;  左连接查询 a表中的数据全部展示 b中不匹配的显示 null
select * from a right join b where a.code = b.code;  又连接查询
select distinct code from a;  去重查询 code
select * from a order by age desc,score asc; 排序查询
select count(type), type from a group by type;  聚合查询  group by 配合 聚合函数 sum() count()
select * from a where deptno in (select deptno from dept);  子查询  可以优化为 连接查询 如下
select a.* from a,b where a.deptno = b.deptno;  连接查询 相对子查询 不用创建中间表
select deptno from a
union
select deptno from b;  去重联合  union all 不去重

猜你喜欢

转载自blog.csdn.net/weixin_37882382/article/details/83110501