Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.

Similar presentations


Presentation on theme: "Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة."— Presentation transcript:

1 Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة من الادوات في ذلك. حيث المرجع الاساسي للمادة هي الكتاب المعتمد في وصف المقرر

2 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe Basic SQL SQL Considered one of the major reasons for the commercial success of relational databases Statements for data definition (DDL) and data manipulation (DML)

3 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe Slide 8- 3 SQL DDL CREATE statement Main SQL command for DDL

4 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe Schema Concepts in SQL SQL Schema Identified by a schema name and authorization identifier to indicate who own the schema Schema elements include Tables, constraints, views, and domains Each statement in SQL ends with a semicolon Example CREATE SCHEMA statement CREATE SCHEMA COMPANY AUTHORIZATION ‘Jsmith’;

5 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe The CREATE TABLE Command Specify a new relation Provide name Specify attributes and initial constraints Can optionally specify schema: CREATE TABLE COMPANY.EMPLOYEE... or CREATE TABLE EMPLOYEE...

6 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe The CREATE TABLE Command (cont’d.) Base tables (base relations) Relation and its tuples are actually created and stored as a file by the DBMS Virtual relations Created through the CREATE VIEW statement

7 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe Slide 8- 7 CREATE TABLE

8 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe Slide 8- 8

9 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe Specifying Constraints in SQL Basic constraints: Key and referential integrity constraints Restrictions on attribute domains and NULLs Constraints on individual tuples within a relation Slide 8- 9

10 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe Specifying Attribute Constraints and Attribute Defaults NOT NULL NULL is not permitted for a particular attribute Default value DEFAULT CHECK clause Dnumber INT NOT NULL CHECK (Dnumber > 0 AND Dnumber < 21); Keyword CONSTRAINT Name a constraint Useful for later altering Slide 8- 10

11 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe Slide 8- 11

12 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe Specifying Key and Referential Integrity Constraints PRIMARY KEY clause Specifies one or more attributes that make up the primary key of a relation Dnumber INT PRIMARY KEY; UNIQUE clause Specifies alternate (secondary) keys Dname VARCHAR(15) UNIQUE; Slide 8- 12

13 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe Specifying Key and Referential Integrity Constraints (cont’d.) FOREIGN KEY clause Default operation: reject update on violation Attach referential triggered action clause Options include SET NULL, CASCADE, and SET DEFAULT Slide 8- 13

14 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe Specifying Constraints on Tuples Using CHECK CHECK clauses at the end of a CREATE TABLE statement Apply to each tuple individually Example: Suppose that the table department contains additional attribute for holding the date of creation of the department, so we can add this check at the end of create statement CHECK ( Dept_create_date <= Mgr_start_date );

15 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe The DROP Command DROP command Used to drop named schema elements, such as tables or constraint Drop behavior options: CASCADE and RESTRICT

16 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe DROP SCHEMA Example: To remove the company database schema and all its tables, domains, and other elements the cascade option is used DROP SCHEMA COMPANY CASCADE; If the RESTRICT option is used, the schema is dropped only if it has no elements in it; otherwise, the DROP command will not be executed

17 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe Slide 8- 17 DROP TABLE Used to remove a relation 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 CASCADE ; WITH CASCADE OPTION ALL CONSTAINTS AND VIEWS THAT REFERENCE THE TABLE ARE DROPED AUTOMATICALLY FROM THE SCHEMA WITH RESTRICT OPTION The table will dropped only if it is not referenced in any constraints

18 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe The ALTER Command Alter table actions include: Adding or dropping a column (attribute) Changing a column definition Adding or dropping table constraints

19 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe The ALTER Command Example: ALTER TABLE COMPANY.EMPLOYEE ADD COLUMN Job VARCHAR(12); The value of the new attribute is set using The DEFAULT value Or the UPDATE command If no default clause is specified, the new attribute will have a NULL in all the tuples of the relation immediately after the command is executed Hence the NOT NULL constraint is not allowed in this case

20 Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe The ALTER Command (cont’d.) Remove the attribute ALTER TABLE compny.employee DROP COLUMN Address CASCADE; Remove the default value ALTER TABLE compny.department ALTER COLUMN Mgr_ssn DROP DEFAULT; Change the default value ALTER TABLE compny.department ALTER COLUMN Mgr_ssn SET DEFAULT “333445555”; Remove the constraint ALTER TABLE compny.employee DROP CONSTRAINT empsuperfk CASCADE;


Download ppt "Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة."

Similar presentations


Ads by Google