DB Review.

Slides:



Advertisements
Similar presentations
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Advertisements

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
1 Designing Tables for an Oracle Database System Database Course, Fall 2003.
1 Lecture 11: Basic SQL, Integrity constraints
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
1 The Oracle Database System Building a Database Database Course The Hebrew University of Jerusalem.
Introduction to Structured Query Language (SQL)
1 Query Languages: How to build or interrogate a relational database Structured Query Language (SQL)
1 Designing Tables for an Oracle Database System Database Course, Fall 2005.
CS 104 Introduction to Computer Science and Graphics Problems Introduction to Database (2) Basic SQL 12/05/2008 Yang Song.
1 Table Alteration. 2 Altering Tables Table definition can be altered after its creation Adding columns Changing columns’ definition Dropping columns.
1 Table Alteration. 2 Altering Tables Table definition can be altered after its creation –Adding columns –Changing columns’ definition –Dropping columns.
1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem.
Introduction to Structured Query Language (SQL)
1 Designing Tables for an Oracle Database System Database Course, Fall 2004.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
ICS 321 Fall 2009 SQL: Queries, Constraints, Triggers Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 9/8/20091Lipyeow.
Databases MIS 21. Some database terminology  Database: integrated collection of data  Database Management System (DBMS): environment that provides mechanisms.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
DAT602 Database Application Development Lecture 3 Review of SQL Language.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
INCLUDING CONSTRAINTS lecture5. Outlines  What are Constraints ?  Constraint Guidelines  Defining Constraint  NOT NULL constraint  Unique constraint.
Visual Programing SQL Overview Section 1.
The Oracle Database System. Connecting to the Database At the command line prompt, write: sqlplus In the beginning your password.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
Relational Database Management System(RDBMS) Structured Query Language(SQL)
SQL constrains and keys. SORTED RESULTS Sort the results by a specified criterion SELECT columns FROM tables WHERE predicates ORDER BY column ASC/DESC;
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
SQL. Internet technologies – Ohad © Database  A database is a collection of data  A database management system (DBMS) is software designed to assist.
1 Designing Tables for a Database System. 2 Where we were, and where we’re going The Entity-Relationship model: Used to model the world The Relational.
Fundamentals of DBMS Notes-1.
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
Web Systems & Technologies
Structured Query Language (SQL) DML
SQL – Part 2.
COP Introduction to Database Structures
SQL: Schema Definition and Constraints Chapter 6 week 6
© פרופ' יהושע שגיב, האוניברסיטה העברית
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Insert, Update and the rest…
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
CS 480: Database Systems Lecture 13 February 13,2013.
SQL The Query Language R & G - Chapter 5
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
Database Systems October 14, 2009 Lecture #5.
Designing Tables for a Database System
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
CS 405G: Introduction to Database Systems
Designing Tables for a postgreSQL Database System
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL data definition using Oracle
Translation of ER-diagram into Relational Schema
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
CMPT 354: Database System I
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Contents Preface I Introduction Lesson Objectives I-2
CSC 453 Database Systems Lecture
Database Design: Relational Model
Structured Query Language Path from Unorganized to Organized….
Including Constraints
CSC 453 Database Systems Lecture
Presentation transcript:

DB Review

Database A database is a collection of data A database management system (DBMS) is software designed to assist in maintaining and utilizing a large collection of data

Why Database is better than a file system Define the structure: I/O efficiency Ability to query(SQL) High availability Distributed

Relational Database Tables Relationships Primary Key Fields (different Types: Int / Boolean / date / file etc..) Relationships One to many Many to one Many to many The database is kept on the disk, so anything you create will be there next time you log on.

Table ID(PK) Country Name 1 England Yaron Gam 2 USA Kate Omaily 3 Jack Jack

Problematic table Prime Minister Country Name Gordon England Yaron Gam Bush USA Kate Omaily Jack Jack

What is the solution Country Name Israel Yaron Gam USA Kate Omaily There is a One-to-One relationship between Country and Prime minister, Country Name Israel Yaron Gam USA Kate Omaily Yael Nacsh Naftali spitzer Prime Minister Country Golda Israel Bush USA

Rules of thumb Avoid duplication One-to-One relationship should be in the same table with other fields.

SQL Structured Query Language is a database computer language designed for the retrieval and management of data in relational database management systems (RDBMS) Database schema creation Modification Database object access control management.

Create table The basic format of the CREATE TABLE command is: CREATE TABLE TableName( Column1 DataType1 ColConstraint, … ColumnN DataTypeN ColConstraint, TableConstraint1, … TableConstraintM );

Example CREATE TABLE Employee( ID INTEGER NOT NULL, Fname VARCHAR(20), Lname VARCHAR(20), Gender CHAR(1), Salary INTEGER NOT NULL, Dept INTEGER );

If you type \d Employee you get: Column Type Modifiers ----------- ------------ ------------ id integer not null fname character varying(20) lname character varying(20) gender character(1) salary integer not null dept integer

Constraints Different types of constraints: * Not Null * Default Values * Unique * Primary Key * Foreign Key * Check Condition

Foreign Key Dept Name ManID 12 Sales 988 45 Repair 876 Department ID FName LName Gender Sallary Dept 02334 Larry Bird M 230000 12 04556 Magic Johnson 270000 45 Foreign Key Dept Name ManID 12 Sales 988 45 Repair 876 Department

CREATE TABLE Employee ( ID INTEGER primary key, Fname VARCHAR(20), Lname VARCHAR(20), Gender CHAR(1), Salary INTEGER NOT NULL, DeptNumber INTEGER REFERENCES Department ); CREATE TABLE Department( DeptNumber INTEGER PRIMARY KEY, Name VARCHAR(20), ManagerId INTEGER );

Deleting a table Syntax: DROP TABLE <tableName>; Mind the order of dropping when there are foreign key constraints.

Insert Column Type Modifiers ----------- ------------ ------------ ----------- ------------ ------------ id integer not null Fname character varying(20) gender character(1) deptnumber integer

Insert syntax Syntax: insert into tablename (field1,field2…) values (v1,’v2’,…) Examples: insert into employee (id,fname,gender,deptnumber) values(122,'Goldman','M',12); insert into employee (id,deptnumber) values(123,13);

Delete Syntax: DELETE FROM Table WHERE Condition; Example: DELETE FROM Employee WHERE id = 121; WHERE Salary > 100000;

Update Syntax: UPDATE Table SET Field1=value1,,,FieldN=valueN WHERE Condition Example: UPDATE Employee SET Salary = 100000 WHERE Salary > 100000;

Queries Syntax: SELECT [Distinct] fields FROM tables WHERE condition condition: A Boolean condition (For example: age>21, or name=‘Yuval’ ). Only rows which return ‘true’ for this condition will appear in the result SELECT [Distinct] fields FROM tables WHERE condition ORDER BY field ASC/DESC

StudentID StudentDept. StudentName StudentAge 1123 Math Moshe 25 2245 Computers Mickey 26 55611 Menahem 29 Select studentID, studentName From students Where StudentDept=‘Math’ StudentID StudentName 1123 Moshe 55611 Menahem

The where clause Numerical and string comparison: !=,<>,=, <, >, >=, <=, between(val1 AND val2) Logical components: AND, OR Null verification: IS NULL, IS NOT NULL Checking against a list with IN, NOT IN.

More examples SELECT sname FROM Sailors WHERE age>=40 AND rating IS NOT NULL ; SELECT sid, sname FROM sailors WHERE sid IN (1223, 2334, 3344) or sname between(‘George’ and ‘Paul’); SELECT sid FROM Sailors WHERE sname LIKE ‘R%’;

How do we compute All sailors who have reserved a boat sid sname rating age 22 31 58 Dustin Lubber Rusty 7 8 10 45.0 55.5 35.0 Reserves sid bid day 22 58 101 103 10/10/96 11/12/96

Join Select sname from sailors, reserves Where sailors.sid=reserves.sid When there is more than one table in the from we compute a cross product

Stage 1: Sailors x Reserves sid sname rating age bid day 22 Dustin 7 45.0 101 10/10/96 58 103 11/12/96 31 Lubber 8 55.5 Rusty 10 35.0

Stage 2: “where sailors.sid=reserves.sid” sname rating age bid day 22 Dustin 7 45.0 101 10/10/96 58 103 11/12/96 31 Lubber 8 55.5 Rusty 10 35.0

Stage 2: “where sailors.sid=reserves.sid” sname rating age bid day 22 Dustin 7 45.0 101 10/10/96 58 Rusty 10 35.0 103 11/12/96

Stage 3: “select sname” Sailors Reserves sid sname rating age bid day 22 Dustin 7 45.0 101 10/10/96 58 Rusty 10 35.0 103 11/12/96

Stage 3: “select sname” Sailors sname Dustin Rusty Final answer

Order Compute the cross product of the tables Delete all rows that do not satisfy condition. Delete all columns that do not appear in fields. If Distinct is specified eliminate duplicate rows.

Aggregation The aggregate operators available in SQL are: COUNT(*) COUNT([DISTINCT] A) SUM([DISTINCT] A) AVG([DISTINCT] A) MAX(A) MIN(A) SELECT Max(S.age) FROM Sailors S

Nested quires Query inside a query Select First_Name from Internet_grades where Grade >= (Select Max(Grade) from Infi_grades);