Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 SQL-2 Tarek El-Shishtawy Professor Ass. Of Computer Engineering.

Similar presentations


Presentation on theme: "1 SQL-2 Tarek El-Shishtawy Professor Ass. Of Computer Engineering."— Presentation transcript:

1 1 SQL-2 Tarek El-Shishtawy Professor Ass. Of Computer Engineering

2 2 DDL examples Creating tables CREATE TABLETableName (Column-definition [,Column-definition] ) ; Column-definition is ColumnNamedata-type[Constraint]

3 3 Naming tables Start with Alphabitical Start with Alphabitical Special characters as ( _, #, ) and numbers are allowed Special characters as ( _, #, ) and numbers are allowed Not case sensitive (emp, Emp, EMP) Not case sensitive (emp, Emp, EMP) Unique name within account Unique name within account Not an sql reserved word as Select, Update, …. Not an sql reserved word as Select, Update, …. Not more that 30 characters Not more that 30 characters

4 4 Example ًُExample for creating table PERSONS ًُExample for creating table PERSONS CREATE TABLE PERSONS ( CODENUMBER(3,0)NOT NULL, NAMEVARCHAR2(50)NOT NULL, NAMEVARCHAR2(50)NOT NULL, ADDRESSVARCHAR2(50) ADDRESSVARCHAR2(50)); Constraints Example NOT NULL NOT NULL PRIMAEY KEY PRIMAEY KEY

5 5 Default values CREATE TABLE INVOICE ( INV_NUMNUMBER(4,0)NOT NULL, INV_TOTALNUMBER(10,3)DEFAULT 0, INV_TOTALNUMBER(10,3)DEFAULT 0, INV_DATEDATEDEFAULT SYSDATE INV_DATEDATEDEFAULT SYSDATE ); );

6 6 Removing table Syntax DROP TABLE TableName ; Example DROP TABLE PERSONS; Note: Note: The difference between Drop and DELETE keywords The difference between Drop and DELETE keywords

7 7 Changing table design ALTER TABLE Adding new fields Adding new fields ALTER TABLE PERSONS ADD (IDNUMBERVARCHAR2(10) ); Modifying Field definition Modifying Field definition ALTER TABLE PERSONS MODIFY (IDNUMBER VARCHAR2(20));

8 8 Checking All tables in the current account All tables in the current account Select * from tab; Table design Table design Desc table_name;

9 9 Examples of DML

10 10 Inserting data Syntax Syntax INSERT INTO TableName [(ColumnName [,ColumnName]...)] VALUES(Value [,Value ]... ) ; Or Or INSERT INTO tablename (first_column,...last_column) (first_column,...last_column) values (first_value,...last_value); values (first_value,...last_value);

11 11 Inserting INSERT INTO PERSONS (CODE, NAME, ADDRESS, PERSONID, BIRTHDATE) VALUES (1,’Ahmed’, ‘Cairo’, ‘101-99’, ’12-SEP-1982’); Note: Default date is in format DD-MON-YYYY JAN, FEB, MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC To confirm write COMMIT; To confirm write COMMIT; To undo write ROLLBACK; To undo write ROLLBACK;

12 12 Selecting data from the table General Form General Form Select Column Names From Table Name [ Where Conditions] /* Optional where */ Examples for Examples for COURSE (CRS, CTITLE, CHRS, CHRSW) COURSE (CRS, CTITLE, CHRS, CHRSW) SELECT CRS, CTITLE FROM COURSE;

13 13 Example Continued SELECT CTITLE, CHRSW FROM COURSE; SELECT * FROM COURSE; SELECT CTITLE, CHRSW FROMCOURSE WHERECRS = 706;

14 14 Example Continued SELECT * FROMCOURSE WHERECHRS >= 60 AND CHRSW = 15; SELECT CTITLE, CHRSW FROMCOURSE WHERE CHRS >= 60 and CHRS = 60 and CHRS < =100;

15 15 Continued SELECT CTITLE, CHRSW FROMCOURSE WHERE CHRS BETWEEN 60 and 100; SELECT CTITLE, CHRSW FROMCOURSE WHERE CHRS >= 60 OR CRS = 706;

16 16


Download ppt "1 SQL-2 Tarek El-Shishtawy Professor Ass. Of Computer Engineering."

Similar presentations


Ads by Google