0% found this document useful (0 votes)
70 views4 pages

Solution: Unnormalized Scheme: Pletscher@inf - Ethz.ch

The document discusses unnormalized database schemes and solutions to normalize the schemes. It begins by showing a CourseAssignment database in 1NF and discusses how to normalize it into 2NF and 3NF. It then provides an overview of databases and SQL, describing their main components and purposes. It also defines various data types in SQL and column attributes that can be used in CREATE TABLE statements. Finally, it provides an example CREATE TABLE statement and discusses using SQLite for exercises.

Uploaded by

mohammed_arfan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views4 pages

Solution: Unnormalized Scheme: Pletscher@inf - Ethz.ch

The document discusses unnormalized database schemes and solutions to normalize the schemes. It begins by showing a CourseAssignment database in 1NF and discusses how to normalize it into 2NF and 3NF. It then provides an overview of databases and SQL, describing their main components and purposes. It also defines various data types in SQL and column attributes that can be used in CREATE TABLE statements. Finally, it provides an example CREATE TABLE statement and discusses using SQLite for exercises.

Uploaded by

mohammed_arfan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Solution: Unnormalized Scheme

CourseAssignment
SQL (Student-ID,Name) Course Room Building Lecturer
{(1,Bernd),(2,Birte),(3,Björn)} P G1 HPH LD
{(2,Birte),(4,Karin),(5,Jo)} ET E1 HPH WK
Patrick Pletscher {(2,Birte),(4,Karin)} RT H44 ML LG
[email protected]

ETH Zurich, Switzerland


Machine Learning group

April 30, 2009

Solution: 1NF Solution: 2NF


The following functional dependencies on parts of the keys exist:
CourseAssignment
Student-ID Name Course Room Building Lecturer Student-ID → {Name}
1 Bernd P G1 HPH LD Course → {Room,Building,Lecturer}
2 Birte P G1 HPH LD
3 Björn P G1 HPH LD
2 Birte ET E1 HPH WK
4 Karin ET E1 HPH WK
5 Jo ET E1 HPH WK
2 Birte RT H44 ML LG
4 Karin RT H44 ML LG

Solution: 2NF Solution: 3NF


All functional dependencies:
CourseAssignment
Student-ID Course Students Student-ID → {Name}
1 P Student-ID Name Course → {Room,Building,Lecturer}
2 P Room → {Building}
1 Bernd
3 P
2 Birte
2 ET There exists a transitive dependency (Course → Room → Building ). The
3 Björn
4 ET dependencies can be written in the following form:
4 Karin
5 ET
5 Jo
2 RT Student-ID → {Name}
4 RT Course → {Room,Lecturer}
Courses Room → {Building}
Course Room Building Lecturer
P G1 HPH LD
ET E1 HPH WK
RT H44 ML LG
Solution: 3NF Databases and SQL
CourseAssignment Databases: A collection of tables
Student-ID Course So far we could think of databases as a collection of tables. However, we
Students would also like to use them to answer questions about the data, e.g.,
1 P Student-ID Name which courses does Jo take?
2 P
1 Bernd
3 P Motivation for SQL
2 Birte
2 ET
3 Björn • A database is mostly concerned with the (efficient) storage of the
4 ET
4 Karin tables etc.
5 ET
5 Jo • Many different databases (MS SQL Server, PostgreSQL, MySQL,
2 RT
4 RT sqlite).
Courses Rooms • Data representation usually different.
Course Room Lecturer Room Building • SQL: A standardized interface between the user (often an
P G1 LD G1 HPH application) and the database.
ET E1 WK E1 HPH
RT H44 LG H44 ML

SQL Overview Create Table


Roughly six main commands CREATE TABLE tablename (
1 CREATE TABLE: creates a table. column_name data_type attributes?,
column_name data_type attributes?,
2 INSERT INTO: inserts values into a table.
?
3 SELECT: queries the databases. );
4 JOIN: merge/join two tables on some values.
5 UPDATE: alters values in a table.
6 DELETE: deletes entries from a table.

Tutorial on the internet


A good (and visually pleasing) tutorial on the internet:
http://sqlzoo.net/.

DataTypes Column attributes in SQL


Data Types in SQL • Keys:
• Characters: • PRIMARY KEY – primary key of the table
• CHAR(20) – fixed length • KEY – foreign key (will be indexed)
• VARCHAR(40) – variable length • INDEX – field to be indexed for fast search

• Numbers: • Null values:


• BIGINT, INT, SMALLINT, TINYINT • NOT NULL – field must be filled in
• REAL, FLOAT – differ in precision • Default value:
• MONEY • DEFAULT ’value’ – value to be used if user gives none
• Times and dates: • Automatic values:
• DATE, TIME • AUTO INCREMENT – automatic sequential numbering
• TIMESTAMP
• Others... Most of them are RDBMS-specific
• Binary objects (such as pictures, sound, etc.)
• BLOB – (Binary Large OBject)
• Others... All are simple and atomic
Create Table Example SQLite
Write the code to create the table given the following structure.
This weeks exercise
(We will insert the values later on)
Should be solved using SQLite, a leightweight SQL database.
CityID Name Country How to get SQLite?
1 Manchester UK
2 London UK • Should be installed on the students workstations.
3 Barcelona Spain • Can be obtained from http://www.sqlite.org/download.html.
• Precompiled Binaries For * is most likely what you want to install.
CREATE TABLE Cities (
CityID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, How to use it?
Name VARCHAR(40) NOT NULL,
• start it using the command sqlite3.
Country VARCHAR(40) NOT NULL
); • display the documentation: .help; exit: .quit.
• You can directly enter the SQL statements.
Remark: Autoincrement slightly different in SQLite than in lecture! • If you start sqlite3 with a filename as argument, all the commands
will be stored to this file.
• Not all of the SQL statements are supported (e.g. autoincrement).

Demo: Create Table Insert Values


Store soccer games and associated data in a database. We would like to
General Command
have the following tables:
• Matches INSERT INTO tablename (column_name, ...) VALUES (value,...)
• Teams
For all char, you need to use quotes!
• Players
• Stadiums Example
• Cities Insert Manchester into the Cities table from above:
Design and create this database: see soccer.sql. INSERT INTO Cities VALUES(1, ’Manchester’, ’UK’);

Demo: Insert Values Select from


Insert some values into the soccer database: see soccer.sql. Simplified syntax:
SELECT column_name1, column_name2, ? FROM table (WHERE cond);
SELECT * FROM table (WHERE cond);

One of the more important commands!


If you want to answer questions about the database, that’s the command
to use! E.g., list the names of the players below the age of 25.

Many additional keywords!


Commands that are often used in queries:
• Join several tables (JOIN).
• Rename columns (AS).
• Order results (ORDER BY).
• ...
See lecture for an overview of these commands!
Join: merge two tables together Demo: Select from
Query the soccer database: see soccer.sql.
An example
show the name of players born after 1985:
SELECT Players.Name, Teams.Name FROM Players
LEFT JOIN Teams ON (Players.TeamID == Teams.TeamID)
WHERE YearBorn > 1985;

Same without a join


show the name of players born after 1985:
SELECT Players.Name, Teams.Name FROM Players, Teams
WHERE (Players.TeamID == Teams.TeamID) AND YearBorn > 1985;

Performance
• Generally better to use a join, as it’s faster!
• A cascade of joins is called a “nested join”. Good to use the join
first, that reduces the number of rows the most.

Alter data: UPDATE and DELETE


Example DELETE
Delete players that most probably are not active anymore:
DELETE FROM Players WHERE (YearBorn < 1950);

Example UPDATE
Christiano Ronaldo leaves Manchester for Chelsea:

UPDATE Players SET TeamID = 3 WHERE


(Name == ’Christiano Ronaldo’);

You might also like