SQL for Database Construction and Application Processing Chapter 7 SQL for Database Construction and Application Processing
First create the database
New types of SQL statements
SQL vs Graphical tools Quicker Facilitates recreation of same tables needed by applications Some applications need you to create temporary tables which require SQL code SQL is standardized and DBMS independent Except for some data types Point 2: when reporting, querying, and data mining
Tables from View Ridge Database Design of Chapter 6 Identity specifies surrogate key (starting at 1, incrementing by 1) Identity (500,1) specifies surrogate key starting at 100 going up by 1 Identity works for SQL Server. Oracle, MySQL and ACCESS use different techniques Everything in this chapter runs on SQL Server
SQL code to create Constarints define the primary key, and a candidate or alternate key. Primary purpose of alternate keys Is to ensure uniqueness of column values; thus alternate keys are defined using the UNIQUE command Each column is defined by giving its name, data type and null status. If you do not specify, NULL is assumed Numeric (4,0) means a 4 digit number with zero places to the right of the decimal point. Primary keys must be NOT NULL Candidate keys can be NULL or NOT NULL SQL statements are not case sensitive – upper case is historical from punch card era Commas are separators Statements are terminated by semicolons
Data Type Reminder – SQL Server
Data Type Reminder – Oracle
Let’s try to create the WORK table
WORK. ArtistID must exist in ARTIST WORK.ArtistID must exist in ARTIST.ArtistID is equivalent to foreign key constraint above DELETE CASCADE would cascade deletions DELETE NO ACTION is the default
Table creation and deletion order Create parents before children Delete children before parents
Relationship Definitions
What would the default values & data constraints be for View Ridge? Table Name Column Default Value Constraint WORK title copy original ARTIST DeceasedDate DeceasedDate must follow BirthDate ArtistID Can’t be null Name must be unique Nationality Angola Birthdate birthDate has to precede current date; birthdate must precede deceasedDate
CHECK CONSTRAINTS Similar to SQL WHERE IN – provides list of valid values NOT IN – for negatively expressed constraints LIKE – used for specification of decimal values
Results
ALTER statements ALTER statement changes table structure, properties, or constraints after it has been created
Examples ALTER TABLE ASSIGNMENT ADD CONSTRAINT EmployeeFK FOREIGN KEY (EmployeeNum) REFERENCES EMPLOYEE (EmployeeNumber) ON UPDATE CASCADE ON DELETE NO ACTION; ALTER TABLE CUSTOMER ADD MyColumn Char(5) NULL; ALTER TABLE CUSTOMER DROP COLUMN MyColumn;
More ALTER examples DROP TABLE [TRANSACTION]; ALTER TABLE CUSTOMER ADD CONSTRAINT MyConstraint CHECK ([Name] NOT IN ('Robert No Pay')); ALTER TABLE CUSTOMER DROP CONSTRAINT MyConstraint; DROP TABLE [TRANSACTION];
More ALTER examples ALTER TABLE CUSTOMER_ARTIST_INT DROP CONSTRAINT Customer_Artist_Int_CustomerFK; ALTER TABLE [TRANSACTION] DROP CONSTRAINT TransactionCustomerFK; DROP TABLE CUSTOMER;
INSERT examples INSERT INTO ARTIST ([Name], Nationality, Birthdate, DeceasedDate) VALUES ('Tamayo', 'Mexican', 1927, 1998); INSERT INTO ARTIST ([Name], Nationality, Birthdate) SELECT [Name], Nationality, Birthdate FROM IMPORTED_ARTIST;