Database Management Systems

Slides:



Advertisements
Similar presentations
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Advertisements

Database Systems: Design, Implementation, and Management Tenth Edition
Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.
Introduction to Structured Query Language (SQL)
Structured Query Language - SQL Carol Wolf Computer Science.
Fall 2001Arthur Keller – CS 1808–1 Schedule Today Oct. 18 (TH) Schemas, Views. u Read Sections u Project Part 3 extended to Oct. 23 (T). Oct.
Introduction to Structured Query Language (SQL)
Winter 2002Arthur Keller – CS 1808–1 Schedule Today: Jan. 29 (T) u Modifications, Schemas, Views. u Read Sections Assignment 3 due. Jan. 31 (TH)
Introduction to Structured Query Language (SQL)
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
CSCI 6962: Server-side Design and Programming
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Winter 2006Keller Ullman Cushing8–1 Turning in Assignments Please turn in hard copy (use only in the direst of circumstances). I am not your secretary.
Chapter 4 Introduction to MySQL. MySQL “the world’s most popular open-source database application” “commonly used with PHP”
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections (except and 6.6.6) Next u Constraints. u Read.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
# 1# 1 Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? CS 105.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
How to: SQL By: Sam Loch.
Chapter 5 Introduction to SQL.
Chapter 3 Introduction to SQL
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Relational Database Design
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
CS 480: Database Systems Lecture 13 February 13,2013.
Data Definition and Data Types
Database application MySQL Database and PhpMyAdmin
Database Management Systems
Designing Tables for a Database System
ORACLE SQL Developer & SQLPLUS Statements
Intro to PostgreSQL.
STRUCTURED QUERY LANGUAGE
Instructor: Mohamed Eltabakh
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Defining a Database Schema
Introduction To Structured Query Language (SQL)
Database systems Lecture 3 – SQL + CRUD
Chapter 7 Introduction to Structured Query Language (SQL)
CS122 Using Relational Databases and SQL
Data Management Innovations 2017 High level overview of DB
Introduction To Structured Query Language (SQL)
Contents Preface I Introduction Lesson Objectives I-2
CSC 453 Database Systems Lecture
CS1222 Using Relational Databases and SQL
Chapter 4 Introduction to MySQL.
SQL: Structured Query Language
CS122 Using Relational Databases and SQL
SQL (Structured Query Language)
Presentation transcript:

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

Outline

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

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

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

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

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

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

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

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

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

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

Outline

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

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

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

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

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;

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

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

Outline

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

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';

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

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

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’;

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

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

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;

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

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

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

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');

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

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

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.

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

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

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.

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

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.

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

DELETE DATA DELETE FROM tablename WHERE conditional-expression

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.

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;

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

Outline

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 …;

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’));

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 http://www.postgresql.org/docs/9.6/static/datatype.html#DATATYPE-TABLE

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

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

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));

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);

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);

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

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';

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

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

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:

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

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.

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.

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

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;

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

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