Intro to SQL

advertisement
Intro to SQL| MIS 2502
Function
Command
Exit
exit
Undo
Rollback – only undoes to last save
Save changes
commit
Intro to SQL| MIS 2502

Spacing not relevant
› BUT… no spaces in an attribute name or
table name
Oracle commands keywords, table
names, and attribute names not case
sensitive
 Entering and retrieving string values are
case sensitive
 To end sentence -> ;

Intro to SQL| MIS 2502
SQL plus’ editor is a pain!
 Enter code in notepad and then copy
and paste at prompt in SQL plus
 Hit enter to run command

Intro to SQL| MIS 2502
Drop table
 Cascade constraints!!! – enforces that no
delete anomolies


DROP TABLE manager CASCADE
CONSTRAINTS;
Intro to SQL| MIS 2502
Char
 Varchar
 Number
 Date
 Integer

Intro to SQL| MIS 2502
CREATE TABLE <tablename>
(<fieldname> <data declaration> …
[CONSTRAINT <integrity constraint declaration>…
);
Don’t need not
null since PK
CREATE TABLE bank_customer
cannot be null
(
Customer_ID NUMBER(5) constraint customer_id primary key,
Customer_LastName VARCHAR2(30) not null,
Customer_FirstName VARCHAR2(30) not null,
Customer_MI Char(1) null,
Add1 VARCHAR2(30) not null,
City VARCHAR2(25) not null,
No comma
State CHAR(2) not null,
since last
Zip
CHAR(5) not null
attribute
);
Intro to SQL| MIS 2502

Describe tablename ->
describe customer
Intro to SQL| MIS 2502

Can change following in a table:
› attribute names, size, data types
› Remove and add attributes
Cannot change a field from null to not
null.
 For FK – can’t reference a field that
doesn’t exist. That’s why I’ve had you
add the tables and attributes first and
then reference them with the Alter
command

Intro to SQL| MIS 2502
Add an attribute
Here adding the
customer_dob attribute to
customer table
Modify an attribute
Changing size of
customer_add1 from 35 to
50 characters

Intro to SQL| MIS 2502
ALTER TABLE
customer ADD
(customer_dob
DATE);
ALTER TABLE
customer MODIFY
(customer_add1
VARCHAR2(50));
Constraint
Description
Example
Primary
Key
Add unique identifier
ALTER table customer
ADD (PRIMARY KEY
(customer_id));
Foreign
Key
Add foreign key to another
table
ALTER TABLE
bank_customer
ADD FOREIGN KEY
(state)
REFERENCES
state(stateid);
Default
Adds a value to an attribute
when a new record is
entered unless user enters
another value
ALTER TABLE
bank_customer
MODIFY state DEFAULT
‘PA’;
i.e. the state is filled with PA
unless user enters another
state
Intro to SQL| MIS 2502
Constraint
Description
Example
Check
Requires user to
enter one of the
accepted values
for a field
(analogous to a
pick list)
Alter table bank_customer
ADD
(CHECK
(city IN (‘Philadelphia’,
‘Abington’, ‘Jenkintown’))
);
Null
Determines if a
field can be left
blank
CANNOT alter null once
entered.
Intro to SQL| MIS 2502

Text must be entered in single quotes…
‘PA’
Intro to SQL| MIS 2502
Download