数据库基础篇之MySQL

--打开数据库
mysql -u 用户名 -p
password:用户名密码

--创建数据库并设置编码格式
create database 库名 charset 编码格式

--查看数据库
show databases;

--打开数据库
use 数据库的名字;

--打开数据库后,显示库中的数据表
show tables;

--在打开的数据库中创建数据表
--字段名 字段类型 约束(可加可不加)
create table teacher(
    id integer primary key auto_increment, --主键/自增长
    name varchar(10) not null,             --非空
    gender integer not null,               --非空
    courseid integer default 0,            --默认为0
    isMaster integer default 0             --默认为0
);

--查询表的字段信息
desc teacher;

--清空数据表
truncate table 表名称

--添加表字段
alter table teacher add sex bool not null;

--修改表字段
alter table 表名称 change 字段名称 字段名称 字段类型 约束; --修改全部
alter table 表名称 modify 字段名称 字段类型 约束;          --修改字段名以外部分

--删除表中某一字段
alter table 表名称 drop 字段名;

--基本的增删改查:
--在创建好的数据表中插入数据(增)
insert into teacher(name,gender) values("张三",1);

--删除数据表中不要的数据(删)
delete from teacher where name="张三";

--修改数据表中的数据(改)
update teacher set name ="李四" where name ="张三”;

--查询数据表中的数据(查)
select * from teacher where name="张三";    --精确查询
select * from teacher where name like"张%"; --模糊查询

--详细的数据库查询语法:
--完整语法
select [select选项] 字段列表[字段别名]或* from 数据源 [where 字句] [group by 字句] [having 字句] [order by 字句] [limit 字句];

--1)[select选项]
select选项包含:all --所有,默认
               distinct --去重,针对的是查询结果的整条记录而言
select distinct sex,courseid from teacher;

--2)[where字句]
where是唯一一个从磁盘开始拿数据就开始进行判断的条件,
从磁盘读取出一条记录,开始进行where判断,判断结果如果成立,
那么取出结果保存到内存,否则放弃。
select * from teacher where name="张三";

--3)[group by字句]
group by字句主要的作用是分组,从而进行统计操作,而不是为了展示(
展示的时候,只会展示分组记录的第一条记录),分组时,一般会结合使用:
count()、max()、min()、avg()、sum()函数

--A:字段分组
select c_id,count(*),max(height),min(height),avg(height),avg(age) from my_student group by c_id;
sql语句的意思是:my_student表以c_id进行分组,然后显示分组后的每组的c_id名称,每组的总数,每组的最高,
最低,平均身高和每组的平均年龄。没有写排序规则时默认为升序。

--B:多字段分组
select c_id,sex,count(*),max(height),min(height),avg(height),sum(age) from my_student group by c_id,sex;
sql语句的意思是:对整个表先按照c_id进行分组,然后在此分组的基础之上,每组再按照sex进行分组。

--C:多字段分组(加上显示每组的某一字段的所有数据)
select c_id,sex,count(*),max(height),min(height),avg(height),sum(age),GROUP_CONCAT(name) from my_student group by c_id,sex;
sql语句的意思是:GROUP_CONCAT(name),显示每组name字段的所有数据

--4)[having 字句]
having的作用类似where,而且having能做几乎所有where能做的事情,而where却不能做having能做的
很多事情,主要是因为where只能在磁盘提取数据的时候对数据进行操作;而在内存中对数据进行group by
分组之后的结果进行处理,只能通过having
select c_id,count(*),max(height),min(height),avg(height),sum(age) from my_student group by c_id having COUNT(*)>=3;

--5)[order by字句]
对数据进行排序操作,根据某个字段进行升序或者降序排序.(进行多字段排序的时候,先根据某一字段进行排序,
然后在排序好的内部再按照某字段进行排序)

--A:单个字段排序
select * from my_student order by c_id;
sql语句的意思是:根据c_id进行排序查询,默认为升序

--B:多字段排序
select * from my_student order by c_id,sex;
sql语句的意思是:根据c_id升序排序后(默认的排序方式),再根据sex进行排序

--C:
升序:asc  降序:desc
select * from my_student order by c_id asc;--升序可以不写
select * from my_student order by c_id desc;

--6)[limit 字句]
限制结果的数量,limit偏移量记录条数

--A:
select * from my_student limit 2;
sql语句的意思是:查询后取前两个

--B:
select * from my_student limit 0,3;
sql语句的意思是:0表示下标,3表示长度


 

猜你喜欢

转载自blog.csdn.net/LNHJlnhj/article/details/81782890