Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.

Similar presentations


Presentation on theme: "Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with."— Presentation transcript:

1 Structured Query Language

2 Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with IBM System R 1987 first ISO standard version 1992 SQL 2 1999 SQL 3 –Object-relational model 2003:SQL 2003 –SQL/XML Products: DB2, Oracle, MS SQL, MySQL

3 Language Overview Three major components: –Data definition language Create Table –Data manipulation language Updating database: –Insert, Delete, Update Query database: –Select –Data control language (DCL) Help DBA control the database: –Grant/revoke privileges to access the database, creating procedures, etc. Interface with database: –Entering command interactively at the DBMS command prompt. –Embedded in a procedural language

4 Data Definition Language SQL Identifiers –Character set: A-Z, a-z, 0-9, _ –<= 128 characters –Start with a letter –Cannot contain spaces

5 SQL Data Types Boolean –True, False, Unknown (for Null) Character –Fixed length: CHARACTER(n), CHAR(n), CHAR – default to 1 character –Varying length: CHARACTER VARYING(n): Maximum length is n VARCHAR(n) Numeric –NUMERIC(i,j), DECIMAL(i,j), DEC(i,j) –INTEGER, INT, SMALLINT – up to 32767 –FLOAT, REAL, DOUBLE PRECISION Date: –DATE –TIME –TIMESTAMP Large objects: –CHARACTER LARGE OBJECT –BINARY LARGE OBJECT

6 CREATE TABLE CREATE TABLE tableName(fields and data type separated by commas); Ex. –CREATE TABLE employee( eid CHAR(5), ename VARCHAR(40), sex CHAR, salary NUMERIC(9,2), hire_Date DATE);

7 Integrity Enhancement Feature Required data: NOT NULL –eid CHAR(5) NOT NULL, Default value:DEFAULT sex CHAR DEFAULT ‘M’, Field domain: CHECK(condition) –salary NUMERIC(9,2) CHECK (salary >= 100 AND salary <=10000), –sex CHAR DEFAULT ‘M’ CHECK (sex in (‘M’,’F”)), PRIMARY KEY –PRIMARY KEY(sid) –PRIMARY KEY(sid, cid) Unique – allow null value, the PRIMARY KEY constraint does not allow null. –ESSN CHAR(9) UNIQUE

8 –CREATE TABLE employee( eid CHAR(5) PRMARY KEY, ename VARCHAR(40), sex CHAR DEFAULT ‘M’ CHECK (sex in (‘M’,’F”)), salary NUMERIC(9,2), hire_Date DATE);

9 Composite Key Example create table orderdetail (oid char(3), cid char(5), qty numeric(5,2), primary key (oid,cid));

10 ALTER TABLE ADD/Modify/DROP COLUMN a new field from a table. Ex. ALTER TABLE employee ADD phone CHAR(8); ALTER TABLE employee Modify phone CHAR(9); ALTER TABLE employee DROP COLUMN Phone;

11 Adding Constraints with the ALTER TABLE command Constraints: –PRIMARY KEY, CHECK, UNIQUE PRIMARY KEY: –ALTER TABLE tablename –ADD CONSTRAINT constraintname –PRIMARY KEY (columnname); CHECK –ALTER TABLE tablename –ADD CONSTRAINT constraintname –CHECK (criteria);

12 Examples ALTER TABLE emp ADD CONSTRAINT empkey PRIMARY KEY (empid); ALTER TABLE emp ADD CONSTRAINT validSalary CHECK (salary between 100 AND 20000); Note: Constraints information are stored in table: USER_CONSTRAINTS. You can use the DESCRIBE command to show fields in this table.

13 Dropping Constraints ALTER TABLE tablename DROP CONSTRAINT constraintname;

14 Disable/Enable Constraints ALTER TABLE tablename DISABLE CONSTRAINT constraintname; ALTER TABLE tablename ENABLE CONSTRAINT constraintname;

15 Creating Table Through SubQuery CREATE TABLE tableName AS (Select query) Ex. –CREATE TABLE newEmp –AS (SELECT empid, ename,salary FROM emp);

16 Renaming a Table Rename oldName to newName

17 Removing a Table DROP TABLE tableName

18 SQL Insert Command INSERT INTO tableName VALUES (field values separated by commas); INSERT INTO tableName (Column names separated by commas)VALUES (field values separated by commas); Ex 1. Customer table with CID, CNAME, CITY, RATING. a. INSERT INTO CUSTOMER VALUES (‘C1’, ‘SMITH’, ‘SF’, ‘A’); b. INSERT INTO CUSTOMER (CID, CNAME,RATING) VALUES (‘C1’, ‘SMITH’, ‘A’);

19 Record with Date Field Oracle date format: –‘dd-mmm-yyyy’ Example: insert into orders values('O7','c2','s1','10-oct-2007');

20 Inserting records from an existing table INSERT INTO stu2 (select * from student);

21 SQL Delete Command DELETE FROM tableName [WHERE criteria]; Ex 1. Delete a record from the Customer table. DELETE FROM CUSTOMER WHERE CID = ‘C1’;

22 SQL Update Command UPDATE tableName SET field = new value [WHERE criteria]; Ex 1. UPDATE CUSTOMER SET RATING = ‘A’ WHERE CID=‘C1’; Ex 2. UPDATE CUSTOMER SET CITY = ‘SF’, RATING = ‘A’ WHERE CID=‘C1’;

23 Dealing with Null Null is a key word. We can use Null in the INSERT, UPDATE, and DELETE command. Use IS NULL (or IS NOT NULL) in a criteria. Examples: –INSERT INTO emp VALUES (‘e95’,’June’,’f’,NULL,5000); –UPDATE emp SET salary=null where empid=‘e99’; –SELECT * FROM emp WHERE salary IS NULL; –SELECT * FROM emp WHERE salary= NULL (not working);


Download ppt "Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with."

Similar presentations


Ads by Google