MySQL入門 | 四

一.數據導入導出

參考教材: http://www.runoob.com/mysql/mysql-database-export.html

個人還是推薦用圖形化界面如:workbench 以及 navicat 進行直接操作,而不是輸代碼.

1.將之前創建的任意一張MySQL表導出,且是CSV格式

SELECT * INTO OUTFILE 'D:/MySQL/customer.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM test_table;

CSV文件包含結果集中的行集合。每行由一個回車序列和由LINES TERMINATED BY '\r\n’子句指定的換行字符終止。文件中的每行包含表的結果集的每一行記錄。
每個值由FIELDS ENCLOSED BY '"'子句指示的雙引號括起來。 這樣可以防止可能包含逗號(,)的值被解釋爲字段分隔符。 當用雙引號括住這些值時,該值中的逗號不會被識別爲字段分隔符。

在這裏插入圖片描述

2.再將CSV表導入數據庫

參考: https://www.yiibai.com/mysql/import-csv-file-mysql-table.html

LOAD DATA INFILE 'D:/mysql/customers.csv' 
INTO TABLE discounts 
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

文件的字段由FIELD TERMINATED BY ','指示的逗號終止,並由ENCLOSED BY '"'指定的雙引號括起來。因爲文件第一行包含列標題,列標題不需要導入到表中,因此通過指定IGNORE 1 ROWS選項來忽略第一行。

二.作業

1. 項目七

創建employee表

create table employee
(id int not null primary key,
Name varchar(255),
Salary varchar(255),
DepartmentId int);
Insert into employee values(
1, 'joe',70000,1);
Insert into employee values(
2, 'Henry',80000,2);
Insert into employee values(
3, 'Sam',60000,2);
Insert into employee values(
4, 'Max',90000,1);
select * from employee;

在這裏插入圖片描述

創建department表

create table department
(id int not null primary key,
Name varchar(255));
Insert into department values(
1, 'IT');
Insert into department values(
2, 'Sales');
select * from department;

在這裏插入圖片描述

篩選每個部門工資最高的員工

SELECT department.Name as department, employee.Name as employee , employee.Salary
from department , employee
where department.id = employee.DepartmentId 
and  employee.Salary in 
(select max(Salary) from employee
group by DepartmentId);

在這裏插入圖片描述

項目八

創建seat表

create table seat
(id int not null primary key,
Student varchar(255));
Insert into seat values(
1, 'Abbot');
Insert into seat values(
2, 'Doris');
Insert into seat values(
3, 'Emerson');
Insert into seat values(
4, 'Green');
Insert into seat values(
5, 'Jeames');
select * from seat;

在這裏插入圖片描述

select (case 
when (select count(*) from seat)%2=1 and id=(select count(*) from seat) then id
when id%2=0 then id-1 
else id+1
end) as id, Student from seat
order by id;

在這裏插入圖片描述

項目九

創建 Scores表

create table Scores
(id int not null primary key,
Score float);
Insert into Scores  values(
1, 3.50);
Insert into Scores values(
2, 3.65);
Insert into Scores values(
3, 4.00);
Insert into Scores values(
4, 3.85);
Insert into Scores values(
5, 4.00);
Insert into Scores values(
6, 3.65);
select * from Scores;

在這裏插入圖片描述

在這裏插入圖片描述

項目十

在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述

項目十一

在這裏插入圖片描述

項目十二

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章