Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structured Query Language Part II Chapter Three CIS 218.

Similar presentations


Presentation on theme: "Structured Query Language Part II Chapter Three CIS 218."— Presentation transcript:

1 Structured Query Language Part II Chapter Three CIS 218

2 3-2 Chapter Objectives SQL Data Definition –Create database structures Primary key constraints Foreign key constraints –Modify and delete database structures SQL Data Manipulation –Add data to a database –Modify and delete data from a database

3 3-3 SQL for Data Definition SQL data definition statements –CREATE To create database objects (e.g. tables) –ALTER To modify the structure and/or characteristics of database objects –DROP To delete/remove database objects

4 3-4 Creating a Table Creating database tables –The SQL CREATE TABLE statement CREATE TABLE Department( DeptID Integer PRIMARY KEY, DeptName Char(25)NOT NULL, MailStopChar(5)NULL ); CREATE TABLE Skill( SkillIDInteger, SkillNameChar(25) );

5 3-5 Composite Primary Keys Creating a table with a composite primary key using PRIMARY KEY constraints The SQL CONSTRAINT keyword CREATE TABLE EmpSkill( EmpID Integer NOT NULL, SkillID Integer NOT NULL, SkillLevel IntegerNULL, CONSTRAINT EmpSkill_PK PRIMARY KEY (EmpID, SkillID) );

6 3-6 Foreign Keys Creating database tables using PRIMARY KEY and FOREIGN KEY constraints CREATE TABLE Employee( EmpID Integer PRIMARY KEY, EmpName Char(25)NOT NULL, DeptIDIntegerNULL, CONSTRAINTDept_FKFOREIGN KEY(DeptID) REFERENCES Department(DeptID) );

7 3-7 Primary Key Constraints Add primary key constraints to an existing table The SQL ALTER statement ALTER TABLE Skill ADD CONSTRAINT Skill_PK PRIMARY KEY(SkillID);

8 3-8 Composite Primary Key Constraints Add a composite primary key constraint to an existing table The SQL ALTER statement ALTER TABLE EMP_SKILL ADD CONSTRAINT EmpSkill_PK PRIMARY KEY(EmpID, SkillID);

9 3-9 Foreign Key Constraints Add a foreign key constraint to an existing table The SQL ALTER statement ALTER TABLE EmpSkill ADD CONSTRAINT Emp_FK FOREIGN KEY(EmpID) REFERENCES Employee(EmpID);

10 3-10 Removing a Constraint Remove the existing constraints on a table DROP CONSTRAINT ALTER TABLE EmpSkill DROP CONSTRAINT Emp_FK;

11 3-11 Deleting Database Objects To remove unwanted database objects from the database, use the SQL DROP statement Warning! The DROP statement will permanently remove the object and all data DROP TABLE Skill;

12 3-12 SQL Data Manipulation Insert –Will add a new row in a table Update –Will update the data in a table that matches the specified criteria Delete –Will delete the data in a table that matches the specified criteria

13 3-13 Adding Data To add a row to an existing table, use the INSERT statement Enclose non-numeric data in single ( ' ) quotes –Access allows double quotes INSERT INTO Department VALUES (1, ‘Info Systems’, ’13-4’); INSERT INTO EMPLOYEE (EmpID, EmpName) VALUES(91, ‘Sanchez');

14 3-14 AutoNumber Fields You cannot use AutoNumber as a datatype AutoNumber fields must be set in Access design view You cannot insert data into an AutoNumber field –INSERT INTO Skill (SkillName) VALUES (‘MS Access’);

15 3-15 Changing Data Values To change the data values in an existing row (or set of rows) use the Update statement UPDATE Employee SET DeptID = 1 WHERE EmpID = 91; UPDATE Department SET MailStop = ’14-3’ WHERE DeptName LIKE ‘Info*’

16 3-16 Deleting Data To delete a row or set of rows from a table using the DELETE statement DELETE FROM EMPLOYEE WHERE EmpID = 91; DELETE FROM EMPLOYEE WHERE EmpName LIKE ‘*Sanchez' ;

17 3-17 Keyword Summary CREATE TABLE DROP TABLE ALTER TABLE INSERT UPDATE DELETE


Download ppt "Structured Query Language Part II Chapter Three CIS 218."

Similar presentations


Ads by Google