Presentation is loading. Please wait.

Presentation is loading. Please wait.

7 7 SQL Data Definition 4Spread throughout chapter 7.

Similar presentations


Presentation on theme: "7 7 SQL Data Definition 4Spread throughout chapter 7."— Presentation transcript:

1 7 7 SQL Data Definition 4Spread throughout chapter 7

2 7 7 Data Definition Commands 4The Database Model u Simple Database -- PRODUCT and VENDOR tables l Each product is supplied by only a single vendor. l A vendor may supply many products. Figure 3.1

3 7 7 The Database Model

4 7 7 Data Definition Commands 4The Tables and Their Components u The VENDOR table contains vendors who are not referenced in the PRODUCT table. PRODUCT is optional to VENDOR. u Existing V_CODE values in the PRODUCT table must have a match in the VENDOR table. u A few products are supplied factory-direct, a few are made in-house, and a few may have been bought in a special warehouse sale. That is, a product is not necessarily supplied by a vendor. VENDOR is optional to PRODUCT.

5 7 7 SQL Data Definition Commands

6 7 7

7 7 7 Data Definition Commands 4Creating the Database Structure CREATE SCHEMA AUTHORIZATION ;  Example: CREATE SCHEMA AUTHORIZATION JONES; CREATE DATABASE ;  Example: CREATE DATABASE CH3;

8 7 7 A Data Dictionary for the CH06 Database Table 6.3

9 7 7 Data Types 4Each attribute will be defined to have a particular data type 4Data type selection is usually dictated by the nature of the data and by the intended use 4Pay close attention to the expected use of attributes for sorting and data retrieval purposes

10 7 7 Some Common SQL Data Types NumericNUMBER(L,D) INTEGER SMALLINT DECIMAL(L,D) CharacterCHAR(L) VARCHAR(L) DateDATE Data TypeFormat

11 7 7 Data Definition Commands 4Creating Table Structures CREATE TABLE ( );

12 7 7 DDL - Create Table CREATE TABLE STUDENT ( SSN CHAR(9) NOT NULL, FNAME VARCHAR(15) NOT NULL, MIDINIT CHAR, LNAME VARCHAR(15) NOT NULL, YEAR CHAR(2) NOT NULL DEFAULT ‘Fr’, MAJOR CHAR(4) DEFAULT ‘Und’, CREDIT INT DEFAULT 0, GPA DECIMAL(4,2), HOMETOWN VARCHAR(15), BALANCE DECIMAL(7,2) DEFAULT 0, CONSTRAINT STDPK PRIMARY KEY (SSN) )

13 7 7 DDL 4Foreign Keys CREATE TABLE enrollments (classindex INT NOT NULL, stdssn CHAR(9) NOT NULL, CONSTRAINT ENRLPK PRIMARY KEY(CLASSINDEX,STDSSN), CONSTRAINT ENRLSECT FOREIGN KEY (CLASSINDEX) REFERENCES SECTION(INDEX) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT ENRLSTUD FOREIGN KEY (STDSSN) REFERENCES STUDENT(SSN) ON DELETE CASCADE ON UPDATE CASCADE ) 4Domains CREATE DOMAIN ZIP_CODE_TYP AS CHAR(9);

14 7 7 Data Definition Commands CREATE TABLE VENDOR (V_CODEFCHAR(5)NOT NULLUNIQUE, V_NAMEVCHAR(35)NOT NULL, V_CONTACTVCHAR(15)NOT NULL, V_AREACODEFCHAR(3)NOT NULL, V_PHONEFCHAR(3)NOT NULL, V_STATEFCHAR(2)NOT NULL, V_ORDERFCHAR(1)NOT NULL, PRIMARY KEY (V_CODE));

15 7 7 Data Definition Commands CREATE TABLE PRODUCT( P_CODEVCHAR(10)NOT NULLUNIQUE, P_DESCRIPTVCHAR(35)NOT NULL, P_INDATEDATENOT NULL, P_ONHANDSMALLINTNOT NULL, P_MINSMALLINTNOT NULL, P_PRICEDECIMAL(8,2)NOT NULL, P_DISCOUNTDECIMAL(4,1)NOT NULL, V_CODESMALLINT, PRIMARY KEY (P_CODE), FOREIGN KEY (V_CODE) REFERENCES VENDOR ON DELETE RESTRICT ON UPDATE CASCADE);

16 7 7 Other SQL Constraints 4NOT NULL constraint u Ensures that a column does not accept nulls 4UNIQUE constraint u Ensures that all values in a column are unique 4DEFAULT constraint u Assigns a value to an attribute when a new row is added to a table 4CHECK constraint u Validates data when an attribute value is entered

17 7 7 Data Definition Commands 4SQL Integrity Constraints u Entity Integrity l PRIMARY KEY l NOT NULL and UNIQUE u Referential Integrity l FOREIGN KEY l ON DELETE l ON UPDATE

18 7 7 Part of Figure 6.2 The Contents of the PRODUCT Table

19 7 7 Domains 4Domain is a set of permissible values for an attribute. 4Defining a domain requires: u Name u Data type u Default value u Domain constraint or condition CREATE DOMAIN depttyp AS CHAR(4); 4 Then can use in defining any attributes of that type – one benefit, we can ensure that we define the PK and FK the same way 4Syntax: CREATE DOMAIN AS DEFAULT CHECK

20 7 7 SQL Indexes 4When a primary key is declared, DBMS automatically creates a unique index 4Often need additional indexes 4Using the CREATE INDEX command, SQL indexes can be created on the basis of any selected attribute

21 7 7 CREATE INDEX P_CODEX ON PRODUCT(P_CODE); CREATE UNIQUE INDEX P_CODEX ON PRODUCT(P_CODE); 4Composite index u Index based on two or more attributes CREATE INDEX meetingX ON SECTION ( Sem, Year, Time, Room); u Often used to prevent data duplication SQL Indexes

22 7 7 DDL 4DROP SCHEMA 4DROP TABLE 4ALTER TABLE

23 7 7 Advanced Data Definition Commands 4All changes in the table structure are made by using the ALTER command u Followed by a keyword that produces specific change u Three options are available l ADD l MODIFY l DROP

24 7 7 Advanced Data Management Commands 4Changing Table Structures ALTER TABLE MODIFY ( ); ALTER TABLE ADD ( );

25 7 7 Changing a Column’s Data Type 4ALTER can be used to change data type 4Some RDBMSs (such as Oracle) do not permit changes to data types unless the column to be changed is empty ALTER TABLE PRODUCT MODIFY (V_CODE CHAR(5));

26 7 7 Changing a Column’s Data Characteristics 4Use ALTER to change data characteristics 4If the column to be changed already contains data, changes in the column’s characteristics are permitted if those changes do not alter the data type 4 ALTER TABLE PRODUCT MODIFY (P_PRICE DECIMAL(9,2));

27 7 7 Adding or Dropping a Column 4Use ALTER to add a column u Do not include the NOT NULL clause for new column ALTER TABLE PRODUCT ADD (P_SALECODE CHAR(1)); 4Use ALTER to drop a column u Some RDBMSs impose restrictions on the deletion of an attribute ALTER TABLE Students DROP COLUMN Year;

28 7 7 4Primary and Foreign Key Designation ALTER TABLE PRODUCT ADD PRIMARY KEY (P_CODE); ALTER TABLE PRODUCT ADD FOREIGN KEY (V_CODE) REFERENCES VENDOR; ALTER TABLE PRODUCT ADD PRIMARY KEY (P_CODE) ADD FOREIGN KEY (V_CODE) REFERENCES VENDOR; Advanced Data Management Commands

29 7 7 4Deleting a Table from the Database u DROP TABLE ; DROP TABLE PART; Advanced Data Management Commands

30 7 7 Views 4A view is a virtual table - looks like it exists but doesn’t 4View has a name (like a table) and you can do many of the same things to views as tables 4Content of a view is derived from the contents of some real table(s). (“base tables”) u View is defined via a SELECT statement … see upcoming 4A user may see the view and think it is the actual DB

31 7 7 4Virtual Tables: Creating a View More Complex SQL Functions

32 7 7 Example Views 4CREATE VIEW CS_STUD AS SELECT* FROM STUDENT WHERE MAJOR = ‘CS’; 4CREATE VIEW PRIV_STUD AS SELECTSSN,FNAME,LNAME,YEAR,MAJOR FROM STUDENT 4CREATE VIEW ENROLL_STATS (DEPT,LARGE,SMALL,AVE,TOTAL) AS SELECT DEPT, MAX(ENROLL),MIN(ENROLL),AVG(ENROLL), SUM(ENROLL) FROM SECTIONS GROUP BY DEPT

33 7 7 Example Views 4CREATE VIEW STUD_ENROLL_INFO AS SELECTENROLLMENTS.INDEX,STUDENT.SSN, FNAME,LNAME, YEAR, MAJOR, GPA FROM ENROLLMENTS,STUDENT WHERE ENROLLMENTS.STUDENT = STUDENT.SSN;

34 7 7 Updates involving Views 4NOT ALL views are updatable (e.g. ENROLL_STATS, STUD_ENROLL_INFO ) 4If a given update is ambiguous as to what the user would want, it is hard for the DBMS to decide for them 4Updatable: u a view based on a single table is updatable if the view includes the PK 4Not Updateable: u views involving joins u view involving grouping and aggregate functions

35 7 7 Advantages of Views 4(Some) Logical Independence 4Show user only the info they need to see 4Views can simplify the user’s interaction with DB

36 7 7 End SQL DDL


Download ppt "7 7 SQL Data Definition 4Spread throughout chapter 7."

Similar presentations


Ads by Google