Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.

Similar presentations


Presentation on theme: "Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems."— Presentation transcript:

1 Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems

2 Types of SQL Statements that will be covered: Data Definition Language (DDL): Commands that define a database. E.g. CREATE, ALTER, DROP,...etc. Data manipulation language (DML): Commands that maintain and query a database. E.g. SELECT, INSERT, UPDATE, DELETE

3 COMPANY Schema Given the following schema for a COMPANY database: EMPLOYEE DEPARTMENT FNAMEMINITLNAMESSNSALARYDNO DNAMEDNUMBERLOCATION

4 SQL*Plus Open SQL*Plus, enter appropriate name, password, & host string scott psu stddata

5 CREATE TABLE Structure CREATE TABLE [schema.]TABLENAME ( ColumnName Type [DEFAULT value] [Constraints], [TableConstraints] );

6 Types CHAR(n): a fixed-length string n characters long (default 1, max 255 character). VARCHAR(n): a variable string up to n character long (max 2000). INT: an integer number. DECIMAL(n,d): a number n digits long including d decimal places. DATE: a valid date YYYY-MM-DD.

7 COLUMN LEVEL CONSTRAINTS NOT NULL UNIQUE PRIMARY KEY REFRENCES table key Check TABLE LEVEL CONSTRAINTS UNIQUE (Column-list) CONSTRAINT name PRIMARY KEY column_list CONSTRAINT name FOREIGN KEY column-list REFRENCES table column-list CHECK (Condition) ON DELETE CASACADE, ON DELETE SET NULL

8 Write appropriate SQL DDL statements to declare the COMPANY schema. CREATE TABLE DEPARTMENT ( DNAMEVARCHAR2(20)NOT NULL UNIQUE, DNUMBERNUMBER(2), LOCATIONVARCHAR2(20), CONSTRAINT DEPT_PKEY PRIMARY KEY (DNUMBER)); CREATE TABLE EMPLOYEE ( FNAMEVARCHAR2(15)NOT NULL, MINIT CHAR, LNAMEVARCHAR2(15)NOT NULL, SSNCHAR(9), SALARYNUMBER(10,2)NOT NULL, DNONUMBER(2), CONSTRAINT EMP_PKEY PRIMARY KEY (SSN), CONSTRAINT EMP_FKEY FOREIGN KEY (DNO) REFERENCES DEPARTMENT (DNUMBER));

9 Display the structure of your tables. desc DEPARTMENT desc EMPLOYEE

10 Tables can be changed using ALTER TABLE

11 Add the following columns to the EMPLOYEE table:  ADDRESS  SEX ( Only accepts M, F) ALTER TABLE table_name ADD column_name datatype ALTER TABLE EMPLOYEE ADD ( ADDRESS VARCHAR2(30), SEX CHAR(1) CHECK (SEX IN ('M', 'F'))); Add/drop column

12 Remove column Address from table employee Alter table Employee Drop column address;

13 Change a column definition Modify ADDRESS so that NULL values are not accepted ALTER TABLE EMPLOYEE MODIFY ADDRESS NOT NULL;

14 Add/ Drop a constraint Drop Primary key from table employee alter table table_name drop constraint constraint_name; Alter table employee Drop constraint EMP_FKEY Add constraint to table employee alter table table_name add constraint constraint_name; ALTER TABLE employee ADD CONSTRAINT emp_pk Primary Key(SSN) ;

15 Insert the following tuple into the DEPARTMENT table: DNAME = ‘Project’ DNUMBER = 5 DLOCATION = ‘Houston’ INSERT INTO table_name VALUES (value1, value2, value3,...) INSERT INTO DEPARTMENT VALUES ('Project', 5, 'Houston');

16 Write an INSERT statement in which each time it is run, the user will be prompted for a new set of values (DEPARTMENT table) INSERT INTO DEPARTMENT VALUES ('&D_NAME', &D_NUMBER, '&D_LOCATION'); Execute the previous statement twice, and insert the following values: D_NAME = Administration D_NUMBER = 4 D_LOCATION = Stafford D_NAME = Headquarters D_NUMBER = 1 D_LOCATION = Bellaire


Download ppt "Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems."

Similar presentations


Ads by Google