MySQL(一)

三范式:
1NF: 列不可拆分
2NF: 唯一標(biāo)識
3NF: 引用主鍵
說明: 后一個范式,都是在前一個范式的基礎(chǔ)上建立的

數(shù)據(jù)完整性:
保證數(shù)據(jù)的正確:
1.指定數(shù)據(jù)的類型
數(shù)字:int(整數(shù)),decimal(浮點數(shù))
decimal(5,2):一共有5位,小數(shù)有2位

字符串:char(有限長度),varchar(有限長度),text(大文本)
char(8)  包含8個字符,不夠補空格  "abcd    "
varchar(8) "abcd"
日期:datetime 
布爾:bit

約束:
主鍵(只能有一個)

下載安裝MySQL,下載安裝MySQL Front;

表操作:
1、查看當(dāng)前數(shù)據(jù)庫中所有表:
show tables;
2、創(chuàng)建表:
auto_increment 表示自動增長
create table 表名(列及類型);
create table student( sno int auto_increment primary key not null,sname varchar(10) not null,gender varchar(10) default '男',age int not null,major varchar(10) not null);

create table student( id int auto_increment primary key not null,name varchar(10) not null,gender bit default 1,birthday date);

3、查看表結(jié)構(gòu):
desc 表名;
4、修改表:
alter table 表名 add/change/drop 列名 類型;
alter table student add isDelete bit default 0;
alter table student add birthday date;
5、更改表名稱:
rename table 原表名 to 新表名;
6、查看表的創(chuàng)建語句:
show create table 表名;

數(shù)據(jù)命令:
添加數(shù)據(jù):insert into 表名(列名) values(值),(值)...;
修改數(shù)據(jù):update 表名 set 列1=值1,... where ...; update student set birthday='1990-4-7' where id=2;
update student set gender=0,birthday='2019-2-2' where id=6;
刪除數(shù)據(jù):delete from 表名 where ...; delete from student where id=5;
邏輯刪除:update ....

查詢:
select id,name from student;
select * from student where id>3;
select distinct id from student;
select * from student where id>3 and gender=0;
select * from student where id<4 or isDelete=0;
模糊查詢:
like
%表示任意多個任意字符
_表示一個任意字符
查詢姓黃的學(xué)生
select * from student where name like '黃%';

select * from student where sname is null;   判空
select * from student where sname is not null; 

聚合函數(shù):
    select count(*) from student where name like '黃%';
    count()[有多少行],max(),min(),sum(),avg(),
分組:
    select 列1,列2,聚合...from 表名 group by 列1,列2,列3...
    select gender,count(*) from student group by gender;
分組后的篩選:
    select 列1,列2,聚合...from 表名 group by 列1,列2,列3... having 列1...集合...
    select gender,count(*) from student group by gender having gender=0;
    select gender,count(*) from student group by gender having count(*)>2;
    別名:
    select gender,count(*) as rs from student group by gender having rs>2;
    where是對from后面指定的表進行數(shù)據(jù)篩選,屬于對原始數(shù)據(jù)進行篩選,having是對group by的結(jié)果進行篩選
排序(order by):
    select * from 表名 order by asc|desc,列2 asc|desc,...
    默認(rèn)按照列值從小到大排列
    asc從小到大排列,desc從大到小排列
    select * from student where isdelete=0 and gender=1 order by id desc;
獲取部分行:
    select * from 表名 limit start,count;
    select * from 表名 limit 1,3;
    select * from student where id=(select min(id) from student where isdelete=0);

建立關(guān)系表:
創(chuàng)建表:
create table sc(
id int auto_increment primary key,
stuid int,
subid int,
score int,
foreign key(stuid) references student(sno),
foreign key(subid) references course(cno)
);
外鍵:
1、要設(shè)置外鍵的字段不能為主鍵
2、改建所參考的字段必須為主鍵
3、兩個字段必須具有相同的數(shù)據(jù)類型和約束

外鍵的級聯(lián)操作:
在刪除student表的數(shù)據(jù)時,如果這個ID在SC中已經(jīng)存在,則會拋出異常
推薦使用邏輯刪除,還可以解決這個問題
可以創(chuàng)建表時指定級聯(lián)操作,也可以在創(chuàng)建表后再修改外鍵的級聯(lián)操作

級聯(lián)操作的類型:
restrict(限制):默認(rèn)值,拋異常
cascade(級聯(lián)):如果主表的記錄刪掉,則從表中相關(guān)聯(lián)的記錄都將被刪除
set null:將外鍵設(shè)置為空
no action:什么都不做

select student.sname,course.cname,sc.score from sc inner join student on sc.stuid=student.sno inner join course on sc.subid=course.cno;

鏈接查詢的分類如下:
    表A inner join 表B:表A與表B匹配的行會出現(xiàn)在結(jié)果中
    表A join 表B:表A與表B匹配的行會出現(xiàn)在結(jié)果中,外加表A中獨有的數(shù)據(jù),未對應(yīng)的數(shù)據(jù)用null填充
    表A rightleft  join 表B:表A與表B匹配的行會出現(xiàn)在結(jié)果中,外加表B中獨有的數(shù)據(jù),未對應(yīng)的數(shù)據(jù)用null填充
    在查詢或條件中推薦使用 "表名.列名" 的語法
    如果多個表中列名不重復(fù)可以省略"表名."部分
    如果表的名稱太長,可以在表名后面使用"as 簡寫名" 或 "簡寫名",為表起個零時的簡寫名稱
練習(xí):
查詢學(xué)生的名字和平均分,按順序排列:
select student.sname,avg(sc.score) from sc inner join student on student.sno=sc.stuid group by student.sname order by sno asc;
查詢男生的姓名,總分:
select student.sname,sum(sc.score) from sc inner join student on student.sno=sc.stuid where gender='男' group by student.sname order by sno asc;
查詢科目的名稱和平均分,按順序排列:
select course.cname,avg(sc.score) from course inner join sc on course.cno=sc.subid group by course.cname order by cno asc;

自關(guān)聯(lián):
create table areas(
id int primary key auto_increment not null,
title varchar(20),pid int,
foreign key(pid) references areas(id)
);
從sql文件中導(dǎo)入數(shù)據(jù):
source areas.sql;

視圖(封裝):
create view v_stu_sub_sco as
select student.*,sc.score,course.cname from sc inner join student on sc.stuid=student.sno
inner join course on sc.subid=course.cno;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容