mysql外键约束分数_MySQL提高(外键约束)
外鍵約束
1.條件語句的寫法
在sql中可以通過'where 條件語句' 來對操作對象進行篩選 -篩選
a.比較運算符:=,<>,,<=,>=
注意:判斷一個字段的值是否為空不用使用=和<>,而是使用'is null'和'is not null'
select number from t_course where title is null; 判斷是否為null
select number from t_course where tirle=''; --判斷是否是空串
b.邏輯運算:and,or,not
c.where 字段名 between 值一 and 值二; -- 篩選指定的字段的值在值一和值二之間的數據
select title,birth from t_course where birth between '1990-1-1' and '1999-12-31';
d.where 字段名 in 集合 --篩選出字段值是集合中的元素;(集合是使用括號括起來里面多個值)
select * from t_course where title in ('歷史','高數','語文');
e.like操作,上一篇文章
2.數據類型
varchar(size):不定長字符串,size決定的是最大長度
char(size):定常字符
text: 不限長度
int/tinyint(-128~127):
float(size,d)/double(size,d) - 這的size環繞d的值都有約束效果
bit: 只有0和1兩個值
date/datetime/time: 值可以是時間函數的結果,也可以是時間字符串,計算或者是比較的時候內部是按時間處理的
3.去重
select distinct credits from t_course;
添加約束
1.創建表的時候添加約束
create table if not exists t_college
(
collid int,
collname varchar(20) not null, -- 創建表的時候添加約束
website varchar(1024),
intro varchar(200),
primary key(collid)
)
2.通過添加約束索引的方式添加約束
alter table 表名 add constraint 索引名 約束 (字段名);
說明:索引名 - 自己隨便命名約束(字段名);
說明:索引名 - 自己隨便命名,用來指向當前添加的約束;約束 - 添加的約束
alter tabler t_college add constraint con_website unique (website);
給t_college表中的website添加unique的約束,約束索引為con_website
b.刪除約束
alter table 表名 drop index 約束索引;用來指向當前添加的約束
alter table t_college drop index con_website;
外鍵和E.R圖
1.什么是外鍵:表中的某個字段的值是根據其他表中主鍵的值來確定的,那么這個字段就是外鍵
1.1多對一的外鍵的添加:將外鍵添加到多的一方對應的表中。
一對一的外鍵添加:將外鍵隨便添加到哪一方,同時添加值唯一約束
多對多的外鍵添加:關系型數據庫中,兩張沒法實現多對多的關系,需要一個中鍵表(中間表有兩個表的外鍵分別參照多對多的兩個表的主鍵)
1.2怎么添加轉眼間
a.外鍵對應的字段
alter table t_student add column collid int;
b.給設計好的外鍵對應的字段添加外鍵約束
alter table 表1 add constraint fk_collid_stu(##索引名) foreign key(表1字段名1) references 表2(字段2);
將表1中的字段1設置為外鍵,并且讓這個外鍵的值參照表2中的字段2
c.刪除外鍵約束
alter table 表名 drop foreign key 外鍵索引名;
可以刪除外鍵約束,但是外鍵索引還在,需要把額外的索引刪除
注意:外鍵約束直接刪除約束的索引無效,必須先將約束刪掉,然后再刪除索引
alter table tb_student drop foreign key fk_collid_stu;
alter table tb_student drop index fk_collid_stu;
d.多對多的外鍵約束
例子
-- =============1.學生表===============
CREATE TABLE IF NOT EXISTS tb_student
(
stuid int not NULL auto_increment,
stuname VARCHAR(20) not NULL,
tel CHAR(11) not NULL,
birth date DEFAULT '2019-10-21',
addr VARCHAR(100),
PRIMARY key(stuid)
);
-- ===============2.課程表================
CREATE TABLE IF NOT EXISTS tb_course
(
couid int NOT NULL auto_increment,
couname VARCHAR(20) NOT NULL,
startdate date NOT NULL DEFAULT '2019-10-21',
intro VARCHAR(200),
credit int NOT NULL,
PRIMARY key(couid)
);
-- =================3.學院表====================
CREATE TABLE IF NOT EXISTS tb_college
(
collid int auto_increment,
website VARCHAR(200) UNIQUE,
collname VARCHAR(50),
PRIMARY KEY(collid)
);
-- =================4.老師表====================
CREATE TABLE IF NOT EXISTS tb_teacher
(
teaid int NOT NULL auto_increment,
teaname VARCHAR(20) NOT NULL,
teaage INT,
tel char(11),
PRIMARY KEY(teaid)
);
1.什么是外鍵:表中的某個字段的值是根據其他表中主鍵的值來確定的.那么這個字段就是外鍵
-- 1.1多對一的外鍵的添加:將外鍵添加到多的一方對應的表中
-- 1.2怎么添加外鍵:
-- a.外鍵對應的字段
alter table t_student add column collid int;
-- b.給設計好的外鍵對應的字段添加外鍵約束
-- alter tabler 表1 add constraint fk_collid_stu(##索引名) FOREIGN KEY (表1字段名1) references 表2(字段2);
-- 將表1中的字段1設置為外鍵,并且讓這個外鍵的值參照表2中的字段2
-- c.刪除外鍵約束
-- alter table 表名 drop foreign key 外鍵索引名; - 可以刪除外鍵約束,但是外鍵索引還在,需要額外的把索引刪掉
-- 注意:外鍵約束直接刪除約束的索引無效,必須先將約束刪掉,然后再刪除索引
alter table tb_student drop foreign key fk_collid_stu;
alter table tb_stubent drop index fk_collid_stu;
-- 一對多
alter table tb_student add column collid int;
alter table tb_student add CONSTRAINT fk_collid_stu FOREIGN key (collid) REFERENCES tb_college (collid);
alter table tb_teacher add column collid int;
ALTER TABLE tb_teacher add CONSTRAINT fk_collid_tea FOREIGN key (collid) REFERENCES tb_college (collid);
alter table tb_course add column teaid int;
alter table tb_course add CONSTRAINT fk_teaid_cou FOREIGN key (teaid) REFERENCES tb_teacher (teaid);
-- 多對多
create table if not EXISTS tb_score
(
scoreid int not null auto_increment,
mark FLOAT(4,1) comment '分數',
stuid int comment '學生外鍵',
couid int comment '課程外鍵',
PRIMARY KEY (scoreid)
);
alter table tb_score add CONSTRAINT fk_stuid_score FOREIGN key (stuid) REFERENCES tb_student (stuid);
alter table tb_score add CONSTRAINT fk_couid_score FOREIGN key (couid) REFERENCES tb_teacher (couid);
-- 添加成績
INSERT INTO tb_score (mark) VALUES (89),(45.5),(100),(95),(60),(77),(90),
(97),(49),(66),(56),(71),(80.5),(33),(87),(56),(74),(68),(80),(92),(34),(75);
select mark from tb_score; -- 獲取表中的所有分數值
select MAX(mark) as maxmark from tb_score; -- 獲取表中所有分數的最大值
select min(mark) from tb_score;
select sum(mark) from tb_score;
select avg(mark) from tb_score;
select count(mark) from tb_score;
查詢的高級操作
1.聚合:max/min/sum/avg平均數/count
select 聚合函數(字段) from 表名 where 條件;
按條件對表查詢指定字段的數據,然后將查詢到的結果做相應的聚合運算,聚合運算的結果是最后的結果
select mark from tb_score;
select max(mark) as maxmark from tb_score;
select min(mark) from tb_score;
select sum(mark) from tb_score;
select avg(mark) from tb_score;
如果計算平均數的時候如果參與運算的對象是null,那么這個數據不會參與計算
select count(mark) from tb_score;
2.分組
select 字段操作 from 表名 where 條件 group by(字段2);
將指定表中的滿足條件的記錄按照指定字段進行指定的聚合操作
求每個學生的平均數
select stuid,avg(mark) from tb_score group by(stuid);
注意:a.字段操作的位置除了分組字段不用聚合,其他字段都必須聚合
b.分組的時候where要放在分組前對需要分組的數據進行篩選
having:分組的時候,在分組后用having代替where來對分組后的數據進行篩選
獲取平均成績最高的學生的id
獲取平均成績大于60分的學生的id
select stuid,avg(mark) from tb_score group by(stuid) having avg(mark)>60;
3.子查詢:將一個查詢操作的結果作為另外一個查詢的數據源
在tb_score表中獲取成績是大于90分的學生的id
select stuid from tb_score where mark>90 and stuid is not null;
獲取成績大于90分的學生的姓名
select stuname from tb_student where stuid in
(select stuid from tb_score where mark>90 and stuid is not null);
將一個查詢結果作為查詢對象提供給另外一個查詢,但是第一個查詢結果需要重命名
select mark from (select stuid,mark from tb_score where mark>90 and stuid is not null)as t2;
TET%W$__[8~]}N%2Z1W706O.png
{P{BCO4B87HY1@Y~Z7$O5VV.png
總結
以上是生活随笔為你收集整理的mysql外键约束分数_MySQL提高(外键约束)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 有没有五金产品展开计算机软件,花样算法猫
- 下一篇: java事务超时时间,java – 如