788 字
4 分钟
Databases Week10 Lecture
New Model
Table of content
- Review Week9 Tutorial.
- Having Clause.
- Model extension.
- Normal forms.
Current Structure

上图为 Logical Model
在其基础上增加数据类型和数据大小即为 Physical Model

Querying Data
select count(*) from customers;select count(distinct full_name) from customers;select count(distinct full_name, cinema) from customers;-- Same full_name, different cinema still be considered as diffrent recordselect full_name, cinemafrom customersorder by 1 asc;
Querying Data (Distinct)
select distinct full_name, cinemafrom customersorder by full_name asc;
Querying Data (HAVING Clause)
The MySQL HAVING clause is used in combination with the GROUP BY clause to restrict the groups of returned rows to only those who’s the condition is TRUE.
select full_name,cinema,count(*) from customersgroup by full_name,cinemahaving count(*)>1order by 1 asc;NOTE
where和group by一起时可能无法正常返回,需使用having

Getting Additional Tables
ccdetails.CSV(Credit Card Details)

Create a New Table in Brookes
create table cc (Full_Name VARCHAR(50),Ccnumber BIGINT,Cinema VARCHAR(15));
Load The Data
LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/ccdetails.csv'INTO TABLE CcCHARACTER SET latin1FIELDS TERMINATED BY ','ENCLOSED BY '"'LINES TERMINATED BY '\n'
Update An Existing Table
- Add a new column
ccnumberto customers table.
alter table brookes.customers add column ccnumber bigint;- Fill the new column with the desirable values.
update customers as a set a.ccnumber =(select b.ccnumber from cc as bwherea.full_name = b.full_name and a.cinema = b.cinema)TIP
update操作非常耗费时间,可能会导致MySQLWorkbench连接超时,需要更改如下设置:
Alternative Method
- Erase the old values
Update customers set ccnumber = null;- Fill the new column with the desirable values.
update customers as a, cc as b set a.ccnumber = b.ccnumberwherea.full_name = b.full_name and a.cinema = b.cinema;这种方法理论上比上面那种快,因为它没有
select
Output

Find the (new) problems
select * from customerswhere ccnumber is null;
Question
Solution (ask for more data)
- Create a new table customers_tmp (just like customers).
create table customers_tmp as select * from customers;(Copies the table data as well).
- Revised code to copy the structure only:
create table customers_tmp as select * from customers where 1=2;OR
create table customers_tmp as select * from customers limit 0;Solution (load the new data)
- Load in the new data file
customer_cc_update.CSVtocustomers_tmp(BEWARE no headings).
LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/customer_cc_update.csv'INTO TABLE brookes.customers_tmpCHARACTER SET latin1FIELDS TERMINATED BY ','ENCLOSED BY '"'LINES TERMINATED BY '\n'Showing All Data
select * from customers where ccnumber is null unionselect * from customers_tmp order by full_name, cinema
Replace the rows in the table
- Delete the null values from
brookes.customers
delete from customers where ccnumber is null;- Replacing the data in
brookes.customers
insert into customers (select * from customers_tmp);Check!
select count(distinct full_name, cinema) from customers;select count(distinct ccnumber) from customers;Indexing
- Indexes are used to retrieve data from the database more fast.

Index Example
select cinema,count(*) from customers group by cinema;- Creating an index:
CREATE INDEX index_name ON table_name (column_name1, column_name2,..);create index cust_cinema on customers (cinema);- Run the selection statement again.
- Have you noticed the difference?
Managing indexes
- Showing index for a table:
show index from table_name;show index from customers;- Deleting an index
DROP index index_name on table_name;drop index cust_cinema on customers;Exporting Data
LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/filename.csv'INTO TABLE table_nameCHARACTER SET latin1FIELDS TERMINATED BY ','ENCLOSED BY '"'LINES TERMINATED BY '\n’IGNORE n LINES;SELECT *INTO OUTFILE 'c:/ProgramData/MySQL/MySQL Server 8.0/Uploads/filename.csv’FIELDS TERMINATED BY ',’OPTIONALLY ENCLOSED BY '“’LINES TERMINATED BY '\n’FROM tablename;Exporting Data (example)
SELECT * INTO OUTFILE 'c:/ProgramData/MySQL/MySQL Server 8.0/Uploads/DB.csv’FIELDS TERMINATED BY ',’OPTIONALLY ENCLOSED BY '“’LINES TERMINATED BY '\n’FROM customers;Normal Forms
- Denormalized
- Impractical.
- Tough to insert, update and delete.
- 1NF
- Every column is unique and not empty.
- No duplicated rows or column.
- No multi-value fields (atomic values).
- 2NF
- Same as 1NF.
- No data redundancy.
- All related items are stored together.
Denormalized Data Table
| Name | Address | Subject | Age |
|---|---|---|---|
| Mark | Crewe | Physics | 16 |
| Caroline | Bolton | Biology, Maths | 16 |
| John | Norwich | Maths | 17 |
| Bob | Huddersfield | Physics, Biology | 15 |
Insert, update, and delete problem
First Normal Form (1NF)
| StdID | Name | Address | Age | Subject |
|---|---|---|---|---|
| 123 | Mark | Crewe | 16 | Physics |
| 124 | Caroline | Bolton | 16 | Biology |
| 132 | John | Norwich | 17 | Maths |
| 165 | Bob | Huddersfield | 15 | Physics |
| 124 | Caroline | Bolton | 16 | Maths |
| 165 | Bob | Huddersfield | 15 | Biology |
No duplicated data or multi values per row and field
Second Normal Form (2NF)
Students
| StdID | Name | Address | Age |
|---|---|---|---|
| 123 | Mark | Crewe | 16 |
| 124 | Caroline | Bolton | 16 |
| 132 | John | Norwich | 17 |
| 165 | Bob | Huddersfield | 15 |
Subjects
| SID | Subject |
|---|---|
| 1 | Physics |
| 2 | Biology |
| 3 | Maths |
Assign
| StdID | SID |
|---|---|
| 123 | 1 |
| 124 | 2 |
| 132 | 3 |
| 165 | 1 |
| 124 | 3 |
| 165 | 2 |
Related attributes separated to appropriate tables
Databases Week10 Lecture
https://mizuki.anka2.top/posts/l5-db-week10-lecture/ 部分信息可能已经过时










