SQL. Internet technologies – Ohad © Database  A database is a collection of data  A database management system (DBMS) is software designed to assist.

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.
The Relational Model. Introduction Introduced by Ted Codd at IBM Research in 1970 The relational model represents data in the form of table. Main concept.
1 Query-by-Example (QBE). 2 v A “GUI” for expressing queries. –Based on the Domain Relational Calulus (DRC)! –Actually invented before GUIs. –Very convenient.
OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column.
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
SQL Constraints and Triggers
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 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
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.
DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.
Database Lecture # 1 By Ubaid Ullah.
1 IT420: Database Management and Organization SQL - Data Manipulation Language 27 January 2006 Adina Crăiniceanu
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
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.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
ICS 321 Fall 2009 SQL: Queries, Constraints, Triggers Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 9/8/20091Lipyeow.
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
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.
ICS 321 Fall 2011 The Relational Model of Data (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 8/29/20111Lipyeow.
1 Database Systems ( 資料庫系統 ) October 24, 2005 Lecture #5.
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.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
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.
Visual Programing SQL Overview Section 1.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
Using SQL Connecting, Retrieving Data, Executing SQL Commands, … Svetlin Nakov Technical Trainer Software University
ICS 321 Spring 2011 The Database Language SQL (iii) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 3/14/20111Lipyeow.
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)
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.
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;
SQL CS 186, Spring 2007, Lecture 7 R&G, Chapter 5 Mary Roth   The important thing is not to stop questioning. Albert Einstein Life is just a bowl of.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Chapter 3 The Relational Model. Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy.
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.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
© פרופ' יהושע שגיב, האוניברסיטה העברית
Insert, Update and the rest…
Query-by-Example (QBE)
Designing Tables for a Database System
DB Review.
Designing Tables for a postgreSQL Database System
Contents Preface I Introduction Lesson Objectives I-2
CSC 453 Database Systems Lecture
Presentation transcript:

SQL

Internet technologies – Ohad © 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

Internet technologies – Ohad © Why Database is better than a file system  Simple, efficient queries(SQL)  concurrency control ( few threads changing the same data)  data modeling and structure  Disk accesses frequent, pretty slow

Internet technologies – Ohad © Relational Database  Tables  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.

Internet technologies – Ohad © DB planning goals  Efficient space  Consistency  Restrictions  Data mining ability

Internet technologies – Ohad © Problematic table Prime MinisterCountryName GordonEnglandYaron Gam BushUSAKate Omaily ObamaUSAJack

Internet technologies – Ohad © What is the solution  There is a One-to-One relationship between Country and Prime minister, CountryName IsraelYaron Gam USAKate Omaily IsraelYael Nacsh IsraelNaftali spitzer Prime MinisterCountry GoldaIsrael BushUSA

Internet technologies – Ohad © Rules of thumb  Avoid duplication  One-to-One relationship should be in the same table with other fields.

Internet technologies – Ohad © 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.

Internet technologies – Ohad ©

CS Lab  In order to connect to it, print the following command in your shell: psql –hdbserver public  \q exit psql  \h [command]help about ‘command’  \d [name] describe table/index/… called ‘name’  \dtlist tables  \di list indexes  \dv list views  \df list functions  \i fileName

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

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

Internet technologies – Ohad © If you type \d Employee you get: Column Type Modifiers idinteger not null fnamecharacter varying(20) lnamecharacter varying(20) gender character(1) salary integer not null deptinteger

Internet technologies – Ohad © Constraints  Different types of constraints:  * Not Null* Default Values  * Unique * Primary Key  * Foreign Key* Check Condition

Internet technologies – Ohad © Foreign Key IDFNameLNameGenderSallaryDept 02334LarryBirdM MagicJohnsonM Foreign Key DeptNameManID 12Sales988 45Repair876 Department

Internet technologies – Ohad © 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 );

Internet technologies – Ohad © Deleting a table  Syntax: DROP TABLE ;  Mind the order of dropping when there are foreign key constraints.

Internet technologies – Ohad © Insert Column Type Modifiers idinteger not null Fnamecharacter varying(20) gender character(1) deptnumberinteger

Internet technologies – Ohad © 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);

Internet technologies – Ohad © Delete  Syntax: DELETE FROM Table WHERE Condition;  Example:  DELETE FROM Employee WHERE id = 121; DELETE FROM Employee WHERE Salary > ;

Internet technologies – Ohad © Update  Syntax: UPDATE Table SET Field1=value1,,,FieldN=valueN WHERE Condition  Example: UPDATE Employee SET Salary = WHERE Salary > ;

Internet technologies – Ohad © Queries  Syntax:  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

Internet technologies – Ohad © StudentIDStudentDept.StudentNameStudentAge 1123MathMoshe ComputersMickey MathMenahem29 Select studentID, studentName From students Where StudentDept=‘Math’ StudentIDStudentName 1123Moshe 55611Menahem

Internet technologies – Ohad © 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.

Internet technologies – Ohad © 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%’;

Internet technologies – Ohad © Reserves sidbidday /10/96 11/12/96 Sailors sidsnameratingage Dustin Lubber Rusty How do we computer All sailors who have reserved a boat

Internet technologies – Ohad © Join Select sname from sailors, reserves Where sailors.sid=reserves.sid When there is more than one table in there from part we computer a cross product

Internet technologies – Ohad © SailorsReserves sidsnameratingagesidbidday 22Dustin /10/96 22Dustin /12/96 31Lubber /10/96 31Lubber /12/96 58Rusty /10/96 58Rusty /12/96 Stage 1: Sailors x Reserves

Internet technologies – Ohad © SailorsReserves sidsnameratingagesidbidday 22Dustin /10/96 22Dustin /12/96 31Lubber /10/96 31Lubber /12/96 58Rusty /10/96 58Rusty /12/96 Stage 2: “where sailors.sid=reserves.sid”

Internet technologies – Ohad © SailorsReserves sidsnameratingagesidbidday 22Dustin /10/96 58Rusty /12/96 Stage 2: “where sailors.sid=reserves.sid”

Internet technologies – Ohad © SailorsReserves sidsnameratingagesidbidday 22Dustin /10/96 58Rusty /12/96 Stage 3: “select sname”

Internet technologies – Ohad © Sailors sname Dustin Rusty Stage 3: “select sname” Final answer

Internet technologies – Ohad © Order 1. Compute the cross product of the tables 2. Delete all rows that do not satisfy condition. 3. Delete all columns that do not appear in fields. 4. If Distinct is specified eliminate duplicate rows.

Internet technologies – Ohad © 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

Internet technologies – Ohad © Nested quires  Query inside a query Select First_Name from Internet_grades where Grade >= (Select Max(Grade) from Infi_grades);