Presentation is loading. Please wait.

Presentation is loading. Please wait.

Session - 6 Sequence - 1 SQL: The Structured Query Language:

Similar presentations


Presentation on theme: "Session - 6 Sequence - 1 SQL: The Structured Query Language:"— Presentation transcript:

1 Session - 6 Sequence - 1 SQL: The Structured Query Language:
Introduction and Tables Presented by: Dr. Samir Tartir

2 Outline Introduction Relational Algebra Versions Scope Data Types
Creating, Altering and Dropping Tables

3 Introduction SQL is the standard for relational databases.
The official pronunciation is “es queue el” Some still use “sequel”. It is based on Relational Algebra and Relational Calculus. User-friendly syntax

4 Relational Algebra vs. SQL
RA operations are too technical for most users. SQL is a high-level declarative language interface. The DBMS decides on the best execution of the query.

5 Versions SQL1 (or SQL-86): SQL2 (or SQL-92):
A joint effort by ANSI (the American National Standards Institute) and ISO (the International Standards Organization). SQL2 (or SQL-92): Major revision: New data types, additional character sets, set operations, etc Other versions with minor revisions exist SQL3, SQL-2003, SQL-2006, SQL-2008

6 SQL Scope Schema: Data: Data Access Control creation and modification
DDL Data: insert, query, update and delete DML Data Access Control DCL

7 Vocabulary SQL uses the terms “Table” for “Relation” “Row” for “Tuple”
“Column” for “Attribute”

8 SQL Data Types Numeric Character Strings Integers Real numbers
INTEGER, and SMALLINT Real numbers FLOAT, REAL, DOUBLE PRECISION Formatted numbers DECIMAL(i,j) or NUMERIC(i,j) Character Strings CHAR(n), VARCHAR(n)

9 SQL Data Types Date and Time: DATE: TIME: TIME(i):
Made up of year-month-day in the format yyyy-mm-dd TIME: Made up of hour:minute:second in the format hh:mm:ss TIME(i): Made up of hour:minute:second plus i additional digits specifying fractions of a second format is hh:mm:ss:ii...i

10 SQL Data Types TIMESTAMP: INTERVAL: Has both DATE and TIME components
Specifies a relative value rather than an absolute value Can be DAY/TIME intervals or YEAR/MONTH intervals Can be positive or negative when added to or subtracted from an absolute value, the result is an absolute value

11 Company ER Schema

12 Relational Schema Mapping

13 CREATE TABLE Specifies a new base relation by giving it a name, and specifying each of its attributes and their data types. A constraint NOT NULL may be specified on an attribute

14 Example CREATE TABLE DEPARTMENT ( DNAME VARCHAR(10) NOT NULL,
Table Name CREATE TABLE DEPARTMENT ( DNAME VARCHAR(10) NOT NULL, DNUMBER INTEGER NOT NULL, MGRSSN CHAR(9), MGRSTARTDATE CHAR(9) ); Column Names Column Types Null?

15 CREATE TABLE Primary key Secondary keys
Referential integrity constraints (foreign keys) Key attributes can be specified via the PRIMARY KEY and UNIQUE phrases

16 Example CREATE TABLE DEPT ( DNAME VARCHAR(10) NOT NULL,
DNUMBER INTEGER NOT NULL, MGRSSN CHAR(9), MGRSTARTDATE CHAR(9), PRIMARY KEY (DNUMBER), UNIQUE (DNAME), FOREIGN KEY (MGRSSN) REFERENCES EMP );

17 REFERENTIAL INTEGRITY OPTIONS
CREATE TABLE DEPT ( DNAME VARCHAR(10) NOT NULL, DNUMBER INTEGER NOT NULL, MGRSSN CHAR(9), MGRSTARTDATE CHAR(9), PRIMARY KEY (DNUMBER), UNIQUE (DNAME), FOREIGN KEY (MGRSSN) REFERENCES EMP ON DELETE SET DEFAULT ON UPDATE CASCADE );

18 REFERENTIAL INTEGRITY OPTIONS
CREATE TABLE EMP( ENAME VARCHAR(30) NOT NULL, ESSN CHAR(9), BDATE DATE, DNO INTEGER DEFAULT 1, SUPERSSN CHAR(9), PRIMARY KEY (ESSN), FOREIGN KEY (DNO) REFERENCES DEPT ON DELETE SET DEFAULT ON UPDATE CASCADE, FOREIGN KEY (SUPERSSN) REFERENCES EMP ON DELETE SET NULL ON UPDATE CASCADE);

19 ALTER TABLE Used to add an attribute to one of the base relations
The new attribute will have NULLs in all the tuples of the relation right after the command is executed; hence, the NOT NULL constraint is not allowed for such an attribute

20 Example Adding a job description to each employee ALTER TABLE EMPLOYEE
ADD JOB VARCHAR(12); The database users must still enter a value for the new attribute JOB for each EMPLOYEE tuple. This can be done using the UPDATE command.

21 Example Modify the size of the last name field to 50.
ALTER TABLE EMPLOYEE MODIFY LNAME VARCHAR(50);

22 Example Remove the birthdate column from the Employee table.
ALTER TABLE EMPLOYEE DROP COLUMN BIRTHDATE;

23 DROP TABLE Used to remove a relation (base table) and its definition
The relation can no longer be used in queries, updates, or any other commands since its description no longer exists Example: DROP TABLE DEPENDENT;

24 DROP TABLE If a table has other tables connect to (using foreign keys), it can’t be removed until the foreign keys are removed first. Or, using the following statement that removes the table and any foreign key constraints connected to it. Example: DROP TABLE EMPLOYEE CASCADE CONSTRAINTS;

25 SUMMARY Introduction to SQL Data Types Manipulating Tables

26 Resources & References
Dr. Samir Tartir Website: Fundamentals of Database Systems by El Masri & Navathe. Publisher : Addison-Wesley, 5th edition, 2006.


Download ppt "Session - 6 Sequence - 1 SQL: The Structured Query Language:"

Similar presentations


Ads by Google