Download presentation
Presentation is loading. Please wait.
Published byRosaline Baker Modified over 9 years ago
1
Your Oracle Account UserName is the same as your UWP username Followed by @EDDB Not case sensitive Initial Password: UWPstudent Password is case sensitive Reset password Email to Ken at wiegmake@uwplatt.edu from your UWP email account if you forget your password 1
2
Login to Your Oracle Account SQL*Plus Oracle 11 All labs in Ullrich Not available from home 2
3
Change Your Oracle Password -- Oracle Command Prompt SQL> -- Change your password -- Remember your new password! SQL> Alter User yourUserName identified by newPassword; -- Every ANSI SQL standard command ends with ; 3
4
Structured Query Language (SQL) One language for Relational Databases ANSI Standard Oracle: SQL*Plus MS SQL Server: Transact-SQL IBM DB2 MySQL Sybase... 4
5
Structured Query Language (SQL) Case insensitive (like VB) Free style (like C++ and Java) Statement terminator – semicolon (like C++ and Java) Programming Rule: Style Each clause of a query on a separate line When creating tables Each field on a separate line Each table constraint on a separate line 5
6
Structured Query Language (SQL) DDL (Data Definition Language) Create Table (user)... Drop Table (user)... Alter Table (user)... DML (Data Manipulation language) Select * From Branch … Insert into Branch... Update branch... Delete from BRANCH... 6
7
Oracle Data Types Char(size) fixed length string up to 2000 bytes default is 1 byte blanks are padded on right when fewer chars entered Varchar2(size) variable size string up to 2000 bytes must specify the limit (size) Varchar(size) same as Varchar2(size) better to use Varchar2 7
8
Oracle Data Types Integer, int, smallint Float Date Valid dates from 1-Jan-4712 B.C. to 31-Dec-4712 A.D. Default format: DD-MON-YY 23-Mar-09 Including time 8
9
Oracle Data Types Number(l, d) l: length (total) d: decimal digits number (5, 2): largest value is 999.99 Decimal(l, d), Numeric(l, d) same as number(l, d) SQL standard blob: binary large object, up to 4 GB clob: character large object, up to 4 GB raw(size): raw binary data, up to 2000 bytes... 9
10
Create a Table SQL> Create Table Test1 ( C1 char(5) Primary Key, C2 Varchar2(50), C3 Integer, C4 Date); 10
11
Show Table Schema SQL> Describe Test1 Or SQL> Desc Test1 Describe is a SQL*Plus command and no semicolon is required. 11
12
Insert Records Insert into Test1 Values (‘cs363', ‘s1', 44, ‘28-feb-12’); Insert into Test1 Values (‘cs334', ‘s2', 45, ‘29-feb-12’); One record at a time! 12
13
Retrieve Records Select * From test1; -- Each clause on a separate line Select * From Test1; 13
14
Entity Integrity Insert into Test1 Values (‘cs363', ‘q2', 17, ‘2-Mar-12’); PK must be Unique! 14
15
Null Values Insert into Test1 Values (‘cs387', ‘q2', 17, null); Null is a key word, not a string! NULL, null and Null are the same SQL is not case sensitive 15
16
Entity Integrity Insert into Test1 Values (null, ‘q2', 17, ‘2-Mar-12’); PK cannot be null! 16
17
Command Commit DDL commands are sent back to server and executed there DML commands are executed at client site Use commit to send results back to server 17
18
Update Records Update test1 Set c3 = 50 Where c1 = ‘cs363’; 18
19
Delete Records Delete from test1 Where c1 = ‘cs363’; -- select to check Delete from test1; -- select to check -- desc to check 19
20
Drop Table Drop table test1; Desc test1 -- to check ERROR: ORA-04043: object test1 does not exist 20
21
Log out SQL> exit 21
22
Schedule Assignment 7 Due Monday, March 18 at 5 pm Test 1 Wednesday, March 20 Lab 206 22
23
Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing (Rno, Pno, ViewDate…) 23
24
In what order to create the tables? Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing (Rno, Pno, ViewDate…) Will it work? YES! Branch (Bno…) Staff (Sno…Bno) PropertyForRent (Pno…Ono) Owner (Ono…) Viewing (Rno, Pno, ViewDate…) Renter (Rno…) Will it work? NO! 24
25
In what order to drop the tables? Viewing (Rno, Pno, ViewDate…) Staff (Sno…Bno) PropertyForRent (Pno…Ono) Owner (Ono…) Renter (Rno…) Branch (Bno…) Will it work? YES! Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing (Rno, Pno, ViewDate…) Will it work? NO! 25
26
Insert Rows The PK must be unique The foreign key value must exist in the parent table if FK is provided FK can be null, meaning not known at the time 26
27
Insert Rows Insert into Branch: no problem Insert into Staff: Cannot insert a staff with Bno being ‘B123’ if Branch table has no ‘B123’ in Bno column FK can be null, meaning not known at the time Branch BnoPhone… B101 B205 Staff SnoPhoneBno SG100B101 SG363Null SA200 27
28
Delete Rows No records in other tables reference the record to be deleted. 28
29
Delete Rows Delete from Staff: no problem Delete from Branch: Delete branch 'B101' What about staff in 'B101‘? Branch BnoPhone… B205 Staff SnoPhoneBno SG100B101 SG363Null SA200B101 29
30
ANSI SQL Solutions In ANSI SQL, there are five choices No Action Cannot delete Set to Null Set to Default No Check No good Cascade Delete all staff in 'B101' from Staff table when deleting branch ‘B101’ Dangerous! 30
31
Oracle Solutions 31
32
Update Record Update table Staff ‘B101’ of SG100 to ‘B205’ New value must exist in Branch Update table Branch ‘B101’ to ‘B303’ Five choices in ANSI SQL, only No Action is implemented in Oracle Branch BnoPhone… B101 B205 Staff SnoPhoneBno SG100B101 SG363Null SA200B205 32
33
Integrity Rules: Constraints Column Constraints Table Constraints 33
34
Column Constraints Primary key Sno Char(4) Primary Key, Alternate Key SSN Char(9) Unique, Foreign Key BNo char(4) References Branch, -- when attribute has the same name BNo char(4) References Branch(B_no), -- when attribute has different names BNo char(4) References Branch on Delete Cascade, -- Do NOT use “Foreign Key” in column constaints! 34
35
Column Constraints Domain constraint Salary Number Check (Salary > 10000 and Salary < 200000), PType varchar2(6) Check (PType in ('House', 'Flat', 'Appt')), -- Strings are in single quotes, NOT double quotes Bno Char(4) Default 'B363' References Branch, Rent Float Check (Rent Between 200 and 400), -- between is Inclusive Required data LName Varchar2(20) Not Null, -- Can be specified only by column constraint 35
36
Column Constraints Create table Staff ( SNo char(4) Primary Key, Bno Char(4) Default 'B363' References Branch on Delete Cascade, FName Varchar2(20) Not Null, LName Varchar2(20) Not Null, -- assuming functions DateDiff and Now DOB Date Not Null Check (DateDiff(Year, Now, DOB) >= 16), Salary Number Check (Salary Between 30000 and 100000), SSN Char(9) Unique, Tel_No Char(12)); -- Primary Key, Unique, References should be the last constraint for a column -- Do not use Foreign key for column constraints, only use References 36
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.