頭歌MySQL數(shù)據(jù)庫答案
- 特別感謝黃副班、小青提供代碼,有問題聯(lián)系公眾號【學(xué)思則安】留言更正
-
- 其他作業(yè)鏈接
- 數(shù)據(jù)庫1-MySQL數(shù)據(jù)定義與操作實(shí)戰(zhàn)
-
- MySQL數(shù)據(jù)庫 - 初識MySQL
-
- MySQL數(shù)據(jù)庫 - 數(shù)據(jù)庫和表的基本操作(一)
- MySQL數(shù)據(jù)庫 - 數(shù)據(jù)庫和表的基本操作(二)
- MySQL數(shù)據(jù)庫 - 單表查詢(一)
- MySQL數(shù)據(jù)庫 - 單表查詢(二)
- MySQL數(shù)據(jù)庫 - 單表查詢(三)
- MySQL數(shù)據(jù)庫 - 連接查詢
- MySQL數(shù)據(jù)庫 - 子查詢
- MySQL數(shù)據(jù)庫 - 復(fù)雜查詢(一)
- MySQL數(shù)據(jù)庫 - 復(fù)雜查詢(二)
- MySQL數(shù)據(jù)庫 - 使用聚合函數(shù)查詢
- MySQL數(shù)據(jù)庫 - 其他函數(shù)的使用
- MySQL數(shù)據(jù)庫 - 分組選擇數(shù)據(jù)
- 數(shù)據(jù)庫2-MySQL數(shù)據(jù)管理技術(shù)實(shí)戰(zhàn)
-
- MySQL開發(fā)技巧 - 視圖
- MySQL開發(fā)技巧 - 索引
- MySQL開發(fā)技巧 - 分頁和索引
- MySQL開發(fā)技巧 - 存儲(chǔ)過程
- MySQL開發(fā)技巧 - 事務(wù)
- MySQL開發(fā)技巧 - 并發(fā)控制
- MySQL開發(fā)技巧 - 行列轉(zhuǎn)換
- MySQL開發(fā)技巧 - 刪除重復(fù)數(shù)據(jù)
- MySQL開發(fā)技巧 - 批量數(shù)據(jù)入庫及檢索
- 數(shù)據(jù)庫3-MySQL數(shù)據(jù)庫系統(tǒng)設(shè)計(jì)實(shí)戰(zhàn)
-
- MySQL開發(fā)技巧 - 查詢、索引和完整性
- 數(shù)據(jù)庫查詢 - 選課系統(tǒng)
- 數(shù)據(jù)庫設(shè)計(jì) - 博客系統(tǒng)
- 數(shù)據(jù)庫開發(fā)基礎(chǔ)案例 - JDBC 技術(shù)應(yīng)用
- 數(shù)據(jù)庫開發(fā)中級案例 - PythonWeb框架應(yīng)用
- 數(shù)據(jù)庫開發(fā)中級案例 -ORM框架應(yīng)用
- 數(shù)據(jù)庫開發(fā)綜合案例 - 倉庫管理系統(tǒng)設(shè)計(jì)
- 數(shù)據(jù)庫開發(fā)綜合案例 - 圖書管理系統(tǒng)設(shè)計(jì)
- 數(shù)據(jù)庫4-層次、網(wǎng)狀、關(guān)系模型實(shí)戰(zhàn)
-
- 數(shù)據(jù)模型
特別感謝黃副班、小青提供代碼,有問題聯(lián)系公眾號【學(xué)思則安】留言更正
其他作業(yè)鏈接
頭歌java實(shí)訓(xùn)答案集
數(shù)據(jù)庫1-MySQL數(shù)據(jù)定義與操作實(shí)戰(zhàn)
MySQL數(shù)據(jù)庫 - 初識MySQL
數(shù)據(jù)庫部分一條一條的寫,可鼠標(biāo)手動(dòng)粘貼,除特定命令外未分大小寫。
第1關(guān):創(chuàng)建數(shù)據(jù)庫
mysql -uroot -p123123 -h127.0.0.1
create database MyDb;
第2關(guān)創(chuàng)建表
mysql -uroot -p123123 -h127.0.0.1
create database TestDb;
use TestDb;
create table t_emp (id int,
name varchar(32),
deptId int,
salary float);
第3關(guān):使用主鍵約束
mysql -uroot -p123123 -h127.0.0.1
create database MyDb;
use MyDb;
create table t_user1(
userId INT PRIMARY KEY,
name VARCHAR(32),
password VARCHAR(11),
phone VARCHAR(11),
email VARCHAR(32));
create table t_user2(
name VARCHAR(32),
phone VARCHAR(11),
email VARCHAR(32),
PRIMARY KEY(name,phone));
第4關(guān):外鍵約束
mysql -uroot -p123123 -h127.0.0.1
create database MyDb;
use MyDb;
CREATE TABLE t_class
(
id INT PRIMARY KEY,
name VARCHAR(22)
);)
CREATE TABLE t_student
(
id INT PRIMARY KEY,
name VARCHAR(22) ,
classId int,
CONSTRAINT fk_stu_class1 FOREIGN KEY(classId) REFERENCES t_class(id)
);
第5關(guān):添加常用約束
mysql -uroot -p123123 -h127.0.0.1
CREATE DATABASE MyDb;
USE MyDb;
CREATE TABLE t_user
(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(32) NOT NULL UNIQUE,
sex VARCHAR(4) DEFAULT '男'
)DEFAULT CHARSET=utf8;
MySQL數(shù)據(jù)庫 - 數(shù)據(jù)庫和表的基本操作(一)
第1關(guān):查看表結(jié)構(gòu)與修改表名
USE Company;
########## Begin ##########
########## modify the table name ##########
ALTER TABLE tb_emp RENAME jd_emp;
########## show tables in this database ##########
show tables;
########## describe the table ##########
describe jd_emp;
########## End ##########
第2關(guān):修改字段名與字段數(shù)據(jù)類型
USE Company;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
########## change the column name ##########
ALTER TABLE tb_emp change Id prod_id int(11);
########## change the data type of column ##########
ALTER TABLE tb_emp MODIFY Name varchar(30);
########## End ##########
DESCRIBE tb_emp;
第3關(guān):添加與刪除字段
USE Company;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
########## add the column ##########
ALTER TABLE tb_emp ADD Country varchar(20) AFTER Name;
########## delete the column ##########
ALTER TABLE tb_emp DROP Salary;
########## End ##########
DESCRIBE tb_emp;
第4關(guān):修改字段的排列位置
USE Company;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
########## modify the column to top ##########
ALTER TABLE tb_emp MODIFY Name varchar(25) FIRST;
########## modify the column to the rear of another column ##########
ALTER TABLE tb_emp MODIFY DeptId int(11) AFTER Salary;
########## End ##########
DESCRIBE tb_emp;
第5關(guān):刪除表的外鍵約束
USE Company;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
########## delete the foreign key ##########
ALTER TABLE tb_emp DROP FOREIGN KEY emp_dept;
########## End ##########
SHOW CREATE TABLE tb_emp G;
MySQL數(shù)據(jù)庫 - 數(shù)據(jù)庫和表的基本操作(二)
第1關(guān):插入數(shù)據(jù)
USE Company;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
########## bundle insert the value #########
INSERT INTO tb_emp(Id,Name,DeptId,Salary)
VALUES (1,"Nancy",301,2300.00),
(2,"Tod",303,5600.00),(3,"Carly",301,3200.00);
########## End ##########
SELECT * FROM tb_emp;
########## End ##########
第2關(guān):更新數(shù)據(jù)
USE Company;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
########## update the value ##########
UPDATE tb_emp
SET Name="Tracy",DeptId=302,Salary=4300.00
WHERE id=3;
########## End ##########
SELECT * FROM tb_emp;
########## End ##########
DESCRIBE tb_emp;
第3關(guān):刪除數(shù)據(jù)
USE Company;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
########## delete the value ##########
DELETE FROM tb_emp
WHERE Salary>3000;
########## End ##########
SELECT * FROM tb_emp;
########## End ##########
DESCRIBE tb_emp;
MySQL數(shù)據(jù)庫 - 單表查詢(一)
第1關(guān):基本查詢語句
USE Company;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
########## retrieving the Name and Salary ##########
select Name,Salary from tb_emp;
########## retrieving all the table ##########
select * from tb_emp;
########## End ##########
第2關(guān):帶 IN 關(guān)鍵字的查詢
USE Company;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
########## retrieving the Name and Salary with IN statement ##########
SELECT Name,Salary FROM tb_emp WHERE Id NOT IN (1);
########## End ##########
第3關(guān):帶 BETWEEN AND 的范圍查詢
USE Company;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
########## retrieving the Name and Salary with BETWEEN AND statement ##########
SELECT Name,Salary FROM tb_emp
WHERE Salary BETWEEN 3000 AND 5000;
########## End ##########
MySQL數(shù)據(jù)庫 - 單表查詢(二)
第1關(guān):帶 LIKE 的字符匹配查詢
USE Company;
######### Begin #########
SELECT Name,Salary FROM tb_emp WHERE Name LIKE "C%";
######### End #########
第2關(guān):查詢空值與去除重復(fù)結(jié)果
USE Company;
######### Begin #########
SELECT * FROM tb_emp WHERE DeptId IS NULL;
######### End #########
######### Begin #########
SELECT DISTINCT Name FROM tb_emp;
######### End #########
第3關(guān):帶 AND 與 OR 的多條件查詢
USE Company;
######### Begin #########
SELECT * FROM tb_emp WHERE DeptId=301 AND Salary > 3000;
######### End #########
######### Begin #########
SELECT * FROM tb_emp WHERE DeptId=301 OR DeptId=303;
######### End #########
MySQL數(shù)據(jù)庫 - 單表查詢(三)
第1關(guān):對查詢結(jié)果進(jìn)行排序
USE School;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
########## 查詢1班同學(xué)的所有信息以成績降序的方式顯示結(jié)果 ##########
select * from tb_score where class_id = 1 order by score desc;
########## End ##########
第2關(guān):分組查詢
USE School;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
########## 對班級名稱進(jìn)行分組查詢 ##########
SELECT * FROM tb_class GROUP BY class_id;
########## End ##########
第3關(guān):使用 LIMIT 限制查詢結(jié)果的數(shù)量
USE School;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
########## 查詢班級中第2名到第5名的學(xué)生信息 ##########
SELECT * FROM tb_score order by score desc LIMIT 1,4;
########## End ##########
MySQL數(shù)據(jù)庫 - 連接查詢
第1關(guān):內(nèi)連接查詢
USE School;
########## 查詢數(shù)據(jù)表中學(xué)生姓名和對應(yīng)的班級 ##########
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
select tb_student.name as studentName,tb_class.name as className from tb_student join tb_class on tb_class.id = tb_student.class_id;
########## End ##########
第2關(guān):外連接查詢
USE School;
########## 使用左外連接查詢所有學(xué)生姓名和對應(yīng)的班級 ##########
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
select tb_student.name as studentName,tb_class.name as className
from tb_class right join tb_student on
tb_class.id=tb_student.class_id;
########## End ##########
########## 使用右外連接查詢所有學(xué)生姓名和對應(yīng)的班級 ##########
select tb_student.name as studentName,tb_class.name as className
from tb_class left join tb_student
on tb_class.id=tb_student.class_id;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
########## End ##########
第3關(guān):復(fù)合條件連接查詢
USE School;
########## 查詢所有班級里分?jǐn)?shù)在90分以上的學(xué)生的姓名和學(xué)生的成績以及學(xué)生所在的班級 ##########
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
select s1.name as studentName,score,
s2.name as className from tb_student as s1,
tb_class as s2 where s1.class_id=s2.id and
s1.score>90 order by score desc;
########## End ##########
MySQL數(shù)據(jù)庫 - 子查詢
第1關(guān):帶比較運(yùn)算符的子查詢
USE Company;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
#1.查詢大于所有平均年齡的員工姓名與年齡
select name,age from tb_emp where age>(select avg(age) from tb_emp);
########## End ##########
第2關(guān):關(guān)鍵字子查詢
USE Company;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
#1.使用 ALL 關(guān)鍵字進(jìn)行查詢
SELECT position,salary FROM tb_salary WHERE salary >
ANY(SELECT max(salary) FROM tb_salary where position="java");
#2.使用 ANY 關(guān)鍵字進(jìn)行查詢
SELECT position,salary FROM tb_salary WHERE salary >
ANY(SELECT min(salary) from tb_salary where position="java");
#3.使用 IN 關(guān)鍵字進(jìn)行查詢
select position,salary from tb_salary where position in("java");
########## End ##########
MySQL數(shù)據(jù)庫 - 復(fù)雜查詢(一)
第1關(guān):交換工資
#請?jiān)诖颂砑訉?shí)現(xiàn)代碼
########## Begin ##########
UPDATE tb_Salary
SET
sex = CASE sex WHEN "m" THEN "f"
ELSE "m"
END;
########## End ##########
第2關(guān):換座位
#請?jiān)诖颂砑訉?shí)現(xiàn)代碼
########## Begin ##########
SELECT if(Id%2=0,Id-1,if(Id=5,Id,Id+1)) AS id,name
FROM tb_Seat ORDER BY Id;
########## End ##########
第3關(guān):分?jǐn)?shù)排名
#請?jiān)诖颂砑訉?shí)現(xiàn)代碼
########## Begin ##########
select Score,(select count(distinct score) from score where score >=s.score) as Rank
from score as s order by Score desc;
select Score,(select count(*) from score as s2 where s2.score >s1.score)+1 as Rank
from score as s1 order by Rank;
########## End ##########
第4關(guān):體育館的人流量
#請?jiān)诖颂砑訉?shí)現(xiàn)代碼
########## Begin ##########
select distinct a.* from gymnasium a,gymnasium b,gymnasium c
where a.visitors_flow>=100 and b.visitors_flow>=100
and c.visitors_flow>=100
and(
(a.id = b.id-1 and b.id = c.id - 1)or
(a.id = b.id-1 and a.id = c.id + 1)or
(a.id = b.id+1 and b.id = c.id + 1)
)
order by a.id;
########## End ##########
第5關(guān):統(tǒng)計(jì)總成績
#請?jiān)诖颂砑訉?shí)現(xiàn)代碼
########## Begin ##########
select t1.classname,t1.chinese,t2.maths
from(select c.classname classname,sum(s.chinese)
chinese from tb_class c,tb_score s where c.stuname=
s.name and s.chinese>=60 group by c.classname)t1,
(select c.classname classname,sum(s.maths)maths from tb_class c,tb_score s
where c.stuname=s.name and s.maths>=60 group by c.classname)t2
where t1.classname=t2.classname;
########## End ##########
MySQL數(shù)據(jù)庫 - 復(fù)雜查詢(二)
第1關(guān):查詢學(xué)生平均分
#請?jiān)诖颂砑訉?shí)現(xiàn)代碼
########## Begin ##########
select b.s_id,b.s_name,ROUND(AVG(a.s_score),2)as avg_score from student b
inner join score a on b.s_id = a.s_id
GROUP BY b.s_id,b.s_name HAVING avg_score <60
union
select a.s_id,a.s_name,0 as avg_score from student a
where a.s_id not in (select distinct s_id from score);
########## End ##########
第2關(guān):查詢修課相同學(xué)生信息
#請?jiān)诖颂砑訉?shí)現(xiàn)代碼
########## Begin ##########
create view temp as(select s_id,group_concat(c_id)as c from score group by s_id);
select * from student where s_id in(select s_id from temp where c=(select c from temp where s_id="01")and s_id<>"01");
########## End ##########
第3關(guān):查詢各科成績并排序
#請?jiān)诖颂砑訉?shí)現(xiàn)代碼
########## Begin ##########
select a.*,count(b.s_score)+1 rank from score a left join score b
on a.c_id = b.c_id and a.s_score <b.s_score
group by a.c_id,a.s_id
order by a.c_id,count(b.s_score);
########## End ##########
第4關(guān):查詢張老師課程成績最高的學(xué)生信息
#請?jiān)诖颂砑訉?shí)現(xiàn)代碼
########## Begin ##########
select a.*,b.s_score,b.c_id,c.c_name from student a
INNER JOIN score b ON a.s_id = b.s_id
INNER JOIN course c ON b.c_id = c.c_id
where b.c_id = (select c_id from course c,teacher d where c.t_id=d.t_id and d.t_name="張三")
and b.s_score in (select MAX(s_score)from score where c_id="02");
########## End ##########
第5關(guān):查詢兩門課程不及格同學(xué)信息
#請?jiān)诖颂砑訉?shí)現(xiàn)代碼
########## Begin ##########
select a.s_id,a.s_name,ROUND(AVG(b.s_score))
avg_score from student a
inner join score b on a.s_id = b.s_id
where a.s_id in(
select s_id from score where s_score<60 GROUP BY s_id having count(*)>=2
)
GROUP BY a.s_id,a.s_name;
########## End ##########
MySQL數(shù)據(jù)庫 - 使用聚合函數(shù)查詢
第1關(guān):COUNT( )函數(shù)
USE School;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
########## 查詢該表中一共有多少條數(shù)據(jù) ##########
select count(*) from tb_class;
########## 查詢此表中367班有多少位學(xué)生 ##########
select classid,count(*) from tb_class where classid=367;
########## End ##########
第2關(guān):SUM( )函數(shù)
USE School;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
########## 查詢所有學(xué)生總分?jǐn)?shù) ##########
select sum(score) from tb_class;
########## 查詢學(xué)生語文科目的總分?jǐn)?shù) ##########
select course,sum(score) from tb_class where course="語文";
########## End ##########
第3關(guān):AVG( )函數(shù)
USE School;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
########## 查詢學(xué)生語文科目的平均分?jǐn)?shù) ##########
select course,avg(score)from tb_class where course="語文";
########## 查詢學(xué)生英語科目的平均分?jǐn)?shù) ##########
select course,avg(score) from tb_class where course="英語";
########## End ##########
第4關(guān):MAX( )函數(shù)
USE School;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
########## 查詢語文課程中的最高分?jǐn)?shù) ##########
select course,max(score) from tb_class where course="語文";
########## 查詢英語課程中的最高分?jǐn)?shù) ##########
select course,max(score) from tb_class where course="英語";
########## End ##########
第5關(guān):MIN( )函數(shù)
USE School;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
########## 查詢語文課程中的最低分?jǐn)?shù) ##########
select course,min(score) from tb_class where course="語文";
########## 查詢英語課程中的最低分?jǐn)?shù) ##########
select course,min(score) from tb_class where course="英語";
########## End ##########
MySQL數(shù)據(jù)庫 - 其他函數(shù)的使用
第1關(guān):字符函數(shù)
#請?jiān)诖颂砑訉?shí)現(xiàn)代碼
########## Begin ##########
select CONCAT(UPPER(SUBSTR(Name,1,1)),LOWER(SUBSTR(Name,2,LENGTH(Name)))) as Name from employee;
########## End ##########
第2關(guān):數(shù)學(xué)函數(shù)文章來源:http://www.zghlxwxcb.cn/news/detail-860970.html
#請?jiān)诖颂砑訉?shí)現(xiàn)代碼
########## Begin ##########
update Score set s_score=TRUNCATE(s_score-(round(sqrt((power(4,4)-power(3,3))/power(2,2)),2)),2);
########## End ##########
第3關(guān):日期時(shí)間函數(shù)和流程文章來源地址http://www.zghlxwxcb.cn/news/detail-860970.html
到了這里,關(guān)于頭歌MySQL數(shù)據(jù)庫實(shí)訓(xùn)答案 有目錄的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!