+ All Categories
Home > Documents > Practice 03: SQL Data Definition Languagesvoboda/courses/2015-1-A7B36DBS/pr… · A7B36DBS:...

Practice 03: SQL Data Definition Languagesvoboda/courses/2015-1-A7B36DBS/pr… · A7B36DBS:...

Date post: 12-Jun-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
13
Faculty of Electrical Engineering, Czech Technical University in Prague Course A7B36DBS: Database Systems SQL – Data Definition Language Martin Svoboda Practice 03:
Transcript
Page 1: Practice 03: SQL Data Definition Languagesvoboda/courses/2015-1-A7B36DBS/pr… · A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015

Faculty of Electrical Engineering, Czech Technical University in Prague

Course A7B36DBS: Database Systems

SQL – Data Definition Language

Martin Svoboda

Practice 03:

Page 2: Practice 03: SQL Data Definition Languagesvoboda/courses/2015-1-A7B36DBS/pr… · A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015

A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015 2

Create Table Statements

• Table creation

• Definition of columns

Page 3: Practice 03: SQL Data Definition Languagesvoboda/courses/2015-1-A7B36DBS/pr… · A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015

A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015 3

Create Table Statements

• Data types

INT, BIGINT, DECIMAL, FLOAT, BOOLEAN, CHAR, VARCHAR, DATE, TIME, DATETIME, …

• Integrity constraints

NOT NULL

PRIMARY KEY

UNIQUE

CHECK

FOREIGN KEY with optional referential actions

Page 4: Practice 03: SQL Data Definition Languagesvoboda/courses/2015-1-A7B36DBS/pr… · A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015

Assignments

Page 5: Practice 03: SQL Data Definition Languagesvoboda/courses/2015-1-A7B36DBS/pr… · A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015

A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015 5

Exercise 1

• Express a CREATE TABLE statement for the following relational schema:

Library(Name, Street, City, PostCode)

Choose appropriate data types for all attributes

Express the PRIMARY KEY and NOT NULL constraints

Page 6: Practice 03: SQL Data Definition Languagesvoboda/courses/2015-1-A7B36DBS/pr… · A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015

A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015 6

Exercise 2

• Express a CREATE TABLE statement:

User(Card, FirstName, LastName, Email, DateOfBirth)

‒ Card is a 16 digits long user card identification number

‒ Date of birth may be specified only optionally

Describe all basic integrity constraints

Check email addresses for correctness

‒ I.e. simply verify (using predicate LIKE) that they correspond to a basic pattern of email addresses

Page 7: Practice 03: SQL Data Definition Languagesvoboda/courses/2015-1-A7B36DBS/pr… · A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015

A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015 7

Exercise 3

• Express a CREATE TABLE statement:

Phone(User, Number) User ⊆ User.Card

‒ Phone numbers are always 9 digits long

Describe referential integrity

Page 8: Practice 03: SQL Data Definition Languagesvoboda/courses/2015-1-A7B36DBS/pr… · A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015

A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015 8

Exercise 4

• Express a CREATE TABLE statement:

Title(IdTitle, ISBN, Title)

‒ IdTitle is an artificially assigned integer identifier

‒ ISBN identifiers are at most 17 characters long

‒ Transform both the relational keys correctly

Author(IdAuthor, Name, YearOfBirth, YearOfDeath)

‒ Both years of birth and death are optional

‒ Check also consistency of their values

Authorship(Title, Author) Title ⊆ Title.IdTitle, Author ⊆ Author.IdAuthor

Page 9: Practice 03: SQL Data Definition Languagesvoboda/courses/2015-1-A7B36DBS/pr… · A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015

A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015 9

Exercise 5

• Express an ALTER TABLE statement:

Add IdLibrary as a new identifier of libraries

Page 10: Practice 03: SQL Data Definition Languagesvoboda/courses/2015-1-A7B36DBS/pr… · A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015

A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015 10

Exercise 6

• Express a CREATE TABLE statement:

Book(Library, Signature, Title, DateOfAcquisition) Library ⊆ Library.IdLibrary Title ⊆ Title.IdTitle

Page 11: Practice 03: SQL Data Definition Languagesvoboda/courses/2015-1-A7B36DBS/pr… · A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015

A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015 11

Exercise 7

• Express a CREATE TABLE statement:

Loan(User, Library, Signature, TimeBorrowed, IdLoan, DateReturned) User ⊆ User.Card (Library, Signature) ⊆ Book.(Library, Signature)

Return date is the actual date of successful return

Add suitable referential actions

‒ When a book / user is…

• … updated then the corresponding loans will be updated too

• … removed then the corresponding loans will be preserved

Page 12: Practice 03: SQL Data Definition Languagesvoboda/courses/2015-1-A7B36DBS/pr… · A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015

A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015 12

Exercise 8

• Create INSERT statements for the following data:

Two loans undertaken by right one user; these loans correspond to two different books of the same title within one library

Specify all the values by yourself, but meaningfully

Page 13: Practice 03: SQL Data Definition Languagesvoboda/courses/2015-1-A7B36DBS/pr… · A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015

A7B36DBS: Database Systems | Practice 03: SQL – Data Definition Language | 29. 10. and 5. 11. 2015 13

Exercise 9

• Express the following UPDATE and DELETE statements:

Change a signature of the book

Remove the user from our database

Describe the exact impact on records and their values in all the involved tables


Recommended