Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL for Database Construction and Application Processing

Similar presentations


Presentation on theme: "SQL for Database Construction and Application Processing"— Presentation transcript:

1 SQL for Database Construction and Application Processing
Chapter 7 SQL for Database Construction and Application Processing

2 First create the database

3 New types of SQL statements

4 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

5 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

6 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

7 Data Type Reminder – SQL Server

8 Data Type Reminder – Oracle

9 Let’s try to create the WORK table

10 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

11 Table creation and deletion order
Create parents before children Delete children before parents

12 Relationship Definitions

13 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

14 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

15 Results

16 ALTER statements ALTER statement changes table structure, properties, or constraints after it has been created

17 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;

18 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];

19 More ALTER examples ALTER TABLE CUSTOMER_ARTIST_INT
DROP CONSTRAINT Customer_Artist_Int_CustomerFK; ALTER TABLE [TRANSACTION] DROP CONSTRAINT TransactionCustomerFK; DROP TABLE CUSTOMER;

20 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;


Download ppt "SQL for Database Construction and Application Processing"

Similar presentations


Ads by Google