Mobile wallpaper 1Mobile wallpaper 2Mobile wallpaper 3Mobile wallpaper 4Mobile wallpaper 5Mobile wallpaper 6
788 字
4 分钟
Databases Week10 Lecture
2025-12-01

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 record

select full_name, cinema
from customers
order by 1 asc;

Querying Data (Distinct)#

select distinct full_name, cinema
from customers
order 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 customers
group by full_name,cinema
having count(*)>1
order by 1 asc;
NOTE

wheregroup 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 Cc
CHARACTER SET latin1
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

Update An Existing Table#

  • Add a new column ccnumber to 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 b
where
a.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.ccnumber
where
a.full_name = b.full_name and a.cinema = b.cinema;

这种方法理论上比上面那种快,因为它没有 select

Output#

Find the (new) problems#

select * from customers
where ccnumber is null;

Question#

How to get the full amount of rows and avoid getting the Null value?

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.CSV to customers_tmp (BEWARE no headings).
LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/customer_cc_update.csv'
INTO TABLE brookes.customers_tmp
CHARACTER SET latin1
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

Showing All Data#

select * from customers where ccnumber is null union
select * 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_name
CHARACTER SET latin1
FIELDS 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#

NameAddressSubjectAge
MarkCrewePhysics16
CarolineBoltonBiology, Maths16
JohnNorwichMaths17
BobHuddersfieldPhysics, Biology15

Insert, update, and delete problem

First Normal Form (1NF)#

StdIDNameAddressAgeSubject
123MarkCrewe16Physics
124CarolineBolton16Biology
132JohnNorwich17Maths
165BobHuddersfield15Physics
124CarolineBolton16Maths
165BobHuddersfield15Biology

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/
作者
🐦‍🔥不死鸟Anka
发布于
2025-12-01
许可协议
MIT

部分信息可能已经过时

封面
Sample Song
Sample Artist
封面
Sample Song
Sample Artist
0:00 / 0:00