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
CREATE TABLE ( … [CONSTRAINT … ); CREATE TABLE bank_customer ( 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, State CHAR(2) not null, Zip CHAR(5) not null ); Intro to SQL| MIS 2502 No comma since last attribute Don’t need not null since PK cannot be null
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 ALTER TABLE customer ADD (customer_dob DATE); Modify an attribute Changing size of customer_add1 from 35 to 50 characters ALTER TABLE customer MODIFY (customer_add1 VARCHAR2(50));
Intro to SQL| MIS 2502 ConstraintDescriptionExample Primary Key Add unique identifierALTER 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); DefaultAdds a value to an attribute when a new record is entered unless user enters another value i.e. the state is filled with PA unless user enters another state ALTER TABLE bank_customer MODIFY state DEFAULT ‘PA’;
Intro to SQL| MIS 2502 ConstraintDescriptionExample CheckRequires 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’)) ); NullDetermines if a field can be left blank CANNOT alter null once entered.
Text must be entered in single quotes… ‘PA’ Intro to SQL| MIS 2502