MySQL数据库知识整理

本文是对MySQL数据库基础知识的整理,希望能给您有所帮助

文章目录

  • 一、启动和登录MySQL
    • 1.启动MySQL服务
      • (1)cmd控制台启动。
      • (2)手动启动
    • 2.登录MySQL
  • 二、MySQL数据库的基本操作
    • 1.创建数据库
    • 2.查看数据库
    • 3.在创建数据库的同时填写数据库的数据类型
    • 4.管理数据库
      • (1)打开数据库。
      • (2)修改数据库。
      • (3)显示数据库结构。
      • (4)删除数据库 。
  • 三、创建MySQL数据库表
    • 1.查看数据库的表空间
    • 2.利用SQL语句创建数据表
    • 3.参照某表结构创建新表
  • 四、管理数据库表
    • 1.查看表
      • (1)查看数据库中已经创建的表。
      • (2)查看表的基本结构。
      • (3)查看表详细结构
    • 2.修改表
      • (1)添加字段
      • (2)修改表名。
      • (3)修改字段的数据类型。
      • (4)删除字段。
    • 3.删除数据库表
    • 4.临时表
      • (1)创建临时表
      • (2)查看临时表
      • (3)删除临时表
  • 五、MySQL数据库表的数据管理
    • 1.使用`insert into`| `replace`语句添加数据
      • (1)利用`insert into`添加表数据
      • (2)利用`insert into`添加多行数据
      • (3)利用`replace into`向表中插入多行数据。
    • 2.利用`load data`语句将TXT文本数据装入数据库表中
    • 3.使用set子句插入数据
    • 4.向表中插入图片数据。
    • 5.修改表数据`update...set...`
      • (1)将学号为18137221508学生的课程号为 c08106的平时成绩daily修改为80分。
      • (2) 将课程student01表中低于700分的入学成绩增加8%。
    • 6.删除表内数据`delete… from…`
  • 六、为MySQL表添加约束
    • 1.非空约束
    • 2.为MySQL表添加主键约束
      • (1)创建表时定义完整性约束
      • (2)修改表的主键
    • 3.外键约束
      • (1)参照完整性
      • (2)设置外键的原则:
      • (3)对已有的表添加外键
      • (4)在创建表时创建外键
    • 4.检查约束
    • 5.唯一性约束
  • 七、MySQL查询语句
    • 1.单表查询语句
      • (1)使用select语句查询一个数据表。
      • (2)查询表中的指定列
      • (3)从多个表中获取数据。
      • (4)select项的原名 as 别名
      • (5)使用谓词过滤记录
      • (6)使用`where`子句过滤结果集
      • (7)使用`order by`子句对结果集排序
      • (8)`group by`子句和`having`子句的使用
      • (9)用`limit`限制查询结果的数量
    • 2.聚合函数查询语句
      • (1)`count()`函数
      • (2)`sum()`函数和`avg()`函数
      • (3)max()函数和min()函数
      • (4)利用`group by`子句与`with rollup`一起进行统计
    • 3.多表连接查询语句
      • (1)内连接
      • (2)左外连接(`left outer join`)。
      • (3)右外连接(`right outer join`)。
      • (4)交叉连接
      • (5)连接多个表
      • (6)合并多个结果集
    • 4.子查询
      • (1)利用子查询做表达式
      • (2)利用子查询生成派生表
      • (3)where子句中的子查询
        • 带比较运算符的子查询。
        • 带in关键字的子查询 。
        • 带exists关键字的子查询。
        • 对比较运算进行限制的子查询。
      • (4)利用子查询插入、更新与删除数据
        • 利用子查询修改表数据。
        • 利用子查询插入纪录。
        • 利用子查询更新数据。
    • 5.使用正则表达式进行模糊查询
      • (1)查询以特定字符或字符串开头的记录。
      • (2)查询以特定字符或字符串结尾的记录。
      • (3)用符号“.”来替代字符串中的任意一个字符。
      • (4)匹配指定字符串。
  • 八、索引
    • 1.利用create index语句创建索引。
      • (1)随机索引
      • (2)唯一性索引
      • (3)复合索引
      • (4)创建表时创建索引。
    • 2.通过alter table语句创建索引
    • 3.删除索引
      • (1)利用alter table语句删除索引的语法格式如下:
      • (2)利用alter table语句同样可以删除前面表中创建的索引
      • (3)利用MySQL Workbench删除索引。
  • 九、视图
    • 1.使用视图管理表数据
      • (1)使用视图进行查询。
      • (2)使用视图进行统计计算
      • (3)使用视图修改基本表数据。
    • 2.检查视图的应用。
    • 3.利用MySQL Workbench 工具创建和管理视图

一、启动和登录MySQL

详情:启动和登录MySQL——MySQL 5.7数据库

1.启动MySQL服务

(1)cmd控制台启动。

net start mysql57 //表示启动MySQL服务
net stop mysql57  //表示关闭MySQL服务。

若启动失败,以管理员身份运行控制台

(2)手动启动

Ctrl+alt+delete启动任务管理器,找到“服务”,鼠标右击mysql57,选择启动或停止

2.登录MySQL

(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数据库的基本操作

详情:(怎么创建,增删查改数据库)MySQL 5.7数据库的创建和管理——MySQL 5.7数据库

1.创建数据库

mysql> create database if not exists 数据库名;

2.查看数据库

mysql> show databases

3.在创建数据库的同时填写数据库的数据类型

mysql> create database 数据库名
	-> default character set gb2312
	-> default collate gb2312_chinese_ci;

4.管理数据库

(1)打开数据库。

mysql> use mysqltest;

(2)修改数据库。

alter {database | schema} 数据库名
[default] character set 字符集
|[default] collate 校对原则;
 mysql> alter database 数据库名
	 -> default character set gb2312
	 -> collate gb2312_chinese_ci;

(3)显示数据库结构。

mysql> show create database 数据库名;

(4)删除数据库 。

drop database  数据库名;

三、创建MySQL数据库表

详情:怎么创建MySQL数据库表——MySQL 5.7数据库

1.查看数据库的表空间

mysql> show variables like 'InnoDB_data%';

2.利用SQL语句创建数据表

列序号 字段名 类型 取值说明 列含义
1 studentno char(11) 主键 学生学号
2 sname char(8) 学生姓名
3 sex enum (2) 性别
4 birthdate date 出生日期
5 entrance int(3) 入学成绩
6 phone varchar(12) 电话
7 Email 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)

3.参照某表结构创建新表

create  table  新表 as select *  from 参照表;

四、管理数据库表

详情:MySQL怎么管理数据库表(查看、修改、删除表,临时表管理)——MySQL 5.7数据库

1.查看表

(1)查看数据库中已经创建的表。

mysql>use 数据库名;//打开数据库
mysql>show tables;

(2)查看表的基本结构。

mysql>describe 表名

(3)查看表详细结构

mysql> show create table 表名;

2.修改表

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] //修改表的默认字符集

(1)添加字段

在student表的Email列后面增加一列address。

列序号 字段名 类型 取值说明 列含义
1 address varchar(30) Email
mysql>alter table student//表名
	->add address varchar(30) not null after 地址;

(2)修改表名。

将sc重命名为se_course

mysql> alter table sc rename to se_course;

(3)修改字段的数据类型。

修改course表的type字段,默认“必修”

mysql> alter table course  
    -> modify type enum('必修','选修') default '必修';

(4)删除字段。

mysql> alter table 表名 drop 字段名; 

3.删除数据库表

mysql> drop table 表名

4.临时表

(1)创建临时表

mysql> create temporary table 表名
	-> (name varchar(10) not null,  
	-> value integer not null  
	->  ); 

(2)查看临时表

mysql> describe 表名

(3)删除临时表

mysql> drop table 表名

五、MySQL数据库表的数据管理

详情:MySQL数据库表怎么进行添加数据、更新数据和删除数据操作——MySQL 5.7数据库

1.使用insert into| replace语句添加数据

(1)利用insert into添加表数据

mysql>insert into student
    ->(studentno,sname,sex,birthdate,entrance,phone,Email)
    -> values ('18122210009','许东山','男','1999/11/5',789,
    -> '13623456778','[email protected]');

(2)利用insert into添加多行数据

mysql> insert into student values
-> ('18122221324','何白露',
-> '女','2000/12/4','879','13178978999','[email protected] '),
-> ('18125111109','敬横江',
-> '男','2000/3/1','789','  15678945623','[email protected] ');

(3)利用replace into向表中插入多行数据。

mysql> replace into course values
-> ('c05103','电子技术','必修','64','16','2'),
-> ('c05109','C语言','必修','48','16','2');

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;                     //输出表的记录

3.使用set子句插入数据

mysql> insert into 表名
	-> set studentno=19120000111,courseno=‘c01236’,
    -> teacherno=‘t01237’;

4.向表中插入图片数据。

mysql> insert into student01  values
    -> ('18122221329','何影映','女','2001/12/9','877',
    -> '13178978997', '[email protected] ', 'd:\\image\\picture.jpg');

5.修改表数据update...set...

update 表名   
set col_name1= [,col_name2=expr2 ...] [where子句] 

(1)将学号为18137221508学生的课程号为 c08106的平时成绩daily修改为80分。

mysql> update score set daily=80
    -> where studentno=18137221508&& courseno=‘c08106’;

(2) 将课程student01表中低于700分的入学成绩增加8%。

mysql> update student01 set entrance=entrance*1.08  
    -> where  entrance<700;

6.删除表内数据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怎么为表添加约束)——MySQL 5.7数据库

1.非空约束

(1)null(允许空值)
(2)not null(不允许空值)

2.为MySQL表添加主键约束

(1)创建表时定义完整性约束

若在列定义的时加上关键字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); 

(2)修改表的主键

如:修改表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);

3.外键约束

(1)参照完整性

(2)设置外键的原则:

必须依赖于数据库中已存在的父表的主键;外键可以为空值。

(3)对已有的表添加外键

如:用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);

(4)在创建表时创建外键

如:在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);

4.检查约束

如:对student02表的birthdate列添加check约束,要求出生日期必须大于1999年12月31日,性别只能是“男”和“女”。

mysql> alter table student02
  	-> add constraint ch_stu_birth
  	-> check(birthdate>'1999-12-31');

5.唯一性约束

唯一性约束将保证所有记录中该字段的值不能重复出现。
如:对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查询语句

1.单表查询语句

详情: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]

(1)使用select语句查询一个数据表。

select * from  表名;

(2)查询表中的指定列

如:查询student表中的studentno、sname和phone数据。

select studentno,sname,phone from student; 

(3)从多个表中获取数据。

select * from1,表2,表3;

(4)select项的原名 as 别名

mysql> select  studentno as '学号',sname as '姓名',
	-> phone as '手机号',year(now())-year(birthdate) as  '年龄'
	-> from  student
	-> where   year(birthdate)>2001;

(5)使用谓词过滤记录

mysql> select  distinct studentno,courseno
    -> from   score
    -> where final>95
    -> order  by studentno;

(6)使用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='教授');

(7)使用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;

(8)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;

(9)用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;

2.聚合函数查询语句

详情:使用MySQL函数查询数据(聚合函数查询语句)——MySQL 5.7数据库

(1)count()函数

通过查询求18级学生的总数

mysql> select count(studentno) as '18级学生数'
	-> from student
	-> where substring(studentno,1,2) ='18';

(2)sum()函数和avg()函数

查询score表中学生的期末总成绩大于270分的学生学号、总成绩及平均成绩。

mysql> select studentno 学号, sum(final) 总分, avg(final) 平均分
	-> from score
	-> group by studentno
	-> having sum(final)>270
	-> order by studentno;

(3)max()函数和min()函数

查询选修课程号为c05109号课程的期末最高分、最低分及之间相差的分数。

mysql> select max(final) 最高分, min(final) 最低分, 
	-> max(final)-min(final) as 分差
	-> from score
	-> where (courseno = 'c05109');

(4)利用group by子句与with rollup一起进行统计

查询score表中每一门课的期末平均值和所有成绩的平均值

mysql> select courseno 课程号,avg(final) 课程期末平均分
	-> from score
	-> group by courseno with  rollup;

3.多表连接查询语句

详情:MySQL使用连接语句查询数据(多表连接查询语句)——MySQL 5.7数据库

(1)内连接

查询选修课程号为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‘;

(2)左外连接(left outer join)。

在mysqltest数据库中利用左外连接方式查询学生的学号、姓名、平时成绩和期末成绩。

mysql> select student02.studentno,sname,daily,final
	-> from student02 left join score1
	-> on student02.studentno=score1.studentno;

(3)右外连接(right outer join)。

mysql> select teacher.teacherno,tname, major, courseno
	-> from teacher right join teach_course
	-> on teacher.teacherno = teach_course.teacherno;

(4)交叉连接

显示student 表和score表的笛卡儿积。

mysql> select student.studentno,sname,score.*
    -> from student cross join score;

(5)连接多个表

查询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';

(6)合并多个结果集

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%';

4.子查询

详情:用子查询语句查询MySQL数据库中的数据(子查询语句)——MySQL 5.7数据库

(1)利用子查询做表达式

查询学号为18125121107的学生的入学成绩、所有学生的平均入学成绩及该学生成绩与所有学生的平均入学成绩的差。

mysql>select studentno,sname,entrance,
	-> (select avg(entrance) from student ) 平均成绩,
	-> entrance - (select avg(entrance) from student ) 分差
	-> from student
	-> where studentno='18125121107'; 

(2)利用子查询生成派生表

查询期末成绩高于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;

(3)where子句中的子查询

带比较运算符的子查询。

查询期末成绩比选修该课程平均期末成绩低的学生的学号、课程号和期末成绩。

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 );

带in关键字的子查询 。

获取期末成绩中含有高于93分的学生的学号、姓名、电话和Email。

mysql> select studentno,sname,phone,Email
	-> from student where studentno in ( select studentno
	-> from score where final>93);

带exists关键字的子查询。

查询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');

(4)利用子查询插入、更新与删除数据

利用子查询修改表数据。

就是利用一个嵌套在insert、updatedelete语句的子查询成批的添加、更新和删除表中的数据。

利用子查询插入纪录。

将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);

5.使用正则表达式进行模糊查询

详情:使用MySQL中的正则表达式查询数据(模糊查询)——MySQL 5.7数据库

(1)查询以特定字符或字符串开头的记录。

使用字符“^”可以匹配以特定字符或字符串开头的记录。

mysql> select studentno,sname,birthdate, phone 
	-> from student
	-> where sname  regexp '^赵';

(2)查询以特定字符或字符串结尾的记录。

使用字符“$”可以匹配以特定字符或字符串结尾的记录。
查询student表中学生电话号码尾数为5的学生部分信息。

mysql> select  studentno, sname, phone, Email
	-> from student
	-> where phone regexp '5$';

(3)用符号“.”来替代字符串中的任意一个字符。

用正则表达式来查询时,可以用“.”来替代字符串中的任意一个字符
查询学生姓名sname字段中以“赵”开头,以“江”结束

mysql> select studentno, sname, phone 
	-> from student 
	-> where  sname regexp '^赵..江$';

(4)匹配指定字符串。

正则表达式可以匹配字符串。当表中的记录包含这个字符串时,就可以将该记录查询出来。如果指定多个字符串时,需要用符号“|”隔开。只要匹配这些字符串中的任意一个即可。
查询学生电话号码出现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,...)

1.利用create index语句创建索引。

(1)随机索引

为student表的phone列上建立一个升序普通索引phone_index。

mysql> create index phone_index on student(phone asc);

(2)唯一性索引

在course表的cname列上建立一个唯一性索引cname_index。

mysql> create unique index cname_index on course (cname);

(3)复合索引

在score表的studentno和courseno列上建立一个复合索引sc_index。

mysql> create index sc_index on score(studentno,courseno);

(4)创建表时创建索引。

创建表时可以直接创建索引,这种方式最简单、方便。

创建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))
	->  );

2.通过alter table语句创建索引

在teacher1表上建立teacherno主键索引(假定未创建主键索引),建立tname和prof的复合索引。

mysql> alter table teacher1
	-> add primary key(teacherno),
	-> add index mark(tname, prof);

如果主键索引已经创建,则会出现如下信息:

ERROR 1068 (42000): Multiple primary key defined

3.删除索引

(1)利用alter table语句删除索引的语法格式如下:

alter [ignore] table table_name 
| drop primary key	
| drop index index_name	
| drop foreign key fk_symbol

(2)利用alter table语句同样可以删除前面表中创建的索引

mysql> alter table course drop index cname_index; 

(3)利用MySQL Workbench删除索引。

详情:几个步骤教你利用MySQL Workbench工具创建和管理索引——MySQL 5.7数据库

九、视图

详情:MySQL视图的应用——MySQL 5.7数据库

1.使用视图管理表数据

(1)使用视图进行查询。

使用视图进行查询实际上就是把视图作为数据源,实现查询功能。

mysql> select 学号, 姓名,课程号,成绩
	-> from stu_view2
	-> where 课程号='c08123' and 成绩 > 80;

(2)使用视图进行统计计算

创建视图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;

(3)使用视图修改基本表数据。

通过视图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;

2.检查视图的应用。

在“查询编辑器”中输入以下程序,创建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','时刻','软件技术','讲师','软件学院');

3.利用MySQL Workbench 工具创建和管理视图

详情:利用MySQL Workbench 工具创建和管理视图——MySQL 5.7数据库

你可能感兴趣的:(Mysql数据库,mysql,数据库)