數據庫——MySQ操作管理—增刪改查

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 之後)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章