本文是对MySQL数据库基础知识的整理,希望能给您有所帮助 |
详情:启动和登录MySQL——MySQL 5.7数据库
net start mysql57 //表示启动MySQL服务
net stop mysql57 //表示关闭MySQL服务。
若启动失败,以管理员身份运行控制台
Ctrl+alt+delete启动任务管理器,找到“服务”,鼠标右击mysql57,选择启动或停止
(1)在DOS窗口(cmd控制台)中登录MySQL数据库。
C:\Users\Administrator>net start mysql57
请求的服务已经启动。
C:\Users\Administrator>mysql -u root -p
Enter password: ****
Mysql>
(2)MySQL客户端登录数据库。
MySQL Command Line Client
MySQL Command Line Client-Unicode
详情:(怎么创建,增删查改数据库)MySQL 5.7数据库的创建和管理——MySQL 5.7数据库
mysql> create database if not exists 数据库名;
mysql> show databases
mysql> create database 数据库名
-> default character set gb2312
-> default collate gb2312_chinese_ci;
mysql> use mysqltest;
alter {database | schema} 数据库名
[default] character set 字符集
|[default] collate 校对原则;
mysql> alter database 数据库名
-> default character set gb2312
-> collate gb2312_chinese_ci;
mysql> show create database 数据库名;
drop database 数据库名;
详情:怎么创建MySQL数据库表——MySQL 5.7数据库
mysql> show variables like 'InnoDB_data%';
列序号 | 字段名 | 类型 | 取值说明 | 列含义 |
---|---|---|---|---|
1 | studentno | char(11) | 主键 | 学生学号 |
2 | sname | char(8) | 否 | 学生姓名 |
3 | sex | enum (2) | 否 | 性别 |
4 | birthdate | date | 否 | 出生日期 |
5 | entrance | int(3) | 否 | 入学成绩 |
6 | phone | varchar(12) | 否 | 电话 |
7 | varchar(20) | 否 | 电子信箱 |
插入上表代码:
mysql> create table if not exists student
(
studentno char(11) not null comment'学号',
sname char(8) not null comment'姓名',
sex enum('男', '女') default '男' comment'性别',
birthdate date not null comment'出生日期',
entrance int(3) null comment'入学成绩',
phone varchar(12) not null comment'电话',
Email varchar(20) not null comment'电子信箱',
primary key (studentno)
);
如果有两个主键,则:
primary key (studentno , courseno)
create table 新表 as select * from 参照表;
详情:MySQL怎么管理数据库表(查看、修改、删除表,临时表管理)——MySQL 5.7数据库
mysql>use 数据库名;//打开数据库
mysql>show tables;
mysql>describe 表名
mysql> show create table 表名;
alter [ignore] table tbl_name
alter_specification [, alter_specification] ...
alter_specification:
add [column] column_definition [first | after col_name ] //添加字段
|alter [column]col_name{set default literal|drop default} //修改字段默认值
|change [column] old_col_name column_definition //重命名字段
[first|after col_name]
|modify [column]column_definition[first|aftercol_name] //修改字段数据类型
|drop [column] col_name //删除列
|rename [TO] new_tbl_name //对表重命名
|order by col_name //按字段排序
|convert TO character set charset_name[collate collation_name] //将字符集转换为二进制
|[default] character set charset_name [collate collation_name] //修改表的默认字符集
在student表的Email列后面增加一列address。
列序号 | 字段名 | 类型 | 取值说明 | 列含义 |
---|---|---|---|---|
1 | address | varchar(30) | 否 |
mysql>alter table student//表名
->add address varchar(30) not null after 地址;
将sc重命名为se_course
mysql> alter table sc rename to se_course;
修改course表的type字段,默认“必修”
mysql> alter table course
-> modify type enum('必修','选修') default '必修';
mysql> alter table 表名 drop 字段名;
mysql> drop table 表名
mysql> create temporary table 表名
-> (name varchar(10) not null,
-> value integer not null
-> );
mysql> describe 表名
mysql> drop table 表名
详情:MySQL数据库表怎么进行添加数据、更新数据和删除数据操作——MySQL 5.7数据库
insert into
| replace
语句添加数据insert into
添加表数据mysql>insert into student
->(studentno,sname,sex,birthdate,entrance,phone,Email)
-> values ('18122210009','许东山','男','1999/11/5',789,
-> '13623456778','[email protected]');
insert into
添加多行数据mysql> insert into student values
-> ('18122221324','何白露',
-> '女','2000/12/4','879','13178978999','[email protected] '),
-> ('18125111109','敬横江',
-> '男','2000/3/1','789',' 15678945623','[email protected] ');
replace into
向表中插入多行数据。mysql> replace into course values
-> ('c05103','电子技术','必修','64','16','2'),
-> ('c05109','C语言','必修','48','16','2');
load data
语句将TXT文本数据装入数据库表中假设teacher表的数据已放在“d:\teacher.txt”中,现将teaching.txt的数据插入到teacher表中。
mysql>load data local infile "d:\\teacher.txt" into table teacher;
Query OK, 9 rows affected, 8 warnings (0.03 sec)
Records: 9 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from teacher; //输出表的记录
mysql> insert into 表名
-> set studentno=‘19120000111’,courseno=‘c01236’,
-> teacherno=‘t01237’;
mysql> insert into student01 values
-> ('18122221329','何影映','女','2001/12/9','877',
-> '13178978997', '[email protected] ', 'd:\\image\\picture.jpg');
update...set...
update 表名
set col_name1= [,col_name2=expr2 ...] [where子句]
mysql> update score set daily=80
-> where studentno=‘18137221508’ && courseno=‘c08106’;
mysql> update student01 set entrance=entrance*1.08
-> where entrance<700;
delete… from…
delete[low_priority] [quick] [ignore] from tbl_name
[where子句]
[order by子句]
[limit row_count]
(1)删除student01表中入学成绩低于750分记录。
mysql> delete from student01 where entrance <750;
(2)删除student01表中入学成绩最低的2行记录。
mysql> delete from student01 order by entrance limit 2;
详情:表的数据完整性(MySQL怎么为表添加约束)——MySQL 5.7数据库
(1)null(允许空值)
(2)not null(不允许空值)
若在列定义的时加上关键字primary key,就可以定义列的完整性约束主键。如:
mysql>create table if not exists course01
(courseno char(6) not null primary key,
cname char(6) not null,
term int(2) not null);
如:修改表student02的主键,删除原来主键sname,增加studentno 为主键。
mysql> alter table student02 add primary key (sname);
mysql> alter table student02 drop primary key;
mysql> alter table student02 add primary key (studentno);
必须依赖于数据库中已存在的父表的主键;外键可以为空值。
如:用alter table语句在数据库teaching中,为表score添加外键约束。
mysql>alter table score
-> add constraint fk_st_score
-> foreign key(studentno) references student(studentno);
mysql>alter table score
->add constraint fk_cou_score
->foreign key(courseno) references course(courseno);
如:在mysqltest数据库,参照score表创建score1表,其中studentno作为外键,参照student02表中的studentno字段。
mysql> create table if not exists score1
-> (studentno char(11) not null, courseno char(6) not null,
-> daily float(3,1) default 0, final float(3,1) default 0,
-> primary key (studentno , courseno),
-> foreign key(studentno)
-> references student02(studentno)
-> on update cascade
-> on delete cascade);
如:对student02表的birthdate列添加check约束,要求出生日期必须大于1999年12月31日,性别只能是“男”和“女”。
mysql> alter table student02
-> add constraint ch_stu_birth
-> check(birthdate>'1999-12-31');
唯一性约束将保证所有记录中该字段的值不能重复出现。
如:对student02表的Email列添加唯一约束。
mysql> alter table student02 add unique (Email);
mysql> select studentno,sname,email from student02;
mysql> insert into student02
values ('16104444444' , '徐赛克', '男' ,
'2001-01-01', '809', '13102020207', '[email protected]');
ERROR 1062 (23000): Duplicate entry '[email protected]' for key 'Email'
//插入失败,违反唯一性约束的条件
详情:MySQL怎样查询数据(单表查询语句)——MySQL 5.7数据库
select语句的基本的语法格式可归纳如下:
select [all|distinct]selection_list
from table_source [where search_condition]
[group by grouping_columns][with rollup]
[having search_condition]
[order by order_expression [asc|desc]]
[limit count]
select * from 表名;
如:查询student表中的studentno、sname和phone数据。
select studentno,sname,phone from student;
select * from 表1,表2,表3;
mysql> select studentno as '学号',sname as '姓名',
-> phone as '手机号',year(now())-year(birthdate) as '年龄'
-> from student
-> where year(birthdate)>2001;
mysql> select distinct studentno,courseno
-> from score
-> where final>95
-> order by studentno;
where
子句过滤结果集查询符合指定条件的记录数据。
mysql> select studentno,sname,phone
-> from student
-> where entrance>800;
带in关键字的查询。
mysql> select studentno,courseno ,daily ,final
-> from score
-> where studentno in('18135222201','18137221508','19123567897');
带between and
的范围查询。
mysql> select studentno, final
-> from score
-> where courseno='c05109' and daily between 80 and 95;
带like的字符匹配查询
mysql> select sname, birthdate, Email
-> from student
-> where sname like '何%' or sname like '韩%';
用is null关键字查询空值。
mysql> select studentno, courseno,teacherno, score
-> from se_course
-> where score is null;
带and的多条件查询。
mysql> select studentno,courseno,daily,final
-> from score
-> where daily >=90 and final >= 85;
带or
的多条件查询。
mysql> select teacherno,tname, major
-> from teacher
-> where department='计算机学院' and (prof='副教授'or prof='教授');
order by
子句对结果集排序mysql> select studentno 学号,sname 姓名 ,entrance 入学成绩
-> from student
-> where entrance>850
-> order by entrance desc;
mysql> select courseno 课程号,daily *0.2+ final*0.8 as '总评',studentno 学号
-> from score
-> where daily *0.2+ final*0.8>90
-> order by courseno, daily *0.2+ final*0.8 desc;
group by
子句和having
子句的使用使用group by
关键字来分组。
mysql> select studentno 学号, round(avg(daily*0.3+final*0.7),2) as'平均分'
-> from score
-> group by studentno;
group by
关键字与group_concat()
函数一起使用。
mysql> select courseno 课程号,group_concat(studentno) 选课学生学号
-> from score
-> group by courseno ;
group by
关键字与having
一起使用。
mysql> select studentno 学号,sum(daily*0.3+final*0.7) as '总分'
-> from score
-> where final>=75
-> group by studentno
-> having count(*)>=3
-> order by sum(daily*0.3+final*0.7) desc;
limit
限制查询结果的数量limit
是用来限制查询结果的数量的子句。
mysql> select studentno,sname,birthdate,phone
-> from student
-> order by entrance desc
-> limit 3;
使用limit还可以从查询结果的中间部分取值。
mysql> select * from score
-> where final>85
-> order by daily asc
-> limit 2,5;
详情:使用MySQL函数查询数据(聚合函数查询语句)——MySQL 5.7数据库
count()
函数通过查询求18级学生的总数
mysql> select count(studentno) as '18级学生数'
-> from student
-> where substring(studentno,1,2) ='18';
sum()
函数和avg()
函数查询score表中学生的期末总成绩大于270分的学生学号、总成绩及平均成绩。
mysql> select studentno 学号, sum(final) 总分, avg(final) 平均分
-> from score
-> group by studentno
-> having sum(final)>270
-> order by studentno;
查询选修课程号为c05109号课程的期末最高分、最低分及之间相差的分数。
mysql> select max(final) 最高分, min(final) 最低分,
-> max(final)-min(final) as 分差
-> from score
-> where (courseno = 'c05109');
group by
子句与with rollup
一起进行统计查询score表中每一门课的期末平均值和所有成绩的平均值
mysql> select courseno 课程号,avg(final) 课程期末平均分
-> from score
-> group by courseno with rollup;
详情:MySQL使用连接语句查询数据(多表连接查询语句)——MySQL 5.7数据库
查询选修课程号为c05109的学生的学号、姓名和期末成绩。
mysql> select student.studentno,sname,final
-> from student inner join score
-> on student.studentno= score.studentno
-> where score.courseno = 'c05109';
直接通过where子句的复合条件查询
mysql> select student.studentno,sname,final
-> from student,score
-> where student.studentno= score.studentno
-> and score.courseno = 'c05109‘;
left outer join
)。在mysqltest数据库中利用左外连接方式查询学生的学号、姓名、平时成绩和期末成绩。
mysql> select student02.studentno,sname,daily,final
-> from student02 left join score1
-> on student02.studentno=score1.studentno;
right outer join
)。mysql> select teacher.teacherno,tname, major, courseno
-> from teacher right join teach_course
-> on teacher.teacherno = teach_course.teacherno;
显示student 表和score表的笛卡儿积。
mysql> select student.studentno,sname,score.*
-> from student cross join score;
查询18级学生的学号、姓名、课程名、期末成绩及学分。
mysql> select student.studentno,sname,cname,final,round(period/16,1)
-> from score join student on student.studentno=score.studentno
-> join course on score.courseno=course.courseno
-> where substring(student.studentno,1,2)='18';
select_statement union [all] select_statement
将student01和student表的部分查询结果集合并
mysql> select studentno,sname,phone from student01
-> where phone like '%131%'
-> union
-> select studentno,sname,phone from teaching.student
-> where phone like '%132%';
详情:用子查询语句查询MySQL数据库中的数据(子查询语句)——MySQL 5.7数据库
查询学号为18125121107的学生的入学成绩、所有学生的平均入学成绩及该学生成绩与所有学生的平均入学成绩的差。
mysql>select studentno,sname,entrance,
-> (select avg(entrance) from student ) 平均成绩,
-> entrance - (select avg(entrance) from student ) 分差
-> from student
-> where studentno='18125121107';
查询期末成绩高于85分、总评成绩高于90分的学生的学号、课程号和总评成绩。
mysql> select TT.studentno 学号 ,TT.courseno 课程号 ,
-> TT.final*0.8+TT.daily*0.2 总评
-> from (select * from score where final>85) as TT
-> where TT.final*0.8+TT.daily*0.2>90;
查询期末成绩比选修该课程平均期末成绩低的学生的学号、课程号和期末成绩。
mysql> select studentno,courseno,final
-> from score as a
-> where final < (select avg(final)
-> from score as b where a.courseno=b.courseno
-> group by courseno );
获取期末成绩中含有高于93分的学生的学号、姓名、电话和Email。
mysql> select studentno,sname,phone,Email
-> from student where studentno in ( select studentno
-> from score where final>93);
查询student表中是否存在2001年12月12日以后出生的学生,如果存在,输出学生的学号、姓名、生日和电话。
mysql> select studentno,sname,birthdate,phone
-> from student
-> where exists (select * from student
-> where birthdate < '2001-12-12');
查找score表中所有比c05109课程期末成绩都高的学号、姓名、电话和期末成绩。
mysql> select student.studentno,sname, phone,final
-> from score inner join student
-> on score.studentno= student.studentno
-> where final >all
-> (select final from score where courseno= 'c05109');
就是利用一个嵌套在insert、update
或delete
语句的子查询成批的添加、更新和删除表中的数据。
将student表中2001年以后出生的学生记录添加到student02表中。
mysql> insert into mysqltest.student02
-> (select * from student
-> where birthdate>='2001-12-31');
将student表中入学成绩低于800的所有学生的期末成绩增加5%。
mysql> update score
-> set final= final*1.05
-> where studentno in
->(select studentno
->from student
->where entrance <800);
详情:使用MySQL中的正则表达式查询数据(模糊查询)——MySQL 5.7数据库
使用字符“^”可以匹配以特定字符或字符串开头的记录。
mysql> select studentno,sname,birthdate, phone
-> from student
-> where sname regexp '^赵';
使用字符“$”可以匹配以特定字符或字符串结尾的记录。
查询student表中学生电话号码尾数为5的学生部分信息。
mysql> select studentno, sname, phone, Email
-> from student
-> where phone regexp '5$';
用正则表达式来查询时,可以用“.”来替代字符串中的任意一个字符
查询学生姓名sname字段中以“赵”开头,以“江”结束
mysql> select studentno, sname, phone
-> from student
-> where sname regexp '^赵..江$';
正则表达式可以匹配字符串。当表中的记录包含这个字符串时,就可以将该记录查询出来。如果指定多个字符串时,需要用符号“|”隔开。只要匹配这些字符串中的任意一个即可。
查询学生电话号码出现131或132数字的学生信息。
mysql> select studentno, sname, phone, Email
-> from student
-> where phone regexp '131|132';
详情:使用MySQL创建和删除索引——MySQL 5.7数据库
创建索引基本形式如下。
create [unique|fulltext|spatial] index index_name
on table_name (index_col_name,...)
为student表的phone列上建立一个升序普通索引phone_index。
mysql> create index phone_index on student(phone asc);
在course表的cname列上建立一个唯一性索引cname_index。
mysql> create unique index cname_index on course (cname);
在score表的studentno和courseno列上建立一个复合索引sc_index。
mysql> create index sc_index on score(studentno,courseno);
创建表时可以直接创建索引,这种方式最简单、方便。
创建teacher1表的tname字段建立一个唯一性索引tname_index,一个前缀索引dep_index。
mysql> create table if not exists teacher1 (
-> teacherno char(6) not null comment '教师编号',
-> tname char(8) not null comment'教师姓名',
-> major char(10) not null comment '专业',
-> prof char(10) not null comment '职称',
-> department char(16) not null comment '部门',
-> primary key (teacherno),
-> unique index tname_index(tname),
-> index dep_index(department(5))
-> );
在teacher1表上建立teacherno主键索引(假定未创建主键索引),建立tname和prof的复合索引。
mysql> alter table teacher1
-> add primary key(teacherno),
-> add index mark(tname, prof);
如果主键索引已经创建,则会出现如下信息:
ERROR 1068 (42000): Multiple primary key defined
alter [ignore] table table_name
| drop primary key
| drop index index_name
| drop foreign key fk_symbol
mysql> alter table course drop index cname_index;
详情:几个步骤教你利用MySQL Workbench工具创建和管理索引——MySQL 5.7数据库
详情:MySQL视图的应用——MySQL 5.7数据库
使用视图进行查询实际上就是把视图作为数据源,实现查询功能。
mysql> select 学号, 姓名,课程号,成绩
-> from stu_view2
-> where 课程号='c08123' and 成绩 > 80;
创建视图course_avg,统计各门课程平均成绩,并按课程名称降序排列。
mysql> create view course_avg
-> as select cname 课程名, avg(final) 平均成绩
-> from score join course on score.courseno= course.courseno
-> group by cname desc;
通过视图teach_view1,对基表teacher进行插入、更新和删除数据的操作。
mysql> insert into teach_view1(teacherno,tname,major,prof,department)
-> values (‘t06027’ , ‘陶期年’ , ‘纳米技术’ , ‘教授’ , ‘材料学院’ );
mysql> update teach_view1 set prof = ‘副教授’ where teacherno = ‘t07019’;
mysql> delete from teach_view1 where teacherno = ‘t08017’;
视图stu_score1依赖于源表student、course和score等3张表,包括studentno、sname、 phone、cname和final等5个字段,通过stu_score1修改基本表student中的学号为18125121107的电话号码。
mysql> update stu_score1 set phone='132123456777'
-> where studentno =‘18125121107’;
在“查询编辑器”中输入以下程序,创建V_dept视图。
mysql> create view V_dept
-> AS
-> select teacherno,tname,major,prof, department
-> from teacher
-> where department ='计算机学院'
-> with check option;
通过视图V_ dept 向基表teacher中插入数据。
mysql> insert into V_dept
-> values(‘t08017’,‘时观’,‘金融管理’,‘副教授’,‘计算机学院’);
通过视图V_sex向基表teacher中插入数据行(‘t08037’,‘时刻’,‘软件技术’,‘讲师’,‘软件学院’)。
mysql> insert into V_dept
-> values('t08037','时刻','软件技术','讲师','软件学院');
详情:利用MySQL Workbench 工具创建和管理视图——MySQL 5.7数据库