安装
1 sudo apt install mysql-server
2 sudo apt install mysql-client
3 sudo qpt install libmysqlclient-dev
进入mysql
mysql -h [主机] -u [sql用户名] -p [sql密码]
连接本机默认账户
mysql -h localhost -u root -p
常用命令
创建数据库
create database [数据库名] default character set utf8;
创建表
create table [表名](字段名 字段类型 约束);
约束:
premary key ----主键
auto_increment---自动增长
not null -------不为空
数据类型 括号内为最大字符长度
int/integer(10)
varchar
char
doubul
字段名-->表名
例:
创建学生表:
creat table student(id int primary key auto_increment,
name varchar(20) not null,
sex char(10),
score double not null);
插入数据
insert into [表名称](字段1,字段2) values(值1,值2),values(值1,值2);
例:可以一次插入多个数据
insert into student(name,sex,score) values(1001,'李四','男',90);
查看表记录
select * from [表名];
修改数值
update [表名] set [字段名]=[值],[字段名1]=[值1] where 条件;
例:
update student set age=10 name="张三"where id=1;
删除数据
delete from [表名] where 条件
查询
select [字段名1],[字段名2] from 表名 where 条件:
select name,score from student where sex="男";
查询表结构
desc 表名称;
排序查询
select * from [表名称] where 条件 order by [字段1],[字段2];
order by:按照神魔进行排序,优先按照字段1,再按照字段2
默认排序升序排序,降序desc 升序asc
例:按照程序进行排序
select * from student order by score
限制查询
限制条数查询
select * from [表名] limit n;
例:
查询前三条
select * from stident limit 3
根据偏移量和条数查询
select * from [表名] limit offset,n;
例;查询前3条
select * from student limit 2,3;
分页查询
select * from student limit(当前页数-1)*一页要显示的行数,一页要显示的行数;
例:
查询第一页
select * from goods limit 0,3;
删除表: drop table [表名] ;
删除数据库:drop database [数据库名]
查询当前所在数据库: select database();
给一个已经存在的表添加字段:
格式:
alter tabale 表名称 add column 列名称 数据类型 约束
例子:添加一个电话好拿的列
alter table student add column tel varchar(20) not null;
修改列表名称:
alter table 表名 change 列名称 数据类型 约束:
也可以改类型(最好用modify)
修改字段
alter table [表名] modify colimn 列名称 数据类型 约束
例:
alter table student modigy column tel varchar(20)not null;
删除一列
alter table [表名称] drop column [列名称];
例:
alter table student drop column tel;
内置函数
sum() -- 求和
avg() -- 求平均值
max() --求最大值
min() --求最小值
count(字段/列名)---找非空的个数
count(*) 行数
select 函数名 from 表名;
例:
求goods 表中所有价格的和
sekect sum(price) from goods
————————————–
查询
2.分组查询的语法
select 字段列表,内置函数 from 表名称 group by 分组字段 【having 过滤条件】;
eg:
select type,avg(price) from product group by type
从产品表中按照类型进行分类,求出价格的平均值
代码示例:
创建product商品表并插入记录:
create table product(
id int primary key auto_increment,
proname varchar(20) not null,
price double not null,
type varchar(10) not null
);
insert into product(proname,price,type)values(‘辣条’,2.5,’零食’),
(‘洗衣粉’,10,’日用品’),(‘铅笔’,3,’文具’),(‘笔记本’,5,’文具’),
(‘旺旺雪饼’,12.5,’零食’),(‘脸盆’,5,’日用品’),(‘牙刷’,3,’日用品’),
(‘二锅头’,20,’烟酒’);
1. 按照商品种类分组,并将每组的价格平均值显示出来
select type,avg(price) from product group by type;
只查询“烟酒”类的平均价格
select type,avg(price) from product group by type having type=’烟酒’;- 以类型分组,查询每一组内容 价格 和每一组的平均值
select type,group_concat(name,price),avg(price) from product group by type;
4.以类型分组,只查询某一组组内容 价格 和每一组的平均值
select type,group_concat(name,price),avg(price) from product group by type having type=”文具”;- 以类型分组,查询每一组内容 价格 和每一组的平均值
3.模糊查询(like)
select 字段列表 from 表名称 where 字段名 like 模糊查询条件
模糊查询的通配符
%—任意多个任意字符
_ 代表一个任意字符
示例代码:
创建一个用户表,表中有用户名和用户密码。
create table user(
id int primary key auto_increment,
name varchar(20) not null,
pwd varchar(20) not null
);
insert into user(name,pwd)values('张三丰','123'),('张无忌','456'),
('李逵','567'),('王刚勇','258'),('李刚','159');
1. 查询所有姓"张"的用户
select * from user where name like '张%';
2. 名字中包含“刚”的用户
select * from user where name like '%刚%';
3. 名字中最后一个字为“刚”的用户
select * from user where name like '%刚';
4. 名字中第二个字为“刚”的用户
select * from user where name like '_刚%';
5. 名字中包含三个字,并且第二个字为“刚”
select * from user where name like '_刚_'
4.一对多关系表
比如:学生表和学生表的关系。一个学校表中的一条信息可以对应学生表中的多条信息
比如:学校表中有清华、北大。清华学校对应很多的学生姓名
示例代码:
先创建学校表:
create table school(
schoolid int primary key auto_increment,
schoolname varchar(30) not null,
address varchar(20) not null,
history text
);
insert into school(schoolname,address,history)values
('清华大学','北京','清华大学是一所历史悠久的学校'),
('北京大学','北京','北京大学也很厉害'),
('西安交通大学','西安',NULL);
再创建学生表:
create table student(
stuid int primary key auto_increment,
stuname varchar(30) not null,
sex char(5) not null,
score double not null,
school_id int not null,
constraint fk_student foreign key (school_id) references school(schoolid)
on delete cascade
);
insert into student(stuname,sex,score,school_id)values('令狐冲','男',92.5,1),
('郭靖','男',82.5,2),('黄蓉','女',85,2),('东方不败','女',96.5,3);
1. 查询每个学生的学号、姓名、性别、成绩、所在学校名称。
select stuid,stuname,sex,score,schoolname from student,school
where student.school_id=school.schoolid;
2.查询名字为“郭靖”的学生的学号、姓名、性别、成绩、所在学校名称。
select stuid,stuname,sex,score,schoolname from student,school
where student.school_id=school.schoolid and student.stuname='郭靖';
5.一对一
一个表中的一条记录最多对应另一个表中的一条记录。
示例代码:
创建人表,有id人的编号、name、sex、age
创建身份证表,有:身份证号、人的ID
先创建person人表:
create table person(
id int primary key auto_increment,
name varchar(20) not null,
age int,
sex char(10)
);
insert into person(name,age,sex)values('张三',25,'男'),('李四',22,'男'),
('宝强',26,'男'),('小宝',12,'女') ;
再创建card身份证表:
create table card(
cardno varchar(20) primary key,
person_id int not null unique,
关联外键
constraint fk_card foreign key(person_id) references person(id)
);
insert into card(cardno,person_id)values('lskkk123456',2),('bqlll56789',3),
('zs123456',1);
5.5 一对多
给student加外键
alter table 表名 add foreign key (外键) references 从表(从表键)
alter table student add foreign key (school_name) r eferences school(id);
6.多对多:
多对多就是一个表中的数据可以对应另一个表中的多条记录,反之也成立
多对多需要一个中间人,中间表。中间表至少包含2个字段,分别关联2个表的主键,这2个字段要设置为外键
示例代码:
学生选课
创建学生表并插入记录:
create table stu(
id int primary key auto_increment,
name varchar(20) not null,
age int,
sex char(5),
score double not null
);
insert into stu(name,age,sex,score)values('令狐冲',22,'男',85),
('郭靖',25,'男',65),('黄蓉',22,'女',79);
创建课程表并插入记录:
create table subject(
id int primary key auto_increment,
name varchar(20) not null
);
insert into subject(name)values('数学'),('英语'),('计算机'),('Python编程课程');
创建中间关联表,并插入记录说明关联关系:
create table stu_subject(
id int primary key auto_increment,
stu_id int,
subject_id int,
foreign key(stu_id) references stu(id),
foreign key(subject_id) references subject(id)
);
insert into stu_subject(stu_id,subject_id)values(1,1),(1,3),(1,4),
(2,1),(2,4),(3,2);
问题:
1.学号为1的学生选择了哪些课程?
第一步:在“中间表”中查询学号为1的学生关联的课程编号
select subject_id from stu_subject where stu_id=1;
第二步:在“课程表”中根据第一步查询的课程编号,将课程名称查询出来?
select name from subject where id in (select subject_id from stu_subject where stu_id=1);