Download presentation
Presentation is loading. Please wait.
Published byCora Ramsey Modified over 9 years ago
1
Agenda for Class 2/02/2006 Finish discussing constraints generated with the CREATE TABLE statement. Discuss DROP statement. Discuss INSERT, COMMIT, DELETE, and UPDATE statements. Introduce SELECT statement. Discuss a few helpful SQLPlus commands. Do SQL class exercise.
2
Example 1: Simple Order Database discussed in class on Tuesday: We discussed in class how to create the tables with primary key and referential integrity constraints, now let’s add some other constraints.
3
CREATE TABLE ord (order_no NUMBER(5), order_date DATE, customer_no NUMBER(3) NOT NULL, ship_code CHAR(2) CHECK (ship_code in(‘02’,’A1’,’B3’,’04’)), CONSTRAINT ord_pk PRIMARY KEY (order_no)); CREATE TABLE prod (prod_no NUMBER(3), description VARCHAR2(50), cost NUMBER(8,2)CHECK (cost >.02), CONSTRAINT prod_pk PRIMARY KEY (prod_no)); CREATE TABLE prod_on_ord (order_no NUMBER(5), prod_no NUMBER(3), quantity NUMBER(6,2)DEFAULT 1, price NUMBER(8,2), CONSTRAINT prodord_pk PRIMARY KEY (order_no, prod_no), CONSTRAINT ord_fk FOREIGN KEY(order_no) REFERENCES ord(order_no), CONSTRAINT prod_fk FOREIGN KEY(prod_no) REFERENCES prod(prod_no));
4
What is a recursive referential integrity constraint? Imagine a situation where a company is a parent company of another company. The organization as a whole keeps track of company name, phone and the identifier of the parent company. CompanyIDNamePhoneParentCompanyID 123Dining Supply Co858-133-4551null 177DineOut775-677-6771123 897RestCo805-891-1233123 788Jansen Supply503-281-0667897
5
Creating a recursive referential integrity constraint CREATE TABLE company (companyIDCHAR(3), companynameVARCHAR2(40), phoneCHAR(10), parentcompanyIDCHAR(3), CONSTRAINT company_pk PRIMARY KEY (companyID), CONSTRAINT parent_fk FOREIGN KEY (parentcompanyID) REFERENCES company(companyID));
6
Deleting a table Cannot have more than one data object with the same name. Must delete data objects before re-creating them. SQL Statement is: DROP TABLE prod_on_ord; Must delete objects in the order of referential integrity constraints, unless the constraints are “cascaded” during the delete process. SQL Statement is: DROP TABLE prod_on_ord CASCADE CONSTRAINTS;
7
SQL INSERT Statement Used to “populate” a table with data. Used to enter one row of data. Character/string data must be entered surrounded by single quotes. Dates are best entered using the Oracle default format of dd-mon-yy or dd-mon-yyyy. For example, today’s date in the Oracle default format is: 02-feb-2006
8
INSERT INTO ord VALUES (14452, ’02-feb-06’, 234, ‘A1’); INSERT INTO ord VALUES (23415, SYSDATE, 2100, ‘B3’); INSERT INTO ord VALUES (14419, ’08-jul-1999’, 320, ‘02’); INSERT INTO ord (order_no, customer_no) VALUES (2231, 334); COMMIT; Using the original ord table example, these are sample INSERT statements used to add rows to the table
9
Purpose of the COMMIT Statement Some SQL statements act directly to save results on disk, others do not. CREATE produces a data object on disk; not just in main memory. INSERT adds a row of data in main memory. To save data from memory to disk, use the COMMIT statement.
10
Deleting Data from a Table DELETE FROM ord WHERE ord_no = 3344; DELETE FROM prod WHERE cost < 1.56; DELETE FROM prod_on_ord WHERE price < 1.25 AND quantity < 3;
11
Changing Existing Data in a Table UPDATEord SETorder_date = ’22-jan-06’ WHEREorder_no = 14452; UPDATEord SETorder_date = SYSDATE WHEREorder_no = 14452; UPDATEprod SETcost = 0 WHEREdescription = ‘bolt’;
12
Retrieving Data from a Table General Syntax for accessing data from a table SELECT [all or distinct] (what columns) FROM (table) WHERE (condition) GROUP BY (grouping fields) HAVING (condition) ORDER BY (sort fields)
13
Example of Retrieving Data from a Table SELECT* FROM ord; SELECT * FROM prod’ SELECT order_id, order_date FROM ord; SELECT order_id, order_date FROM ord WHERE cust_id = ‘6511’; The * means retrieve all columns. The FROM statement, without a WHERE statement, means retrieve all rows.
14
Retrieving Info from Oracle SQLPlus command to see structure of data object: DESCRIBEemp; SQL command to see contents of your tablespace: SELECT table_name FROM tabs; Tabs is a data view in the Oracle data dictionary containing information about all database objects in a given tablespace.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.