Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Slides:



Advertisements
Similar presentations
SQL Rohit Khokher.
Advertisements

TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
Database Design -- Basic SQL
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Beginning SQL Tutorial Author Jay Mussan-Levy. What is SQL?  Structured Query Language  Communicate with databases  Used to created and edit databases.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 SQL: Data Definition, Constraints, and Basic Queries and Updates.
Introduction to Structured Query Language (SQL)
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Modification of the Database – Deletion Delete all account records at the Perryridge branch.
Introduction to Databases Transparencies
SQL SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi Information Systems Spring 2011.
Chapter 7: SQL, the Structured Query Language Soid Quintero & Ervi Bongso CS157B.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR.
Introduction to Databases. Case Example: File based Processing Real Estate Agent’s office Property for sale or rent Potential Buyer/renter Staff/employees.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
ASP.NET Programming with C# and SQL Server First Edition
CSE314 Database Systems Lecture 4 Basic SQL Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
Structured Query Language. SQL is an ANSI (American National Standards Institute) standard computer language for accessing and manipulating database systems.
Database A collection of related data. Database Applications Banking: all transactions Airlines: reservations, schedules Universities: registration, grades.
Database Technical Session By: Prof. Adarsh Patel.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
DATABASE TRANSACTION. Transaction It is a logical unit of work that must succeed or fail in its entirety. A transaction is an atomic operation which may.
SQL FUNDAMENTALS SQL ( Structured Query Language )
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
Advanced Database CS-426 Week 1 - Introduction. Database Management System DBMS contains information about a particular enterprise Collection of interrelated.
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
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.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
GLOBEX INFOTEK Copyright © 2013 Dr. Emelda Ntinglet-DavisSYSTEMS ANALYSIS AND DESIGN METHODSINTRODUCTORY SESSION EFFECTIVE DATABASE DESIGN for BEGINNERS.
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
Chapter 11 Database and SQL. Flat Files and Databases Flat files Databases Advantages Efficient use of resources Access control Disadvantages Security.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
 CONACT UC:  Magnific training   
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
SQL Basics Review Reviewing what we’ve learned so far…….
SQL Structured Query Language. SQL is an ANSI (American National Standards Institute) standard computer language for accessing and manipulating database.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
Fundamentals of DBMS Notes-1.
SQL Query Getting to the data ……..
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
Introduction to Database Systems, CS420
STRUCTURED QUERY LANGUAGE
IST 210: Organization of Data
SQL-1 Week 8-9.
Contents Preface I Introduction Lesson Objectives I-2
Structured Query Language
Manipulating Data Lesson 3.
Presentation transcript:

Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website; available at -

Web programming course, Winter 2005 Outline Database introduction  Motivation  History  Implementation SQL introduction

Web programming course, Winter 2005 Utility of Databases Data have value independent of use Organized approach to data management (e.g., data mining) Advantages  Eliminate redundancy in data  Share data  Archive data  Security of data  Integrity of data

Web programming course, Winter 2005 DEFINITION A collection of application programs that perform services to end users. Each program defines and manages its own data. File Based Systems

Web programming course, Winter 2005 Data Entry & Reports File handling Routines File Definition Sales Files Data Entry & Reports File handling Routines File Definition Lease Files File Based Processing physical structure and storage of the data files are defined in the program code

Web programming course, Winter 2005 Data Dependence Duplication of Data Incompatible file formats Limitations of File Based Systems How can these problems be resolved?

Web programming course, Winter 2005 A shared collection of logically related data designed to meet the information requirements of an organisation The Database Approach

Web programming course, Winter 2005 Data Entry & reports Data Entry & reports DBMS Sales Leases Application Programs App. Programs Database Database Processing

Web programming course, Winter 2005 DEFINITION A software system that enables users to define, create and maintain the database and which provides controlled access to the database Database Management System (DBMS)

Web programming course, Winter 2005 Allows users to define the database (DDL) Allows users to insert, update, delete & retrieve data (DML) Provides controlled access  a security system  an integrity system  a concurrency control system  a recovery system Facilities of a DBMS What does it mean to define a database?

Web programming course, Winter 2005 Hardware Software Data Procedures People Components of a DBMS

Web programming course, Winter 2005 Minimal data redundancy Consistency of data Integration of data Improved integrity Consistent security Standards Increased productivity Advantages

Web programming course, Winter 2005 Complexity Additional Hardware Costs Size Performance Experts -Specialised Personnel Potential organisational Conflict Higher impact of failure (centralized data source failure) Disadvantages

Web programming course, Winter 2005 Database Interfaces web server & interface prog relational database web browser dedicated applicationSQL command interface SQL

Web programming course, Winter 2005 Interaction and Feedback Transaction: non-decomposable unit of data manipulation  example: purchasing an airline ticket on-line  typically small and fast for commercial applications  may be long and involved in engineering applications Rollback: if any part of a transaction fails, all completed parts are “rolled back” or undone  example: if you haven’t provided your credit card number, airline ticket purchase on-line transaction fails  rollback ensures integrity of database  automatically done by DBMS

Web programming course, Winter 2005 Relational Database Model Database  Database is a collection of tables (relations)  Data are stored in tables Tables  Each table has a name  Each table has a set of columns (fields) and rows of data (records)  All operations process a table to produce a new table  Each table has a fixed number of columns  Each table has an arbitrary number of rows Based on set theory SQL (Structured Query Language)  DBMS independent language Why?

Web programming course, Winter 2005 Weather Sample Table CityStateHighLow PhoenixArizona10590 TusconArizona10192 FlagstaffArizona8869 San DiegoCalifornia7760 AlbuquerqueNew Mexico8060

Web programming course, Winter 2005 Database Columns (Fields) Columns  Each column has a name  Columns are accessed by name  No standard column ordering  Does not make sense to say “the third column” like it does in a “paper” table or spreadsheet  Data in a column belongs to a particular domain Columns are the “attributes” of the dataset Each value in a column is from the same domain Each value in a column is of the same data type

Web programming course, Winter 2005 Database Rows (Records) Rows  Each row entry is either a simple value or empty ("null")  Rows are sets of values for the columns (attribute values)  Primary key: a set of columns that uniquely identifies each row  Each row must be unique given the primary key (no duplicates)  Rows are referenced by the primary key  Row order cannot be determined by the user (Does not make sense to say “the fourth row” like it does in a “paper” table or spreadsheet)

Web programming course, Winter 2005 Data Types Each row value is an instance of a primitive data type  Integer  Real (e.g., number, currency  Character (e.g., text, hyperlink, yes/no)  Date/Time No complex types in standard DBMS (matrix, drawing)  Object oriented databases may allow objects and structures Non existent value is “null”

Web programming course, Winter 2005 Domain types char(n): fixed length char string varchar(n): variable-length char string int or integer smallint numeric(p,d): fixed-point number of given precision real, double precision float(n): floats with a given precision date: containing year,month, and date time: in hours, minutes, and seconds Null value is part of each domain

Web programming course, Winter 2005 SQL Structured Query Language Communicate with databases Used to created and edit databases. Also used to create queries, forms, and reports

Web programming course, Winter 2005 DDL and DML SQL consists of two types of statements SQL Data Definition Language (DDL) permits database tables to be created or deleted inserts new data into a database table  CREATE TABLE - creates a new database table  ALTER TABLE - alters (changes) a database table  DROP TABLE - deletes a database table SQL Data Manipulation Language (DML)  SELECT - extracts data from a database table  UPDATE - updates data in a database table  DELETE - deletes data from a database table  INSERT INTO - inserts new data into a database table

Web programming course, Winter 2005 Define new domains and tables CREATE DOMAIN personDom CHAR(20); CREATE TABLE emp (enamepersonDom, dnoint default 0, salreal );

Web programming course, Winter 2005 “Select” clause 1 Specify attributes to project onto Emp (ename, dno, sal) SELECT * FROM Emp WHERE Emp.Sal < 60K; use ‘*’ to denote all attributes: SELECT columns FROM table WHERE condition ;

Web programming course, Winter 2005 “Select” clause 2 “SELECT” does not automatically eliminate duplicates. Emp (ename, dno, sal) Select dno From Emp; Use keyword distinct to explicitly remove duplicates Select distinct dno From Emp;

Web programming course, Winter 2005 “FROM” clause Specify relations E1: Emp (ename, dno, sal) Dept(dno, dname, mgr) E2: Emp (ename, dno, sal) SELECT columns FROM table WHERE condition ; SELECT E1.ename FROM Emp as E1, Dept, Emp as E2 WHERE E1.dno = Dept.dno AND Dept.mgr = E2.ename AND E1.sal > E2.sal; Renaming relations: Use “as” to define “tuple variables,” to disambiguate multiple references to the same relation ? Who makes more money than his boss?

Web programming course, Winter 2005 “WHERE” clause Specify optional conditions “Employees who work for Sally and have a salary < 90K” SELECT ename FROM Emp, Dept WHERE Emp.dno=Dept.dno AND D.mgr = ‘Lisa’ AND sal < 90000; Emp (ename, dno, sal)Dept(dno, dname, mgr) Lisa

Web programming course, Winter 2005 Conditions Used In Where Clause =equals >greater than <less than >=greater than or equal to <=less than or equal to <>not equal to

Web programming course, Winter 2005 Like Used to make complex searching easy. If you are trying to find all people’s names which begin with E for example: SELECT firstname FROM employee WHERE firstname LIKE 'E%';

Web programming course, Winter 2005 Ordering output tuples Order the tuples by dno. Within each dept, order salaries from highest to lowest. For salary ties, use alphabetical order on the name. SELECT * FROMEmp order by dno, sal, ename; enamednosallocation John NY Roger NY Mary London Susan London Joe Paris

Web programming course, Winter 2005 Set Operations Union: . Intersect: . Except: - (select mgr from D where dname=‘toy’) union (select mgr from D where dname = ‘sells’); (select mgr from D where dname=‘toy’) intersect (select mgr from D where dname = ‘sales’); (select mgr from D where dname=‘toy’) except (select mgr from D where dname = ‘sells’); “Find names of people who are managers of either the toy or the sales department.” “Find names of people who are managers of both the toy and the sales departments.” “Find names of people who are managers of the toy but of the sales department.”

Web programming course, Winter 2005 Conserving Duplicates The UNION, INTERSECT, and EXCEPT operators use the set semantics, not bag semantics. To keep duplicates, use “ALL” after the operators:  UNION ALL, INTERSECT ALL, EXCEPT ALL Student (ssno, name) TA (ssno, name) Result (SELECT ssno FROM student) UNION (SELECT ssno FROM ta); ALL

Web programming course, Winter 2005 Aggregation functions MIN, MAX, SUM, COUNT, AVG  input: collection of numbers/strings (depending on operation)  output: relation with a single attribute with a single row “What is the minimum, maximum, average salary of employees in the toy department” SELECT MIN(sal), MAX(sal), AVG(sal) FROM Emp, Dept WHERE Emp.dno = Dept.dno and D.dname = ’Toy’;

Web programming course, Winter 2005 JOIN Sometimes we have to select data from two or more tables to make our result complete. We have to perform a join Tables in a database can be related to each other with keys. A primary key is a column with a unique value for each row. The purpose is to bind data together, across tables, without repeating all of the data in every table.

Web programming course, Winter 2005 INNER JOIN The INNER JOIN returns all rows from both tables where there is a match. Employee_IDName 01Hansen, Ola 02Svendson, Tove 03 Svendson, Stephen 04Pettersen, Kari Orders: Prod_ID ProductEmployee_ID 234Printer01 657Table03 865Chair03 Employees: SELECT Employees.Name, Orders.Product FROM Employees INNER JOIN Orders ON Employees.Employee_ID=Orders.Employee_ID Result NameProduct Hansen, OlaPrinter Svendson, StephenTable Svendson, StephenChair If there are rows in Employees that do not have matches in Orders, those rows will not be listed

Web programming course, Winter 2005 Left\Right Join The LEFT JOIN returns all the rows from the first table (Employees), even if there are no matches in the second table (Orders). The RIGHT JOIN returns all the rows from the second table (Orders), even if there are no matches in the first table (Employees). SELECT Employees.Name, Orders.Product FROM Employees LEFT JOIN Orders ON Employees.Employee_ID=Orders.Employee_ID SELECT Employees.Name, Orders.Product FROM Employees RIGHT JOIN Orders ON Employees.Employee_ID=Orders.Employee_ID Name Product Hansen, OlaPrinter Svendson, Tove Svendson, StephenTable Svendson, StephenChair Pettersen, Kari NameProduct Hansen, OlaPrinter Svendson, StephenTable Svendson, StephenChair

Web programming course, Winter 2005 INSERT INTO The INSERT INTO statement is used to insert new rows into a table INSERT INTO table_name VALUES (value1, value2,....) INSERT INTO table_name (column1, column2,...) VALUES (value1, value2,....) Or specify the columns for which you want to insert data:

Web programming course, Winter 2005 Insert Into Example LastNameFirstNameAddressCity PettersenKariSto 20Stav INSERT INTO Persons VALUES ('Hetland', 'Camilla', 'Hagabakka 24', 'Sandnes') LastName FirstNameAddressCity PettersenKariSto 20Stav HetlandCamillaHaga 24Sandnes

Web programming course, Winter 2005 UPDATE The UPDATE statement is used to modify the data in a table. UPDATE table_name SET column_name = new_value WHERE column_name = some_value

Web programming course, Winter 2005 Person: LastName FirstNameAddressCity NilsenFredKirk 56Stav Rasmussen Sto 67 LastName FirstNameAddressCity NilsenFredKirk56Stav RasmussenNinaSto 67 UPDATE Person SET FirstName = 'Nina' WHERE LastName = 'Rasmussen' UPDATE Person SET Address = 'Stien 12', City = 'Stav' WHERE LastName = 'Rasmussen' LastName FirstNameAddressCity NilsenFredKirk56Stav RasmussenNina Stien 12 Stav

Web programming course, Winter 2005 DELETE The DELETE statement is used to delete rows in a table. DELETE FROM table_name WHERE column_name = some_value LastName FirstNameAddressCity NilsenFredKirk 56Stav DELETE FROM Person WHERE LastName = 'Rasmussen' LastName FirstNameAddressCity NilsenFredKirk56Stav RasmussenNina Stien 12 Stav

Web programming course, Winter 2005 Summary Why uses databases instead of file systems, what are the pro-cons, when to use each alternative? What is a relational database? How can you define its semantics? What is SQL? What are DDL and DML? What statements are related to each SQL rule

Web programming course, Winter 2005 Questions?