CH 7 (Summary): SQL

CH 7 (Summary): SQL

<p>CH 7 (Summary): SQL --a standard for relational DB --used as DDL, DML --portable</p><p>Benefits: .reduced training costs .enhanced productivity .application portability .multiple vendors .application longevity .cross systems communications</p><p>Creating DB Structure: steps</p><p>.create SCHEMA .create TABLES .create VIEWS .create indexes etc.</p><p>SCHEMA: an overall description of database structure where Tables will reside</p><p>CREATE SCHEMA name of schema: or</p><p>CREATE DATABASE dbname;</p><p>ORACLE schema is created for you for home work assignments == CREATE TABLEs:..SEE Pg 222 [RC]; ORACLE HANDOUT; and lesson 17 (BF)</p><p>..define fields ..field type ..length ..null/not null ..unique ..primary key(s) ..foreign keys Format:</p><p>CREATE TABLE tablename (col name datatype col_constraints, . . . [primary key block] [referential constraint] . . .); constraints in Create table statements:</p><p>Constraint clause can constrain a single column or a group of columns in a table</p><p>It helps maintain data integrity</p><p>Primary key Foreign key Check conditions</p><p>Two ways to specify constraints: ..part of col def (col constraint) ..at the end of the table (table constraint)</p><p>Primary key:</p><p>A table can have only ONE primary key P.K. can not have NULL values for single-column primary key, it can be defined with a column constraint ex:</p><p>WORKER (w_name, age, lodging) Create table WORKER (w_name varchar(25) Primary key, age number, Lodging varchar(15)); or Create table WORKER ( w_name varchar(25), age number, Lodging varchar(15), primary key (name)); ex: Concatenated or composite PK</p><p>ENROLL(st_name, cl_name, grade)</p><p>PK is st_name, cl_name</p><p>Create table Enrollment ( ST_name varchar(30) not null, cl_name varchar(25) not null, grade smallint primary key (st_name, cl_name));</p><p>NOTE: A concatenated PK must be defined at the END</p><p>Foreign Key: (SEE ORACLE HANDOUT AND PAGE 225-227 (RC), LESSON 22 upto page 186 only (bf) known as referential integrity constraint ex: WORKER (name, age, lodging) LODGING (lodging_name , No_of_rooms, lodging_Address)</p><p>Lodging in table WORKER is a FK to lodging_name in table LODGING</p><p>Create table WORKER( name... age... lodging..REFERENCES LODGING); will automatically reference PK of LODGING relation or foreign key(lodging) references LODGING(lodging_name) CHECK constraint: lesson 22 upto page 186 only(bf) many cols must have a value within certain range or to satisfy certain conditions, with a check constraint we can do that for every value in that column format: col type CHECK (cod) cod is any valid expression that tests True/false. ex: if age should be between 18 & 65 create table WORKER (name.. age number check(age between 18 and 65), lodging...); or age...check (age > 12), ex: class char(20) not null check (class in ('SO', 'jr', 'sn') )</p><p>Data type: (see ORACLE handout)</p><p>NUMBER INTEGER INTEGER(size) CHAR (size) LONG DATE VARCHAR2(size) VARCHAR(size) NUMBER: upto 40 digits, plus a space for a sign and dec. can be represented in regular notation, i.e., 0-9, +/- and a decimal or SCIENTIFIC notation ...NUMBER(size) col of specified size ...NUMBER(size,d) specified size with d digits after decimal. NUMBER(5,2) can have nothing larger than (999.99)</p><p>INTEGER: same as NUMBER but does not accept decimal digits INTEGER(size):integer of specified size) CHAR(size): fixed-length character data. max size is 255 LONG: char data of variable size upto 2Gb long. Only one LONG column may be defined per table. Can not be used in subqueries, function expressions, where clauses or indexes</p><p>VARCHAR2(size):variable length char with max of size bytes (upto 2000)</p><p>DATE: valid dates range from jan 1, 4712 B.C. to dec 31, 4712 A.D.</p><p>ORACLE default format: 01-Jan-05 can work with date and time</p><p>Blob: binary large objects Can store multimedia applications like graphics, audio, video etc..</p><p>DATA NAMES:</p><p>. can be upto 30 char. long .begin with a letter .not case sensitive .underscores are allowed .no reserved words</p><p>DDL commands:</p><p> .CREATE TABLE  .DROP TABLE  .ALTER TABLE  .CREATE INDEX  .DROP INDEX  .CREATE VIEW  .DROP VIEW  .CREATE SYNONYM  .DROP SYNONYM  .CREATE CLUSTER  .DROP CLUSTER</p><p>Drop Table: allows user to delete a table from the schema format: DROP TABLE table-name ex: ORDER(O#, O_date, Promised_date, C#)</p><p>CUSTOMER(C#, name, address, discount)</p><p>CREATE TABLE ORDER (ORDERNO INTEGER NOT NULL, ORDERDATE DATE, PROMISED_DATE DATE, CUSTOMER_NO INTEGER NOT NULL REFERENCES CUSTOMER (c_no), PRIMARY KEY(ORDERNO));</p><p>ADDITIONAL TABLE FEATURES: CREATE SYNONYM:</p><p>FORMAT:</p><p>CREATE SYNONYM synname for tablename ex: create synonym MIS_STUDENT for STUDENT;</p><p>DROP synonym MIS_STUDENT</p><p>COMMIT/ROLLBACK: used after insert, delete or update operations. Important when an error is discovered commit: saves work of the session rollback: restores database to its original content since last commit</p><p>Oracle allows AUTOCOMMIT set autocommit show autocommit autocommit off: (default)</p><p>Altering Table structure(see ORACLE Handout), lesson 17 p 247- (RC) all changes in table structure are done thru ALTER command</p><p>.MODIFY allows changing existing column</p><p>.ADD allows adding new fields</p><p>NOTE: DELETEs are NOT allowed</p><p>ALTER format:</p><p>ALTER TABLE tablename [ADD (col name..., colname...)] or</p><p>ALTER TABLE tablename [MODIFY (col name..)] rules for ADDition:</p><p>.can add a col anytime if not null is not specified</p><p>.can add multiple col at one time</p><p>.can add a NULL column in three steps:</p><p>.add the col without not null .fill every row .modify col to not null ex: want to ADD a new column ship_add to table ORDER</p><p>ALTER table ORDER ADD (ship_add char(30) not null); will give errors, since nOT NULL is NOT allowed getting around the ADD problem: step 1: alter table order add (ship_add char(30)); step2: update table order set ship_add = 'UB'; step 3: alter table order modify (ship_add not null); Entering Data in tables: lesson 15 (BF), p230(RC) and oracle handout format:</p><p>INSERT INTO table-name VALUES ( val1, val2, val3...) values must be in the same order as the cols in the table structure char must be in single quotes date must be in quotes and default format (see ORACLE handout) ex: insert into order values (61396,’6-jun-97', '29-jun-97',1256); you can also insert a NULL value in a column..col will be left empty for this row insert into order values(61396,'6-jun-97', NULL,1256); ======Changing Table contents (NOT TABLE STRUCTURE): lesson 17(BF), p234-(RC)</p><p>DELETE..used to remove rows from table</p><p>UPDATE: changes specific values for each column we wish to change delete format:</p><p>DELETE FROM tablename condition; delete from order where cust_num = 1234; you can rollback to undo this</p><p>UPDATE format: UPDATE tablename SET values WHERE conditions; UPDATE ORDER SET Promised_date='12-jul-97' Where cust_no=1256);</p><p>QUERIES:</p><p>SQL can be used as a DML to manipulate data: format: SELECT col names</p><p>FROM table names</p><p>[Where condition(s)] [Group BY..][HAVING.] [UNION\INTERSECT\MINU S [ORDER BY....]</p><p>Additional readings</p><p>Http://otn.oracle.com http://www.sqlcourse.com/</p><p> http://www.w3schools.com/sql/default.asp</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    10 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us