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 Introduction SQL is the standard for relational databases.
It is based on Relational Algebra and Relational Calculus. User-friendly syntax

3 SQL Scope Schema: Data: Data Access Control creation and modification
DDL (Data Definition Language) Data: insert, query, update and delete DML (Data Manipulation Language) Data Access Control DCL (Data Control Language)

4 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)

5 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

6 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

7 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?

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

9 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 );

10 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

11 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.

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

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

14 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;

15 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;


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

Similar presentations


Ads by Google