Chapter Five Objectives Insert Data into tables Create Query files to insert data into tables Make changes to the data in the tables Extract data from tables and insert them into another table Delete rows from tables
2 Example: DESC student; NAMENULLTYPE Name VARCHAR2(80) IdNUMBER(9) Gpa NUMBER(3,2) B_DateDATE MajorCHAR(4)
3 Inserting New Rows into a Table: (General format) INSERT INTO tablename (col1, col2,...) VALUES (value1, value2,.... ); Or INSERT INTO tablename(col1, col2, col3,….)` Subquery;
4 Inserting a New Row Add a New Student with ID= and Name= Sarah, GPA=0.0, and no Major: SQL> INSERT INTO Student(Name,ID,GPA) VALUES ('Sarah', ,0.0); 1 row created.
5 Practice: -Insert a new row into your table Custom-Insert a new row into your table Order-Examine your tablesSELECT * FROM custom; -Insert a new row into your table Custom-Insert a new row into your table Order-Examine your tablesSELECT * FROM custom; Insert a new row into your table Customer Insert a new row into your table Order Form Examine your tables SELECT * FROM custom;
6 Inserting Special Values: SQL> INSERT INTO student (ID,NAME, B_Date,GPA) VALUES (1111, 'GREEN', SYSDATE+18, 3.90); 1 row created.
7 SQL> SELECT * from student where ID=1111; NAME ID GPA B_DATE MAJOR GREEN NOV-02
8 Inserting a Date Value: SQL> INSERT INTO student (ID,Name,B_Date,GPA) VALUES (22222,'BLUE',TO_Date('JAN 03,79','MON DD,YY'),2.90);
9 SQL> SELECT * FROMstudent WHEREName=‘BLUE’; NAME ID GPA B_DATE MAJOR BLUE JAN-79
10 Practice: Insert a new row into your table order form using the SYSDATE for the value of the order date
11 Inserting Values By Using Substitution Variables: SQL> INSERT INTO student (ID,Name,GPA) VALUES (&student_id,'&student_name',&Gpa_input); Enter value for student_id: 1212 Enter value for student_name: RED Enter value for gpa_input: 3.30 old 2: VALUES (&student_id,'&student_name',&Gpa_input) new 2: VALUES (1212,'RED',3.30) 1 row created.
12 Creating a script customized prompts: ACCEPTstudentId PROMPT ‘Please enter a student ID: ‘; ACCEPTstudentName PROMPT ‘Please enter a student Name: ‘; ACCEPTGpaInput PROMPT ‘Please enter a Gpa: ‘; INSERT INTO student (Id, Name, B_Date, Gpa) VALUES(&studentId, ‘&studentName’, &GpaInput);
13 Inserting Rows Using Subquery: Create a Temp Table to Store COSC Students INSERT INTOTemp (Name, ID, GPA) (SELECTName, ID, GPA FROM Student WHEREMajor='COSC'); Note: you must have Temp table created before inserting data into it.
14 Update (Modify a Row) (General Format) UPDATEtable SETColumn = Expression [WHERECondition];
15 Example Changing ID of every one;(Multiply by 100 ) (Add two digits to student ID) UPDATEStudent SETID = ID * 100;
16 Update Change major of all COSC students to MATH. SQL> UPDATE Student SET Major='MATH' WHERE Major='COSC‘; 0 rows updated.
17 Practice: Change the city name in customer table from Frostburg to Cumberland using UPDATE command.
18 Deleting Rows: DELETE FROMtablename [WHERECondition];
19 Deleting Rows: Delete all the student's courses of student with ID= SQL> DELETE FROM Student WHERE ID= ; 1 row deleted. DELETE FROM department WHEREdept=’MATH’;
20 Practice: Delete rows from table Customer if balance is less than $50