Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Management Systems

Similar presentations


Presentation on theme: "Database Management Systems"— Presentation transcript:

1 Database Management Systems
Faculty of Computer Science Technion – Israel Institute of Technology Database Management Systems Course Tutorial 2: SQL

2 Outline

3 Relational Model - Recap
Field, Property, Attribute black purple pink Table, Relation Schema animal age color Row, Tupple cat 23 pink

4 Database example Library Database Ordered Cust_Id Book_Id Order_Date
Books Book_Id Book_Name Year Max_Time Faculty Pages Customers Cust_Id Cust_Name Faculty Borrowed Cust_Id Book_Id From_Date To_Date

5 Database example Customers(Cust_Id, Cust_Name, Faculty)
Cust_Id: Customer ID (unique) Cust_Name: Customer Name Faculty: Faculty Name

6 Database example Customers(Cust_Id, Cust_Name, Faculty) Faculty
CS Moshe Cohen 12345 EE Avi Barak 23456 MED 34567 Lior Edri 45678 56789 67890 Two Avi Barak, different faculties Two Moishe Cohen in EE

7 Database example Books(Book_Id, Book_Name, Year, Max_Time, Faculty, Pages) Book_Id: Unique book id Book_Name: Bool title Year: Year of print Max_Time: Maximum borrowing time in days Faculty: Faculty name Pages: Page number

8 Database And Knowledge
Database example Books(Book_Id, Book_Name, Year, Max_Time, Faculty, Pages) Faculty Pages Max_Time Year Book_Name Book_Id CS 348 7 1998 Database Systems 1111 14 1112 424 2001 1113 390 1 Database And Knowledge 2222 EE 2223 180 21 Electronic Circuits 3333 MED 580 1985 Genes 7 4444 450 1988 Anatomy 5555

9 Database example Ordered(Cust_Id, Book_Id, Order_Date)
Cust_Id: Customer ID Book_Id : Book ID Order_Date: Date of book order

10 Database example Ordered(Cust_Id, Book_Id, Order_Date) Order_Date
14-Oct-2002 1111 12345 24-Oct-2002 1112 45678 30-Oct-2002 1113 12-Oct-2002 2222

11 Database example Borrowed(Book_Id, Cust_Id, From_Date, To_Date)
Book_Id: Book ID Cust_Id: Customer ID From_Date: Borrowing date To_Date: Return date

12 Database example Borrowed(Book_Id, Cust_Id, From_Date, To_Date)
13-Oct-2002 56789 5555

13 Outline

14 SQL Queries: General Format
//Choose the data SELECT [ALL | DISTINCT] {[table.]* | expr [alias], exp [alias], …} //Data sources FROM table [alias], table [alias], … //Condition on the data [WHERE condition] //Aggregations [GROUP BY expr, expr, … [HAVING condition]] //Groups arithmetics [{INTERSECT | EXCEPT | UNION | UNION ALL } SELECT …] //Sorting [ORDER BY expr [ASC | DESC ], expr [ASC | DESC],…]; The expressions inside [] are optional

15 SQL Queries: SELECT Specific columns selection (by name):
SELECT column1, column2, column3,… FROM table; All columns selection: SELECT *

16 Database And Knowledge
SQL Queries: SELECT Example: select the name of every book, and its page number SELECT Book_Name, Pages FROM Books; Faculty Pages Max_Time Year Book_Name Book_Id CS 348 7 1998 Database Systems 1111 14 1112 424 2001 1113 390 1 Database And Knowledge 2222 EE 2223 180 21 Electronic Circuits 3333 MED 580 1985 Genes 7 4444 450 1988 Anatomy 5555

17 Database And Knowledge
SQL Queries: SELECT Example: select the name of every book, and its page number SELECT Book_Name, Pages FROM Books; Pages Book_Name 348 Database Systems 424 390 Database And Knowledge 180 Electronic Circuits 580 Genes 7 450 Anatomy We have duplications, use DISTINCT

18 SQL Queries: Where Filtering tuples by Boolean condition.
SELECT column1, column2, column3,… FROM table WHERE Boolean_Condition; Example: the names of the books which printed after 1990 SELECT Book_Name FROM Books WHERE Year > 1990;

19 Database And Knowledge
SQL Queries: Where SELECT Book_Name FROM Books WHERE Year > 1990 Faculty Pages Max_Time Year Book_Name Book_Id CS 348 7 1998 Database Systems 1111 14 1112 424 2001 1113 390 1 Database And Knowledge 2222 EE 2223 180 21 Electronic Circuits 3333 MED 580 1985 Genes 7 4444 450 1988 Anatomy 5555

20 Database And Knowledge
SQL Queries: Where SELECT Book_Name FROM Books WHERE Year > 1990 Book_Name Database Systems Database And Knowledge Electronic Circuits

21 Outline

22 UPDATE DATA UPDATE tablename SET column-assignment-list
WHERE conditional-expression ;

23 UPDATE DATA Goal: update values in existing tuples
First option: set fix value in specific columns in every tuple. Example: move every book to the general library and limit their borrowing time to 7 days UPDATE Books SET Max_Time = 7, Faculty = 'GEN';

24 Database And Knowledge
UPDATE DATA: Example Books Faculty Pages Max_Time Year Book_Name Book_Id CS 348 7 1998 Database Systems 1111 14 1112 424 2001 1113 390 1 Database And Knowledge 2222 EE 2223 180 21 Electronic Circuits 3333 MED 580 1985 Genes 7 4444 450 1988 Anatomy 5555

25 Database And Knowledge
UPDATE DATA: Example UPDATE Books SET Max_Time = 7, Faculty = 'GEN': Faculty Pages Max_Time Year Book_Name Book_Id GEN 348 7 1998 Database Systems 1111 1112 424 2001 1113 390 Database And Knowledge 2222 2223 180 Electronic Circuits 3333 580 1985 Genes 7 4444 450 1988 Anatomy 5555

26 UPDATE DATA Second option: update only part of the tuples by adding a WHERE condition. Example: limit the maximum borrowing time to a week, for all books in CS faculty. UPDATE Books SET Max_Time = 7 WHERE Faculty = `CS’;

27 Database And Knowledge
UPDATE DATA: Example Books Faculty Pages Max_Time Year Book_Name Book_Id CS 348 7 1998 Database Systems 1111 14 1112 424 2001 1113 390 1 Database And Knowledge 2222 EE 2223 180 21 Electronic Circuits 3333 MED 580 1985 Genes 7 4444 450 1988 Anatomy 5555

28 Database And Knowledge
UPDATE DATA: Example UPDATE Books SET Max_Time = 7 WHERE Faculty = 'CS': Faculty Pages Max_Time Year Book_Name Book_Id CS 348 7 1998 Database Systems 1111 1112 424 2001 1113 390 Database And Knowledge 2222 EE 2223 180 21 Electronic Circuits 3333 MED 580 1985 Genes 7 4444 450 1988 Anatomy 5555

29 UPDATE DATA Third option: set an expression
Example: increase borrowing time limit by 1, to all books. UPDATE Books SET Max_Time = Max_Time + 1;

30 Database And Knowledge
UPDATE DATA: Example Books Faculty Pages Max_Time Year Book_Name Book_Id CS 348 7 1998 Database Systems 1111 14 1112 424 2001 1113 390 1 Database And Knowledge 2222 EE 2223 180 21 Electronic Circuits 3333 MED 580 1985 Genes 7 4444 450 1988 Anatomy 5555

31 Database And Knowledge
UPDATE DATA: Example UPDATE Books SET Max_Time = Max_Time + 1: Faculty Pages Max_Time Year Book_Name Book_Id CS 348 8 1998 Database Systems 1111 15 1112 424 2001 1113 390 2 Database And Knowledge 2222 EE 2223 180 22 Electronic Circuits 3333 MED 580 1985 Genes 7 4444 450 1988 Anatomy 5555

32 INSERT DATA INSERT INTO tablename [( column-list )]
VALUES ( constant-list ) ; Constant list must be coherent with column-list

33 INSERT DATA Goal: adding new tuples First option: add one given tuple.
Example: add a client to Customers table INSERT INTO Customers VALUES (78901, 'Roy Peled', 'EE');

34 INSERT DATA: Example Customers Faculty Cust_Name Cust_Id CS
Moshe Cohen 12345 EE Avi Barak 23456 MED 34567 Lior Edri 45678 56789 67890

35 INSERT DATA: Example INSERT INTO Customers VALUES (78901, 'Roy Peled', 'EE'): Faculty Cust_Name Cust_Id CS Moshe Cohen 12345 EE Avi Barak 23456 MED 34567 Lior Edri 45678 56789 67890 Roy Peled 78901

36 INSERT DATA You can initiate only specific fields Example:
INSERT INTO Customers(Cust_Id,Cust_Name) VALUES (78901, 'Roy Peled'); Important ! Unintialized fields (Faculty) will be set as NULL If a coulmn does not allow NULL values, the INSERT action is ilegal and will fail.

37 INSERT DATA: Example Customers Faculty Cust_Name Cust_Id CS
Moshe Cohen 12345 EE Avi Barak 23456 MED 34567 Lior Edri 45678 56789 67890

38 INSERT DATA: Example INSERT INTO Customers(Cust_Id, Cust_Name)
VALUES (78901, 'Roy Peled'): Faculty Cust_Name Cust_Id CS Moshe Cohen 12345 EE Avi Barak 23456 MED 34567 Lior Edri 45678 56789 67890 Roy Peled 78901

39 INSERT DATA You can add into a table the result given by a query.
Example: Insert the client ID into the table Readers, for all client who ordered a book. INSERT INTO Readers( Id) (SELECT Cust_Id FROM Ordered); This is legal only if the table Readers exists.

40 Database And Knowledge
INSERT DATA: Example Readers: Ordered: Cust_Id Cust_Id Book_Name Order_Date 12345 12345 Database Systems 14-Oct-2002 45678 45678 Anatomy 24-Oct-2002 12345 12345 Database And Knowledge 30-Oct-2002 45678 45678 Electronic Circuits 12-Oct-2002

41 INSERT DATA What will happen if we’ll call INSERT into a table with the data from the same table? INSERT is executed over a temporary copy of the table Example: INSERT INTO Readers (SELECT * FROM Readers) This will not cause an infinite loop but will only duplicate the table.

42 INSERT DATA: Example Readers: Readers: INSERT INTO Readers
CS Moshe Cohen 12345 EE Lior Edri 45678 Faculty Cust_Name Cust_Id Readers: INSERT INTO Readers (SELECT * FROM Readers); Readers: CS Moshe Cohen 12345 EE Lior Edri 45678 Faculty Cust_Name Cust_Id 12345 Moshe Cohen CS 45678 Lior Edri EE

43 DELETE DATA DELETE FROM tablename WHERE conditional-expression

44 DELETE DATA DELETE deletes tuples from the table.
You need to define which tuple you want to delete. Exmple: delete all orders DELETE FROM Ordered; The table itself is not deleted, but is contain 0 tuples.

45 DELETE DATA Adding WHERE allows us to choose which tuples will be deleted. Example: Delete every order of the customer with ID 12345: DELETE FROM Ordered WHERE Cust_Id = 12345;

46 Database And Knowledge
DELETE DATA: Example Order_Date Book_Name Cust_Id 14-Oct-2002 Database Systems 12345 24-Oct-2002 Anatomy 45678 30-Oct-2002 Database And Knowledge 12-Oct-2002 Electronic Circuits DELETE FROM Ordered WHERE Cust_Id = 12345; Ordered: Order_Date Book_Name Cust_Id 24-Oct-2002 Anatomy 45678 12-Oct-2002 Electronic Circuits

47 Outline

48 Create Table Create an empty table:
CREATE TABLE table (column_name column_type [(length)] [NOT NULL],…); Create a table from a query results: CREATE TABLE table AS SELECT …;

49 Create Table: Example CREATE TABLE EXAMPLE (AB VARCHAR(15),
CD INTEGER, EF INTEGER NOT NULL, GH DECIMAL(5), IJ DECIMAL(7,2), KL TEXT default(‘DB’));

50 PostgreSQL Types VARCHAR (n) – String with length<= n
TEXT – String with unlimted length BOOLEAN – true\false DATE – date INTEGER – integer DECIMAL [(p,s)] – number with given decimal precision. Additional PostgreSQL types

51 Primary Key CREATE TABLE EXAMPLE (AB VARCHAR(15), CD INTEGER,
EF DECIMAL(5), GH DECIMAL(7,2), PRIMARY KEY (AB,CD)); A primary key cannot be NULL

52 UNIQUE CREATE TABLE EXAMPLE (AB VARCHAR(15), CD INTEGER,
EF DECIMAL(5), GH DECIMAL(7,2), UNIQUE (AB,CD)); UNIQUE (CD,EF));

53 Foreign Key Recall Ordered table:
Ordered(Cust_Id, Book_Id, Order_Date). How to impose that every Cust_id is valid? CREATE TABLE Ordered (Cust_Id INTEGER, Book_Id INTEGER, Order_Date TIMESTAMP, FOREIGN KEY (Cust_Id) REFERENCES Customers(Cust_Id));

54 Foreign Key You can also write CREATE TABLE Ordered (Cust_Id INTEGER,
Book_Id INTEGER, Order_Date TIMESTAMP, FOREIGN KEY (Cust_Id) REFERENCES Customers); (Cust_Id INTEGER REFERENCES Customers, Order_Date TIMESTAMP);

55 Foreign Key Let’s assume that there is an order for customer with id 1002, and now we delete this client. What will happen? You can define CREATE TABLE Ordered (Cust_Id INTEGER, Book_Id INTEGER, Order_Date TIMESTAMP, FOREIGN KEY (Cust_Id) REFERENCES Customers(Cust_Id) ON DELETE CASCADE);

56 Check CREATE TABLE EXAMPLE (A TEXT, B INTEGER, C INTEGER,
CHECK (B > 0), CHECK (B < C)); B INTEGER CHECK (B > 0),

57 CREATE TABLE: Example Example: create a new table named CSBooks which contains all the books from ‘CS’ faculty. CREATE TABLE CSBooks AS SELECT Book_Id, Book_Name FROM Books WHERE Faculty = 'CS';

58 Database And Knowledge
CREATE TABLE: Example Books Faculty Pages Max_Time Year Book_Name Book_Id CS 348 7 1998 Database Systems 1111 14 1112 424 2001 1113 390 1 Database And Knowledge 2222 EE 2223 180 21 Electronic Circuits 3333 MED 580 1985 Genes 7 4444 450 1988 Anatomy 5555

59 CREATE TABLE: Example CSBooks: Book_Name Book_Id Book_Name Book_Id
Database Systems 1111 1112 1113 Database And Knowledge 2222 CSBooks: Book_Name Book_Id Database Systems 1111 1112 1113 Database And Knowledge 2222

60 Database And Knowledge
Create Table New table schema: Columns types are determined by the CREATE query. You can rename the columns CREATE TABLE CSBooks(Id, Name) AS SELECT Book_Id, Book_Name FROM Books WHERE Faculty = 'CS'; Database Systems 1111 1112 1113 Database And Knowledge 2222 Name Id CSBooks:

61 DROP TABLE You can delete the table by calling DROP Example: delete CSBooks table: DROP TABLE CSBooks;

62 VIEWS Motivation: aliasing a query result as a table without actually create the table. The query that defines the view is saved in the DB and calculated every time it is being called. View can be a “window” where you can see a part of the database, on your needs. Usage: simplify queries code, calculations, privacy & security.

63 VIEWS Create a view: Example: CREATE VIEW view_name AS SELECT…;
CREATE VIEW CSBooksView AS SELECT Book_Id, Book_Name, Max_Time FROM Books WHERE Faculty = 'CS'; CSBooksView hides every columns in Books, exept Book_Id, Book_Name, Max_Time, and holds only the data relevant for CS. You cannot use ORDER BY when creating a view.

64 VIEWS CSBooksView Max_Time Book_Name Book_Id Faculty Pages Max_Time
7 Database Systems 1111 14 1112 1113 1 Database And Knowledge 2222 Max_Time Book_Name Book_Id CSBooksView Faculty Pages Max_Time Year Book_Name Book_Id CS 348 7 1998 Database Systems 1111 14 1112 424 2001 1113 390 1 Database And Knowledge 2222 EE 2223 180 21 Electronic Circuits 3333 MED 580 1985 Genes 7 4444 450 1988 Anatomy 5555

65 VIEWS You can write query on a view as if it was a table
Example: Return all the books from ‘CS’ which their borrowing time is 7 days: SELECT Book_Name FROM CSBooksView WHERE Max_Time = 7;

66 DROP VIEW You can delete a view by DROP VIEW Example:
DROP VIEW CSBooksView; The underlined table remains intact.

67 Question What is difference? CREATE TABLE CSBooks AS
SELECT Book_Id, Book_Name FROM Books WHERE Faculty = 'CS'; CREATE VIEW CSBooksView AS הטבלה לא משתנה אחרי עידכונים ב BOOKS


Download ppt "Database Management Systems"

Similar presentations


Ads by Google