mysql基础

show databases;
create database python charset=utf8;
show create database python;
drop databases python;

create table tb_test(id int primary key not null auto_increment, name varchar(30))
create table tb_student(
id int unsigned not null auto_increment primary key,
name varchar(30),
-- 无符号整形0-255
age tinyint unsigned default 0,
high decimal(5,2),
-- 枚举类型 可用数字表示 1,2
gender enum("男","女") default "男",
-- 最后一个字段不能写 ,
cls_id int unsigned
)
insert into tb_student values(0, "老王", 18, 177.34, "男", 0),(1,"重工",46,234.44,"女",1)
alter tbale tb_student add birthday datetime;
-- 只修改类型 不修改名字
aleter table tb_student modify birthday date;
-- 修改列名和类型
alter table tb_student change birthday birth date default "2000-01-01";
-- 删除列
alter table tb_student drop high;
show create table tb_student;
-- 查看表结构
desc tb_student;
-- 更新所有
update tb_student set gender=1,age=44;
update tb_student set age=43,name="貂蝉" where id=1;
select name as 姓名, gender as 性别 from tb_student;
-- 删除所有男性
delete from tb_student where gender="男";
-- _仅仅只匹配一个字符 %匹配任意个字符 可以有可以没有
select name from tb_student where name like "%小明";
select name from tb_student where name like "__小明";
-- rlike匹配正则表达式
select name from tb_student where name rlike "^周.*"
-- 范围查询
select name from tb_student where age not in (12,13,14)
select name from tb_student where age not between 12 and 56
-- 判空
select name from tb_student where age is not null
select name from tb_student where age is null
-- 默认升序排列 等价于order by age asc
select distinct age 年龄 from tb_student order by age desc
select age 年龄 from tb_student order by age
select * from tb_student where age between 18 and 34 and sex="女" order by height desc, age, id desc
-- 聚合函数 count max min avg sum
select count(*) from tb_student
-- round()保留指定位数小数
select round(sum(*)/count(*),2) from tb_student
-- 一般分组和聚合函数结合使用 统计信息 以分组列作为查询列 配合聚合函数求改组信息
select gender, count(*) from tb_student group by gender
-- 查询每个分组内详细信息
select gender, group_concat(name,"_",age," ",id) from tb_student group by gender
-- having 用来对分组进行过滤 只用在group by后 一般和聚合函数结合使用 eg:只显示平均年龄大于20的分组
select gender, group_concat(name) from tb_student group by gender having avg(age) > 20
-- limit 分页 SQL语句执行顺序 from => where => group by => having => order by => limit => select
select * from tb_student limit 0,10
select * from tb_student limit 10,10
select * from tb_student limit (page-1) * size, size
select * from tb_student where gender = 2 order by height desc limit 0, 2
-- 多表查询 默认显示两表所有列 mysql不支持full join
-- inner join =》 join left outer join => left join
-- right outer join => right join full outer join => full join
select s.*, c.name from tb_student s join tb_class c on s.cid = c.id where s.cid = 2
-- natual join 省略连接条件 当连接条件为双方唯一相同列时
select * from tb_student natual join tb_class
-- source 省市县.sql copy数据至 database 需要在sql文件目录下登录mysql客户端 use dbname
-- 自关联
select * from tb_areas a join tb_areas b on a.id=b.pid where a.city="陕西"
--嵌套子查询
select * from tb_areas where pid = (select id from tb_areas where provice = "河北" )
-- 拆表
--将查询出的数据插入一张已经创建好的新表
insert into goods_cate(name) select cate_name from tb_goods group by cate_name
-- 更新现有表cate_name为新表id
update goods as g join goods_cate as c on g.cate_name = c.name set g.cate_name = c.id
-- 修改cate_name类型与列名
alter table goods change cate_name cate_id int unsigned not null
-- 修改cate_name外键关联新表
alter table goods add foreign key(cate_id) references goods_cate(id)
-- 删除外键
alter table goods drop foreign key(cate_id)
-- 创建视图 删除视图 视图不能更新
create view view_name as select * from tb_student
show tables
drop view view_name
-- 事务 mysql客户端默认开启事务 每条修改语句都会执行commit
-- pymysql 中每条修改语句需要在语句后 单独commit
-- mysql开启多条语句事务 begin transaction; 或者 start transaction;
-- commit; rollback; commit前任何语句在退出事务时都会自动rollback
-- 主键自增长插入 主键字段写0或null Null都可以
-- 创建索引
create table tb_test_index(title varchar(10))
-- 插入10000条测试数据 开启运行事件检测功能测试查询事件 约29ms
set profiling=1
select * from tb_test_index where title="9998"
show profiles
-- 为字段创建索引 再次测试 约0.5ms
create index title_index on tb_test_index(title(10))
-- 查询表中索引 删除索引
show index from tb_student
drop index index_name on tb_student
-- 必须root用户登录
-- 新建用户且只能在本机登录只对test数据库中的表进行读操作
grant select on test.* to "zhangsan"@"localhost" identified by "123456"
-- 新建用户以指定密码能在任何电脑登录对test数据库中所有表有所有权限
grant all privileges on test.* to "laowang"@"%" identified by "1334566"
-- 默认mysql只允许本机登录 可以在配置文件中 #注释掉bind-address=127.0.0.1重启即可远程连接 但不推荐 可用ssh先连接主机再登录mysql
-- 删除用户
drop user "zhangsan"@"localhost"
delete from user where user="zhangsan"
-- 操作完成后需要刷新权限
flush privileges
-- 备份 主从
mysqldump -uroot -p123456 --all-databases --lock-all-tables > /master_db.sql
-- (不登陆)执行命令 mysql -uroot -p123456 < master_db.sql
-- 主从配置文件更改
vi /etc/mysql/mysql.conf.d/mysqld.cnf
-- 添加
server-id = (唯一表示该主机id的数字 可以以ip地址最后后几位表示)
log_bin = /var/log/mysql/mysql-bin.log
-- 主从命令 先重启主机和从机
-- 在主机中建立从机登录账号
GRANT REPLICATION SLAVE ON *.* to 'slave1'@'%' identified by '123456';
flush privileges;
show master status;
-- 从机中执行命令 maser_log_file和master_log_pos值与上面命令中对应值相同
(stop slave)
CHANGE MASTER TO MASTER_HOST='192.168.8.10',
  MASTER_PORT=3306,
  MASTER_USER='slave1',
  MASTER_PASSWORD='123456',
  MASTER_LOG_FILE='binlog.000001',
  MASTER_LOG_POS=1304;
start slave;
show slave status\G;

你可能感兴趣的:(mysql基础)