启动mysql
1
sudo mysql.server start
1
2
3
4
5
6
7
8
9
10
错误:

ERROR! MySQL server PID file could not be found!
Starting MySQL
. ERROR! The server quit without updating PID file (/usr/local/var/mysql/XXdeMacBook-Pro.local.pid).

解决:

1.输入命令:sudo chown -R _mysql /usr/local/var/mysql
2.再次重启:sudo mysql.server restart

登陆mysql

1
2
sudo -uroot -p
密码:无

​ 1.如何查看有什么数据库?

1
show databases;

2.如何选择数据库?

1
use databasesName;

3.如何查看该数据库中有哪些表?

1
show tables;

4.如何显示某一表中的所有数据?

1
select * from tableName;

5.如何退出数据库服务器?exit;

1
exit;

6.如何在数据库服务器中创建自己的数据库?

1
create database databaseName;

7.如何创建一个数据表?

1
2
3
4
5
create table tableName(
id char,//id为属性名,char为属性类型
name varchar(20),
sex char
);
  1. 如何查看一个表的描述?

    1
    desc table_name;
 11.如何删除数据    先插入数据:   
1
2
3
4
INSERT INTO pet VALUES('kk1','cc1','dog1','1','1998-1-2',null); 
INSERT INTO pet VALUES('kk2','cc2','dog2','2','1998-2-2',null);
INSERT INTO pet VALUES('kk3','cc3','dog3','1','1998-3-2','1998-12-2');
INSERT INTO pet VALUES('kk4','cc4','dog4','2','1998-4-2',null);

12.mysql建表中的约束

1.主键约束: 它能够唯一确定一张表中的一条记录,增加主键约束之后,就可以使得字段不重复而且不为空

1
2
3
4
create table tableName(
id int primary key,//primary key为主键的意思,这里意为设置
//id为主键
)

**2.复合主键:**意为把其中的几个属性联合起来作为主键(复合主键允许单个码重复,但不允许同时重复)

1
2
3
4
5
6
7
8
9
10
11
create table tableName(
id char,
name varchar(20),
primary key(id,name)//设置复合主键
);
insert into tableName values('1','echo');
//Query OK, 1 row affected (0.00 sec)
insert into tabelName values('1','echos');
//Query OK, 1 row affected (0.00 sec)
insert into tableName values('1','echo');
//ERROR 1062 (23000): Duplicate entry '1-echos' for key 'user.PRIMARY'

where子句

单值查询:

1
2
3
-- 注释
-- 查询学生表的SId = 01 的学生的全部信息
select * from student where SId = '01';

image-20201129222320483

1
2
-- 查询姓名为 赵雷的学生的SId
select SId from Student where Sname = '赵雷';

image-20201129222749470

多值查询:

image-20201129223607625

1
2
-- 查询来自Student表的SId为‘01’的学生的出生日期和姓名
select Sname,Sage from student where SId = '01';

image-20201129223138565

1
2
-- 查询来自Course表 TId 为 01 的课程名和课程号
select Cname,CId from Course where TId = '01';

image-20201129223632398

否定查询

1
2
-- 查询Student表中所有Ssex不为 男 的所有学生信息
select * from Student where Ssex != '男';

image-20201129224010486

1
2
-- 查询Student表中所有性别不为 男 的所有学生学号
select SId from Student where Ssex != '男';

image-20201129224147941

1
2
-- 查询Student 表中性别为男且SId为 01 的学生的性别
select Sname from Student where (Ssex = '男' && SId = '01');

image-20201129224420108

1
2
-- 查询Student 表中性别不为男和姓名为赵雷的学生的所有信息
select * from Student where(Ssex != '男' || Sname = '赵雷');

image-20201129224645730

1
2
查询来自Student表中学号大于03并且小于10或者性别为男的所有学生的性别,姓名和学号
SELECT Sname,SId,Ssex FROM Student WHERE( (SId> '03' && SId< '10') || Ssex='男');

image-20201129225351504

多表查询

连表查询,不适用任何外界条件,生成笛卡尔积

1
select *from student,Sc;

image-20201129225742406

下面还有很长,表的长度等于笛卡尔积集的大小student的元祖数量 * Sc的元祖数量 = 13 * 18;

内链接(常用)只连接匹配的行

1
2
-- 查询所有学生的所有成绩
select * from SC inner join student on student.SId = Sc.SId;

1
2
-- 查询学生的所有信息及其成绩
SELECT Student.*,SC.score from SC,Student where(SC.SId = Student.SId);

image-20201130121516910

多表查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
-- 1 查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
SELECT
p.*,
c.Cname,
f.score
FROM
Student p,
SC f,
SC s,
Course c
WHERE
(
p.SId = f.SId
AND f.CId = '01'
AND s.CId = '02'
AND f.score > s.score
AND f.CId = c.CId
);

image-20201130124751243

数据更新

1. 修改某一个元组的值

1
2
将学生95001的年龄改为22岁。
update student set student.sage = 22 where student.sno = 95001;

2. 修改多个元组的值

1
2
3
4
将所有学生的年龄增加1岁。
update student.sage set student.sage = student.sage + 1;
将信息系所有学生的年龄增加1岁。
update student set student.Sage = student.Sage + 1 where student.Sdpet = 'IS';

3. 带子查询的修改语句

1
2
3
4
5
6
7
将计算机科学系全体学生的成绩置零。
UPDATE SC
SET Grade=0
WHERE
'CS'= (SELETE Sdept
FROM Student
WHERE Student.Sno = SC.Sno);

4. 删除某一个元组的值

1
2
删除学号为95019的学生记录。
DELETE from student where student.Sno = 95019

视图的操作

行列子集视图

1
2
create view is_student as SELECT student.sage,student.sdpet from student where student.sno = 95001;
-- 从单个基本表导出只是去掉了基本表的某些行和某些列保留了码

WITH CHECK OPTION**的视图**

1
2
3
4
5
6
7
8
9
-- 建立信息系学生的视图,并要求透过该视图进行的更新操作只涉及信息系学生。
CREATE VIEW I_Student AS SELECT
Sno,
Sname,
Sage
FROM
Student
WHERE
student.sdpet = 'IS' WITH CHECK OPTION;

三范式

1.1NF

1
1NF要求数据库表单设计必须满足原子性,及所有属性都是不可拆分的

2.2NF

1
2NF要求数据库表单设计必须满足主属性完全依赖,及所有属性都必须完全依赖主属性,不能出现部分依赖

3.3NF

1
2
3
3NF要求数据库表单设计必须满足非主属性对主属性的直接依赖,不能存在传递依赖。
及A->B->C(false)
及A->B,A->C(true)

4.BCNF

1
BCNF所有属性都不部分依赖和传递依赖于码