Wednesday, January 14, 2015.  Proposed by E.F.Codd in his 1970 paper  Used in System R, IBM’s research relational database in early 1970s-D.D. Chamberlin.

Slides:



Advertisements
Similar presentations
Announcements Read 6.4 – 6.6 for Monday Homework 5, due Friday (today) Project Step 4, due Monday Research paper –List of sources - due 10/29.
Advertisements

Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Virtual training week 4 structured query language (SQL)
Database Design -- Basic SQL
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
SQL. Components of SQL Data definition language (DDL) –To create database structures –To enforce constraints etc. Data manipulation language (DML) –To.
Java and Databases CS-328 Dick Steflik. Database Drivers Think of a database as just another device connected to your computer like other devices it has.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Introduction to Structured Query Language (SQL)
1 Query Languages: How to build or interrogate a relational database Structured Query Language (SQL)
Using SQL to create tables Ways of using Databases.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
1 Announcements Read 6.7 – 6.10 for Friday Homework 6, due Friday 10/29 Research paper –List of sources - due 10/29 Department Seminar –The Role of Experimentation.
Oracle Data Definition Language (DDL)
1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu
CSCI 6962: Server-side Design and Programming
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Database Lecture # 1 By Ubaid Ullah.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
The Relational Model These slides are based on the slides of your text book.
ASP.NET Programming with C# and SQL Server First Edition
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Announcements Read JDBC Project Step 5, due Monday.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29.
BACS--485 SQL 11 BACS 485 Structured Query Language.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Relational Databases Database Driven Applications Retrieving Data Changing Data Analysing Data What is a DBMS An application that holds the data manages.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Design and Management CPTG /23/2015Chapter 12 of 38 Functions of a Database Store data Store data School: student records, class schedules,
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
1 Database Management Systems Physical Database Design - SQL (Part 1)
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
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.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
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.
Session 1 Module 1: Introduction to Data Integrity
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
1 CS 430 Database Theory Winter 2005 Lecture 10: Introduction to SQL.
1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL.
Populating and Querying tables Insert, Update, Delete and View (DML)
CP476 Internet Computing Perl CGI and MySql 1 Relational Databases –A database is a collection of data organized to allow relatively easy access for retrievals,
ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 6 CGI/Perl and databases.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
IS6146 Databases for Management Information Systems Lecture 3: SQL III – The DDL Rob Gleasure robgleasure.com.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
1 A Very Brief Introduction to Relational Databases.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
Fundamentals of DBMS Notes-1.
Rob Gleasure robgleasure.com
Chapter 5 Introduction to SQL.
Rob Gleasure robgleasure.com
Managing Tables, Data Integrity, Constraints by Adrienne Watt
JDBC.
SQL-1 Week 8-9.
Presentation transcript:

Wednesday, January 14, 2015

 Proposed by E.F.Codd in his 1970 paper  Used in System R, IBM’s research relational database in early 1970s-D.D. Chamberlin et al at IBM Research Center, San Jose, California  Used in Oracle, released in late 1970s  Incorporated into IBM’s SQL/DS in 1981, and DB2 in 1983  Also used in Microsoft SQL Server, MySQL, Informix, Sybase, PostGreSQL, Microsoft Access, and others

 ANSI and ISO published SQL standards in 1986, called SQL-1  Minor revision, SQL-89  Major revision, SQL-2,1992  SQL-3, 1999, 2003, 2006, multi- part revision, includes new data types, object-oriented (OO) facilities, user defined datatypes (UDTs), triggers, support for XML  Most vendors support standard, but have slight variations of their own

 Data definition language - DDL  Data manipulation language - DML  Authorization language – grant privileges to users

 Separate external, logical, internal models  Logical level-base tables and indexes  Indexes, B+ or B trees – maintained by system  Relational views (external level) - derived from base tables  Users see views or base tables, or combination  Internal level - files  SQL supports dynamic database definition- can modify structures easily

CREATE TABLE CREATE INDEX ALTER TABLE RENAME TABLE DROP TABLE DROP INDEX Also – CREATE VIEW

CREATE TABLE base-table-name ( colname datatype [column constraints], [,colname datetype [column constraints …]]..., [table constraints] [storage specifications]);

 No SQL keywords  Table name unique within the database  Column name unique within the table  In Oracle, identifiers must be at most 30 characters long, begin with an alphabetic character, and contain only alphanumeric characters (but _, $ and # are permitted)

 Each column must have a datatype specified  Available datatypes vary from DBMS to DBMS  Standards include various numeric types, fixed-length and varying-length character strings, bit strings, and user-defined types  Oracle types include CHAR(N), VARCHAR2(N), NUMBER(N,D), DATE, BLOB (binary large object) and others  SQL Server includes types of NUMERIC, BINARY, CHAR, VARCHAR DATETIME, MONEY, IMAGE, and others  Microsoft Access supports several types of NUMBER, as well as TEXT, MEMO, DATE/TIME, HYPERLINK, YES/NO, and others

Column constraints – in-line constraints NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, REF, CHECK, and DEFAULT Table constraints – out-of-line constraints all of the above except NOT NULL

CREATE TABLE Student( stuIdVARCHAR2(6), lastNameVARCHAR2(20) NOT NULL, firstName VARCHAR2(20) NOT NULL, majorVARCHAR2(10), credits NUMBER(3) DEFAULT 0, CONSTRAINT Student_stuId_pk PRIMARY KEY (stuId), CONSTRAINT Student_credits_cc CHECK ((credits>=0) AND (credits < 150))); CREATE TABLE Faculty( facIdVARCHAR2(6), nameVARCHAR2(20) NOT NULL, departmentVARCHAR2(20), rank VARCHAR2(10), CONSTRAINT Faculty_facId_pk PRIMARY KEY (facId)); CREATE TABLE Class( classNumberVARCHAR2(8), facId VARCHAR2(6) NOT NULL, schedule VARCHAR2(8), roomVARCHAR2(6), CONSTRAINT Class_classNumber_pk PRIMARY KEY (classNumber), CONSTRAINT Class_facId_fk FOREIGN KEY (facId) REFERENCES Faculty (facId) ON DELETE SET NULL, CONSTRAINT Class_schedule_room_uk UNIQUE (schedule, room)); CREATE TABLE Enroll( stuIdVARCHAR2(6), classNumberVARCHAR2(8), gradeVARCHAR2(2), CONSTRAINT Enroll_classNumber_stuId_pk PRIMARY KEY (classNumber, stuId), CONSTRAINT Enroll_classNumber_fk FOREIGN KEY (classNumber) REFERENCES Class (classNumber) ON DELETE CASCADE, CONSTRAINT Enroll_stuId_fk FOREIGN KEY (stuId) REFERENCES Student (stuId) ON DELETE CASCADE);

 Can create any number of indexes for tables  Stored in same file as base table  Facilitate fast retrieval of records with specific values in a column  Keep track of what values exist for the indexed columns, and which records have those values  B+ trees or B trees used – see Appendix A for review of concepts  Overhead – system must maintain index

CREATE [UNIQUE] INDEX indexname ON basetablename (colname [order] [,colname [order]]...) [CLUSTER] ; Ex. CREATE INDEX Student_lastName_firstName_ndx ON Student (lastName, firstName);  UNIQUE specification enforces unique values for indexed column or combination of columns  Except when specified, column need not be unique  Order is ASC(default) or DESC  Can have major and minor orders  CLUSTER specification keeps records with same value for indexed field together (only one per table)  Oracle automatically indexes primary key columns

 To add a new column ALTER TABLE basetablename ADD columnname datatype; Ex. ALTER TABLE Student ADD COLUMN birthdate DATETYPE; ◦ Cannot specify NOT NULL, since existing records have no value for this field  To drop a column ALTER TABLE basetablename DROP COLUMN columnname; Ex. ALTER TABLE Student DROP COLUMN major;  To change a column’s properties ALTER TABLE basetablename MODIFY COLUMN colname [new specifications];  To rename a column ALTER TABLE Student RENAME COLUMN colname TO new-colname;

 To add a constraint ALTER TABLE basetablename ADD CONSTRAINT constraint_defn;  To drop a constraint ALTER TABLE basetablename DROP CONSTRAINT constraint_name;  To rename a table: RENAME TABLE old-table-name TO new-table-name; Ex: RENAME TABLE FACULTY TO TEACHERS;  To drop a table: DROP TABLE basetablename; Ex. DROP TABLE CLASS;  To drop an index: DROP INDEX indexname; Ex. DROP INDEX Student_lastName_fristName_ndx;

 Non-procedural, declarative language  Can be interactive, can be embedded in host language, or can be stand-alone programming language (SQL/PSMs)  Basic commands SELECT UPDATE INSERT DELETE

SELECT [DISTINCT] col-name [AS newname], [,col-name..]… FROMtable-name [alias] [,table-name]… [WHEREpredicate] [GROUP BY col-name [,col-name]…[HAVING predicate] or [ORDER BY col-name [,col-name]…];  Powerful command – equivalent to relational algebra’s SELECT, PROJECT, JOIN and more…  Can be applied to one or more tables or views  Can display one or more columns (renaming if desired)  Predicate is optional, and may include usual operators and connectives  Can put results in order by one or more columns  Can group together records with the same value for column(s)  Can also use predefined functions

UPDATEtablename SETcolumnname = expression [columnname = expression]... [WHEREpredicate];  Used for changing values in existing records  Can update, zero, one, many, or all records in a table  For null value, use SET columnname = NULL  can use a sub-query to identify records to be updated

INSERT INTOtablename [(colname [,colname]...)] VALUES(constant [,constant]...);  Used for inserting new records into database, one at a time  Not necessary to name columns if values are supplied for all columns, in proper order  To insert null value for a column, specify only the other columns or write null as the value  Can specify DEFAULT as value if column has a default defined  Can specify values for some columns, in any order, as long as values match order

DELETE FROMtablename WHEREpredicate;  Used for deleting existing records from database  Can delete zero, one, many, or all records  Operation may not work if referential integrity would be lost  Can use a sub-query to target records to be deleted  If you delete all records from a table, its structure still remains, and you can insert into it later

 Can be subsets of base tables, or subsets of joins, or contain calculated data  Reasons for views ◦ Allow different users to see the data in different forms ◦ Provide a simple authorization control device ◦ Free users from complicated DML operations ◦ If database is restructured, view can keep the user's model constant

CREATE [OR REPLACE] VIEW viewname [(viewcolname,viewcolname]...)] AS SELECT colname [,colname]... FROM basetablename [,basetablename]... WHERE condition;  Can create vertical subset of table, choosing only certain columns, with no WHERE, called value- independent view  Can choose only certain rows, using WHERE, called value-dependent view  Can use a join of tables to create view of combination  Can use functions or subqueries in SELECT

 Can write new SQL statements using view name in FROM line  Can create a view of a view  Can sometimes insert/update a view ◦ requires that the primary key be in the view ◦ Actually updates underlying table  Can modify existing view by using Create or replace

 SQL can be embedded in host languages, such as Visual Basic, C, C++, Java, COBOL, Fortran…  Client-side programs  Host language provides control structures; SQL used for database access  SQL statements preceded by EXEC SQL, end with ;  Executable SQL statement can appear wherever a host language executable statement can appear  Pre-compiler for DB compiles SQL separately from program; creates access module  Host language statements compiled as usual  Data exchange done using shared variables

 DBMS can provide a library of functions available to host languages using an API  ODBC/JDBC provide standardized connectivity using a common interface, allows common code to access different databases  Most vendors provide ODBC or JDBC drivers that conform to the standard  Requires four components: application, driver manager, driver, and data source (database)

 Application, using the standard API ◦ initiates the connection with the database ◦ submits data requests as SQL statements to the DBMS ◦ retrieves the results ◦ performs processing ◦ terminates the connection  Driver manager ◦ loads and unloads drivers at the application’s request ◦ passes the ODBC or JDBC calls to the selected driver  Database driver ◦ links the application to the data source ◦ translates the ODBC or JDBC calls to DBMS-specific calls ◦ handles data translation needed because of any differences between the DBMS’s data language and the ODBC/JDBC standard ◦ Controls error handling differences that arise between the data source and the standard.  Data source: database (or other source), DBMS and platform; provides the data