数据库——MySQL操作管理—增删改查

MySQL使用管理工具:Navicat for MySQL

  • 用途:新建用户,新建查询。
  • 例:GRANT ALL PRIVILEGES ON *.* TO "LP"@"%" IDENTIFIED BY "123456"
  • 释:授权  所有  权限 在 所有数据库 的 所有 表 给 从任何地方 登陆的IP 用户 通过 123456密码

MySQL数据库:

  • 1.数据以表格的形式出现
  • 2.每行为各种记录名称
  • 3.每列为记录名称所对应的数据域
  • 4.许多的行和列组成一张表单
  • 5.若干的表单组成database

MySQL的关键词:

  • 数据库:
  • 数据库是一些关联表的集合。
  • 数据表:
  • 表是数据的矩阵。在一个数据库中的表看起来像一个简单的电子表格。
  • 列:
  • 一列(数据元素) 包含了相同的数据, 例如邮政编码的数据。
  • 行:
  • 一行(=元组,或记录)是一组相关的数据,例如一条用户订阅的数据。
  • 主键:
  • 主键是唯一的。一个数据表中只能包含一个主键。使用主键来查询数据。
  • 索引:
  • 使用索引可快速访问数据库表中的特定信息。索引是对数据库表中一列或多列的值进行排序的一种结构。类似于书籍的目录。

 mysql增删改查

  • 1.查看数据库:
  • show databases;
  • 2.进入数据库:
  • use mysql;
  • 3. 创建数据库:
  • CREATE DATABASE lp DEFAULT CHARSET utf8;
  • 4.删除数据库:
  • DROP DATABASE lp;
  • 5.创建表:
  • 注意:数【int(5:长度为0-999999)】   字符串【char varchar(255:name最大字节长度255) text不写长度】
  • CREATE TABLE tb_users(id int(5),name varchar(255),sex varchar(5),age int(3));
  • 6.删除表 :
  • DROP TABLE tb_users;
  • 7.查看表:
  • SHOW TABLES;
  • 8.查看表结构:
  • 注意:(null允许为空  Key约束 Default默认值 Extra备注)
  • SHOW COLUMNS FROM tb_users;
  • 9.调整命令行编码:
  • 注意:(中文编码:数据库存数据编码:utf8     数据库命令行传输数据编码改不了     命令行显示数据编码:gbk)
  • set names gbk;
  • 10.表中插入一条数据:
  • INSERT INTO tb_users(id,name,sex,age)VALUES(1,"潘廷宇","男",23);
  • 11.表中插入多条数据:
  • INSERT INTO tb_users(id,name,sex,age)VALUES(1,"张三","男",23),(2,"张三","男",23),(3,"张三","男",23);
  • 12.查询所有数据:
  • SELECT *from tb_users;
  • 13.插入指定列数据:
  • INSERT INTO tb_users(name,sex)VALUES("潘廷宇","男");
  • 14.省略列名:
  • INSERT INTO tb_users(name,sex,age)VALUES("潘廷宇","男",24);
  • 15.删除指定行数据:
  • DELETE FROM tb_users WHERE id=1;
  • 16.清空数据表:
  • delete from tb_users;
  • 17.更改数据:
  • update tb_users set age=age+5 where id=3;

例:

数据表

  • 表:
  • create table Student    -- 学生表
  • (
  • Sno char(3) NOT NULL Primary key ,    -- 学号 ,设为主键,不允许空值   
  • Sname char(8) NOT NULL,        -- 学生姓名
  • Ssex char(2)NOT NULL,        -- 学生性别
  • Sbirthday datetime,     -- 学生出生年月
  • Class char(5)         -- 学生所在班级
  • );
  • create table Teacher        -- 教师表
  • (
  • Tno char(3)NOT NULL primary key,        -- 教工编号设为主键
  • Tname char(4)NOT NULL,        -- 教工姓名
  • Tsex char(2)NOT NULL,        -- 教工性别
  • Tbirthday datetime,        -- 教工出生年月
  • Prof char(6),        -- 职称
  • Depart varchar(10)NOT NULL        -- 教工所在部门
  • );
  • create table Course        -- 课程表
  • (
  •     Cno char(5) NOT NULL Primary key ,        -- 课程号设为主键
  •     Cname varchar(10) NOT NULL,        -- 课程名称
  •     Tno char(3) NOT NULL references Teacher(Tno)        -- 教工编号设为外键
  • );
  • create table Score    -- 成绩表
  • (
  • Sno char(3) NOT NULL references Student(Sno),    -- 学号设为外码
  • Cno char(5) NOT NULL references Course(Cno),    -- 课程号设为外码
  • Degree Decimal(4,1),    -- 成绩
  • primary key(Sno,Cno)    -- 学号和课程号设为联合主键
  • );
  • 数据:
  • insert into Student values(108,'曾华','男','1977-09-01','95033');
  • insert into Student values(105,'匡明','男','1975-10-02','95031');
  • insert into Student values(107,'王丽','女','1976-01-23','95033');
  • insert into Student values(101,'李军','男','1976-02-20','95033');
  • insert into Student values(109,'王芳','女','1975-02-10','95031');
  • insert into Student values(103,'陆君','男','1974-06-03','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 Teacher values(831,'刘冰','女','1977-08-14','助教','电子工程系');
  • insert into Course values('3-105','计算机导论',825);
  • insert into Course values('3-245','操作系统',804);
  • insert into Course values('6-166','数字电路',856);
  • insert into Course values('9-888','高等数学',831);
  • insert into Score values(103,'3-245',86);
  • insert into Score values(105,'3-245',75);
  • insert into Score values(109,'3-245',68);
  • insert into Score values(103,'3-105',92);
  • insert into Score values(105,'3-105',88);
  • insert into Score values(109,'3-105',76);
  • insert into Score values(101,'3-105',64);
  • insert into Score values(107,'3-105',91);
  • insert into Score values(108,'3-105',78);
  • insert into Score values(101,'6-166',85);
  • insert into Score values(107,'6-166',79);
  • insert into Score values(108,'6-166',81);

以上面数据表为例进行查询。

例:

    查询方式

  • 1.查询studebt表中的所有数据:
  • SELECT *from Student;
  • 2.查询studebt表中的姓名和性别:
  • SELECT sname,ssex from Student;
  • 3.查询studebt表中编号大于105的人的姓名和生日:
  • select sname,sbirthday from student where sno>105;
  • 4.查询studebt表中95033班级的所有女孩的信息:
  • select *from student where class=95033 and(或&&) ssex="女";
  • 5.查询studebt表编号为101或103或105的学生:
  •  select *from student where sno=101 ||(或or) sno=103 || sno=105;
  • select *from student where sno in (101,103,105);
  • 6.查询studebt表编号不为101或103或105的学生:
  • select *from student where sno not in (101,103,105);
  • 7.查询studebt表编号在101-106的学生:between 
  • select *from student where sno >=101 && sno <=106;(<>不等于、<=大于等于、>=小于等于;)
  • select *from student where sno between 101 and 106;
  • 8.查询studebt表编号不在101-106的学生:
  • select *from student where sno not between 101 and 106;
  • 9.将数据表信息按照编号进行降序排序:(order by ... desc降序 asc升序)
  • select *from student order by sno desc;
  • 10.将数据表信息按照年龄进行降序排序:(年龄降序=生日升序)
  • select *from student order by sbirthday asc;
  • 11.查询数据表信息的前三条
  • select *from student limit 0,3;
  • 12.查询数据信息的第2到5条
  • select *from student limit 1,4;
  • 13.查询学生中第二大的那个人的信息(排序order by sbirthday asc) 
  • select *from student order by sbirthday asc limit 1,1;
  • 14.查询所有人的平均分是多少(平均数avg)
  • select avg(Degree) from score;
  • 15.查询每个人的平均分是多少(按照个人编号进行分组 group by)
  • select sno,avg(degree) from score group by sno;
  • 16.查询一共有多少条分数记录(统计数量count,统计最大值max)
  • select count(sno) from score;
  • 17.查单科成绩有大于85的人的平均分(需要对分组进行过滤having 用在,)
  • selet avg(Degree) from score group by sno having max(degree)>85;
  • 18.过滤重复值:distinct
        
  • select distinct SNO as cd from score ;
  • 19.查询选修人数大于3人的平均分
  • select avg(Degree) from score group by cno having count(sno)>3;
  • 20.当成绩大于80时,成绩为A, 查每个学科得了A 的学生的平均分
  • select avg(degree)from score where degree>80 group by cno;
  • 21.当成绩大于80时,成绩为A, 查每个学科不少于2个得了A 的学生的平均分
  • select cno,avg(degree)from score where degree>80 group by cno having count(degree)>=2;
  • 22.当成绩大于80时成绩记为A查得A不少于两个的学科的平均分:(子查询in())
  • select avg(degree) from score where cno in ( select cno from scorewhere degree >80 group by cno having count(cno)>1) group by cno;
  • 23.求单科成绩最高的那个科目的平均成绩
  • 1. 求最高成绩所在的学科:
  • select cno from score where  degree = (select max(degree) from score);
  • 或select cno from score order by degree limit 0,1;
  • 2.求这个学科所有成绩的平均分:
  • select avg(degree) from score where cno  = ( select cno from score order by degree limit 0,1);
  • 24.求选修课程人数最多的课程的所有成绩
  • select * from score where cno = (select cno from score group by cno order by count(*) desc limit 1);
  • 25.求每个学生每个科目的成绩(多表联查)
  • select student.sname,course.Cname,score.Degree from course,student,score where score.cno=course.cno and student.sno=score.sno;
  • 26.求所有姓王的人的成绩
  • select student.sname,course.Cname,score.Degree from course,student,score where score.cno=course.cno and student.sno=score.sno and sname like"王%";
  • 27.求个人最高分和最低分差距最大的所有成绩
  • 1.查最高分和最低分差距最大的人
  • select sno,max(degree)-min(degree) as 分差 from score group by sno asc limit 1;
  • 2.查这个人的所有成绩
  • select sname,degree from student,score where student.sno = score.sno and student.sno =(select sno from (select sno ,max(degree)-min(degree) as fc  from score group by sno order by fc desc limit 1) as t1
  • 28.求带学生最多的老师的职称是什么
  • 1.先查学生选修人数最多的课程
  • select  cno  from score group by cno order by count(*)  desc limit 1;
  • 2.通过课程编号查询老师职称
  • select prof from teacher,course where teacher.Tno = course.Tno and course.cno =(select  cno  from score group by cno order by count(*)  desc limit 1);

where和having区别

  • where是判断数据从磁盘读入内存的时候,having是判断分组统计之前的所有数据
  • where不能使用统计函数count(),having能使用统计函数count()
  • where不能使用字段别名,having能使用字段别名(having可以用在group by 之后)
发布了36 篇原创文章 · 获赞 130 · 访问量 2065

猜你喜欢

转载自blog.csdn.net/cldimd/article/details/104953867