回顾完pandas,紧接着也罢把mysql也回顾回顾吧。
打开cmd,输入:net start mysql80
,其中80是之前你安装mysql时设置的服务器的名称,
如果报如下的错,就说明时cmd的权限不够,要打开cmd所在的目录,更改为管理员模式,并将更改后的cmd文件复制到c:/windows下,并重命名为cmdd,以后运行cmd就通过win+r然后输入cmdd就OK了,如果你用原来的cmd还是会报错哦。
启动成功为:
附带一句:这个mysql启动可以通过如下的按钮直接完成:
停止服务器:
net stop mysql80
停止与登录服务器账户密码后再退出的效果一样:
exit;
对下面基本的语句不做过多的解释,只对比较难的做解释。
show databases;
use 某个数据库;
show tables;
create table student(name varchar(10));
其中的10表示的是最多10个汉字哦,再mysql中,一个汉字占用3个字节。
desc student;
或者describe student;
显示student表的配置结构。
select * from student;
*表示没有具体的查询对象,返回所有的内容。
insert into student values('张三');
update student set name='王四';
delete from student where name='王四';
drop table student;
create table student(name varchar(10), id int); alter table student add primary key(id);desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(10) | YES | | NULL | |
| id | int | NO | PRI | NULL | |
+-------+-------------+------+-----+---------+-------+
还可以通过下面的操作达到同样的效果:
alter table student modify id int primary key;
有一点需要注意的是:主键设置了并不会在表中有所显示,除非用desc。按理说应该是会放在最前面的一列才更直观。
删除所有的primary key:
alter table student drop primary key;
create table student(name varchar(10), unique(name));
创建唯一的primary key。
alter table student drop index name;
删除唯一的primary key。
create table student(name varchar(10) not null);
非空约束。
alter table student modify name varchar(10);
删除非空约束, 通过modify间接去掉了原来的约束。
create table student(name varchar(10) default '张三', id int);
默认约束,但这个默认值有用吗?好像有没有都没啥影响吧。
alter table student modify name varchar(10);
删除默认约束。
create table class(name varchar(10), id int primary key);create table student(name varchar(10), id int primary key, class_id int, foreign key(class_id) references class(id));
主表是:class
副表是:student
当主表有数据被副表利用时,主表不能被删除
主表中没有的数值,在副表中不能使用
这个项目目的在于复习。
# 创建数据库data1
CREATE DATABASE data1;
# 使用数据库data1
USE data1;
# 创建学生表
CREATE TABLE student (
num VARCHAR(20) PRIMARY KEY,
name VARCHAR(20) NOT NULL,
gender VARCHAR(10) NOT NULL,
birthday DATE,
class VARCHAR(20)
);
# 创建教师表
CREATE TABLE teacher (
num VARCHAR(20) PRIMARY KEY,
name VARCHAR(20) NOT NULL,
gender VARCHAR(10) NOT NULL,
birthday DATE,
profession VARCHAR(20) NOT NULL,
department VARCHAR(20) NOT NULL
);
# 创建课程表
CREATE TABLE course (
num VARCHAR(20) PRIMARY KEY,
name VARCHAR(20) NOT NULL,
t_num VARCHAR(20) NOT NULL,
FOREIGN KEY(t_num) REFERENCES teacher(num)
);
# 成绩表
CREATE TABLE score (
s_num VARCHAR(20) NOT NULL,
c_num VARCHAR(20) NOT NULL,
score DECIMAL,
FOREIGN KEY(s_num) REFERENCES student(num),
FOREIGN KEY(c_num) REFERENCES course(num),
PRIMARY KEY(s_num, c_num) # 这里设置联合primary key的原因是学生的num和课程的num只能有一个是相同的,因为同一个学生不会选两个一样的课
);
# 查看所有表
SHOW TABLES;
# 添加学生表数据
INSERT INTO student VALUES('101', '曾华', '男', '1977-09-01', '95033');
INSERT INTO student VALUES('102', '匡明', '男', '1975-10-02', '95031');
INSERT INTO student VALUES('103', '王丽', '女', '1976-01-23', '95033');
INSERT INTO student VALUES('104', '李军', '男', '1976-02-20', '95033');
INSERT INTO student VALUES('105', '王芳', '女', '1975-02-10', '95031');
# 添加教师表数据
INSERT INTO teacher VALUES('804', '李诚', '男', '1958-12-02', '副教授', '计算机系');
INSERT INTO teacher VALUES('856', '张旭', '男', '1969-03-12', '讲师', '电子工程系');
INSERT INTO teacher VALUES('825', '王萍', '女', '1972-05-05', '助教', '计算机系');
# 添加课程表数据
INSERT INTO course VALUES('3-105', '计算机导论', '825');
INSERT INTO course VALUES('3-245', '操作系统', '804');
INSERT INTO course VALUES('6-166', '数字电路', '856');
# 添加添加成绩表数据
INSERT INTO score VALUES('103', '3-105', '92');
INSERT INTO score VALUES('103', '3-245', '86');
INSERT INTO score VALUES('103', '6-166', '85');
INSERT INTO score VALUES('105', '3-105', '88');
INSERT INTO score VALUES('105', '3-245', '75');
INSERT INTO score VALUES('105', '6-166', '79');
# 查看表结构
SELECT * FROM course;
SELECT * FROM score;
SELECT * FROM student;
SELECT * FROM teacher;
结果:
mysql> SELECT * FROM course;
+-------+------------+-------+
| num | name | t_num |
+-------+------------+-------+
| 3-105 | 计算机导论 | 825 |
| 3-245 | 操作系统 | 804 |
| 6-166 | 数字电路 | 856 |
+-------+------------+-------+
mysql> SELECT * FROM score;
+-------+-------+-------+
| s_num | c_num | score |
+-------+-------+-------+
| 103 | 3-105 | 92 |
| 103 | 3-245 | 86 |
| 103 | 6-166 | 85 |
| 105 | 3-105 | 88 |
| 105 | 3-245 | 75 |
| 105 | 6-166 | 79 |
+-------+-------+-------+
mysql> SELECT * FROM student;
+-----+------+--------+------------+-------+
| num | name | gender | birthday | class |
+-----+------+--------+------------+-------+
| 101 | 曾华 | 男 | 1977-09-01 | 95033 |
| 102 | 匡明 | 男 | 1975-10-02 | 95031 |
| 103 | 王丽 | 女 | 1976-01-23 | 95033 |
| 104 | 李军 | 男 | 1976-02-20 | 95033 |
| 105 | 王芳 | 女 | 1975-02-10 | 95031 |
+-----+------+--------+------------+-------+
mysql> SELECT * FROM teacher;
+-----+------+--------+------------+------------+------------+
| num | name | gender | birthday | profession | department |
+-----+------+--------+------------+------------+------------+
| 804 | 李诚 | 男 | 1958-12-02 | 副教授 | 计算机系 |
| 825 | 王萍 | 女 | 1972-05-05 | 助教 | 计算机系 |
| 856 | 张旭 | 男 | 1969-03-12 | 讲师 | 电子工程系 |
+-----+------+--------+------------+------------+------------+
mysql> select * from student;
+-----+------+--------+------------+-------+
| num | name | gender | birthday | class |
+-----+------+--------+------------+-------+
| 101 | 曾华 | 男 | 1977-09-01 | 95033 |
| 102 | 匡明 | 男 | 1975-10-02 | 95031 |
| 103 | 王丽 | 女 | 1976-01-23 | 95033 |
| 104 | 李军 | 男 | 1976-02-20 | 95033 |
| 105 | 王芳 | 女 | 1975-02-10 | 95031 |
+-----+------+--------+------------+-------+
# 选取某些字段的行,注意了字段的顺序会决定最后表的呈现顺序
mysql> select name, gender, class from student;
+------+--------+-------+
| name | gender | class |
+------+--------+-------+
| 曾华 | 男 | 95033 |
| 匡明 | 男 | 95031 |
| 王丽 | 女 | 95033 |
| 李军 | 男 | 95033 |
| 王芳 | 女 | 95031 |
+------+--------+-------+
mysql> select distinct department from teacher;
+------------+
| department |
+------------+
| 计算机系 |
| 电子工程系 |
+------------+
mysql> select * from score where score between 60 and 80;
+-------+-------+-------+
| s_num | c_num | score |
+-------+-------+-------+
| 105 | 3-245 | 75 |
| 105 | 6-166 | 79 |
+-------+-------+-------+
# 注意要加括号哦
mysql> select * from score where score in (80, 86, 88);
+-------+-------+-------+
| s_num | c_num | score |
+-------+-------+-------+
| 103 | 3-245 | 86 |
| 105 | 3-105 | 88 |
+-------+-------+-------+
mysql> select * from student order by class desc;
+-----+------+--------+------------+-------+
| num | name | gender | birthday | class |
+-----+------+--------+------------+-------+
| 101 | 曾华 | 男 | 1977-09-01 | 95033 |
| 103 | 王丽 | 女 | 1976-01-23 | 95033 |
| 104 | 李军 | 男 | 1976-02-20 | 95033 |
| 102 | 匡明 | 男 | 1975-10-02 | 95031 |
| 105 | 王芳 | 女 | 1975-02-10 | 95031 |
+-----+------+--------+------------+-------+
mysql> select * from score order by c_num asc, score desc;
+-------+-------+-------+
| s_num | c_num | score |
+-------+-------+-------+
| 103 | 3-105 | 92 |
| 105 | 3-105 | 88 |
| 103 | 3-245 | 86 |
| 105 | 3-245 | 75 |
| 103 | 6-166 | 85 |
| 105 | 6-166 | 79 |
+-------+-------+-------+
mysql> select count(*) from student where class='95031';
+----------+
| count(*) |
+----------+
| 2 |
+----------+
mysql> select s_num, c_num from score where score=(select max(score) from score);
+-------+-------+
| s_num | c_num |
+-------+-------+
| 103 | 3-105 |
+-------+-------+
或者:
mysql> select s_num, c_num from score order by score desc limit 0,1;
+-------+-------+
| s_num | c_num |
+-------+-------+
| 103 | 3-105 |
+-------+-------+
mysql> select c_num, avg(score) from score group by c_num;
+-------+------------+
| c_num | avg(score) |
+-------+------------+
| 3-105 | 90.0000 |
| 3-245 | 80.5000 |
| 6-166 | 82.0000 |
+-------+------------+
mysql> select c_num, avg(score), count(*) from score group by c_num having count(c_num)>=2 and c_num like '3%';
+-------+------------+----------+
| c_num | avg(score) | count(*) |
+-------+------------+----------+
| 3-105 | 90.0000 | 2 |
| 3-245 | 80.5000 | 2 |
+-------+------------+----------+
mysql> select name, c_num, score from student, score where student.num=score.s_num;
+------+-------+-------+
| name | c_num | score |
+------+-------+-------+
| 王丽 | 3-105 | 92 |
| 王丽 | 3-245 | 86 |
| 王丽 | 6-166 | 85 |
| 王芳 | 3-105 | 88 |
| 王芳 | 3-245 | 75 |
| 王芳 | 6-166 | 79 |
+------+-------+-------+
mysql> select c_num, avg(score) from score where s_num in (select num from student where class = '95031')
-> group by c_num;
+-------+------------+
| c_num | avg(score) |
+-------+------------+
| 3-105 | 88.0000 |
| 3-245 | 75.0000 |
| 6-166 | 79.0000 |
+-------+------------+
mysql> select c_num, avg(score), count(*) from score group by c_num having count(c_num)>=2 and c_num like '3%';
+-------+------------+----------+
| c_num | avg(score) | count(*) |
+-------+------------+----------+
| 3-105 | 90.0000 | 2 |
| 3-245 | 80.5000 | 2 |
+-------+------------+----------+
mysql> select name, c_num, score from student, score where student.num=score.s_num;
+------+-------+-------+
| name | c_num | score |
+------+-------+-------+
| 王丽 | 3-105 | 92 |
| 王丽 | 3-245 | 86 |
| 王丽 | 6-166 | 85 |
| 王芳 | 3-105 | 88 |
| 王芳 | 3-245 | 75 |
| 王芳 | 6-166 | 79 |
+------+-------+-------+
mysql> select num, name,birthday from student where year(birthday)
-> in (select year(birthday) from student where num in (101, 108));
+-----+------+------------+
| num | name | birthday |
+-----+------+------------+
| 101 | 曾华 | 1977-09-01 |
+-----+------+------------+