mysql复习——基础知识

一、什么是事务?及其特性?

事务:是一系列的数据库操作,是数据库应用的基本逻辑单位。

特性:

(1)原子性:即不可分割性,事务要么全部被执行,要么就全部不被执行。

(2)一致性或可串性。事务的执行使得数据库从一种正确状态转换成另一种正确状态

(3)隔离性。在事务正确提交之前,不允许把该事务对数据的任何改变提供给任何其他事务,

(4) 持久性。事务正确提交后,其结果将永久保存在数据库中,即使在事务提交后有了其他故障,事务的处理结果也会得到保存。

简单理解:在事务里的操作,要么全部成功,要么全部失败。

二、数据库操作练习

1.链接数据库
mysql -uroot -ppython
2.退出数据库
exit; quit;
3.显示数据库的版本
select version();
4.显示时间
select now();
5.查看所有的数据库
show databases;
6.创建数据库
create database 数据库的名字 charset=utf8;
7.查看创建数据库的语句
show create database pymysql_2019;
8.查看当前使用的数据库
select database();
9.使用数据库
use 数据库的名字;
10.删除数据库
drop database 数据库的名字;
drop database pymysql_2018;
20.删除字段
alter table 表名 drop 字段名;
alter table students drop birth;
21.删除表
drop table 表名
22.删除数据
delete from 表名 where 条件;
11.查看当前数据库的表
show tables;
12.创建表
create table students(
       id int unsigned not null auto_increment primary key,
       name varchar(30) not null,
        age tinyint unsigned default 0,
        high decimal(5,2),
        gender enum('男','女','中性','保密') default "保密",
        class_id int unsigned 
        );
create table students1(
	   id int unsigned not null auto_increment primary key,
       name varchar(30) not null,
    	age tinyint unsigned default 0,
    	high decimal(5,2),
    	gender enum('男','女','中性','保密') default "保密",
    	class_id int unsigned
		);
13.查看表的结构
desc 数据表的名字;
15.查询表中所有的数据
select * from 表名; select 列表1 from 表名;(查询指定字段)

#####16.查看表的创建语句

show create table 表名;
17.添加表的字段
alter table students add birth datetime;
18.修改字段(不重命名版)
alter table 表名 modify 列名 类型;

19.修改字段(重命名版)
alter table 表名 change 列名 类型;
14.插入一条数据
insert into classes values(0, "python2班");
insert into students values(0,"张三",15,170,"女",1);
14-1.部分插入
insert into 表名(列名1,) value ()
22.修改
update 表名 set 列名1=where 条件;
update students set name = "张三" where id =1;
23.查询
select * from students where id<5;
select * from students where gender="女" and age>18;
select * from students where age>18 or height>=180;
23-1模糊查询
select name from students where name like "小%";
select name from students where name rlike "^周.+";
23-2.范围查询
1.非连续范围
	select name from students where age in(18, 34);
2.连续范围
	select * from students where age between 18 and 34;
23-3.查询判断是不是空
select * from students where height is null;(判断是空)
select * from students where height is not null;(判断是非空)
24.给列指定别名
select 字段 as 姓名 from 表名 条件
select name as 姓名 from students where id>1;
25.给表起别名
select s.name,s.age from students as s;
26.查询消除重复行
select distinct 字段 from 表名;
27.排序
select * from 表名 where gender = 1 order by 列名 asc;(从小到大)
select * from students where gender = 2 order by id asc;
select * from 表名 where gender = 1 order by 列名 desc;(从大到小)
select * from students where gender = 2 order by id desc;
28.计算数量
select count(*) as "男性人数" from students where gender=1;
29.最大值、最小值、求和、平均值
select max(age) from students;
select min(age) from students;
select sum(age) from students;
select avg(age) from students;
30.四舍五入
select round (avg(height),2) from students where gender=1;
31.分组
select gender, count(*) from students where gender=1 group by gender;
select gender, group_concat(name) from students group by gender having count(*)>2;
32.分页
select * from students where gender=1 limit 2;
select * from students limit 0, 2;
33.连接
select s.*,c.name from students as s inner join classes as c on s.cls_id = c.id;
34.子查询
select * from students where height = (select max(height) from students);
35.导入sql文件
source areas.sql;
36.外键
alter table goods add foreign key (brand_id) references goods_brands(id);
发布了7 篇原创文章 · 获赞 4 · 访问量 622

猜你喜欢

转载自blog.csdn.net/weixin_43755186/article/details/105392358