77
Data TypesData Types
Each attribute will be defined to have a particular data type
Data type selection is usually dictated by the nature of the data and by the intended use
Pay close attention to the expected use of attributes for sorting and data retrieval purposes
77
Some Common SQL Data Types
Numeric NUMBER(L,D)INTEGERSMALLINTDECIMAL(L,D)
Character CHAR(L)VARCHAR(L)
Date DATE
Data Type Format
77
Data Definition CommandsData Definition Commands Creating Table Structures
CREATE TABLE <table name>(<attribute1 name and attribute1 characteristics,attribute2 name and attribute2 characteristics,attribute3 name and attribute3 characteristics,primary key designation,foreign key designation and foreign key requirements>);
77
DDL - Create TableDDL - Create TableCREATE 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) )
77
DDLDDL Foreign KeysCREATE 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 )
DomainsCREATE DOMAIN ZIP_CODE_TYP AS CHAR(9);
77
Other SQL ConstraintsOther SQL Constraints
NOT NULL constraint
Ensures that a column does not accept nulls
UNIQUE constraint
Ensures that all values in a column are unique
DEFAULT constraint
Assigns a value to an attribute when a new row is added to a table
CHECK constraint
Validates data when an attribute value is entered
77
Data Definition CommandsData Definition Commands
SQL Integrity Constraints Entity Integrity
PRIMARY KEY NOT NULL and UNIQUE
Referential Integrity FOREIGN KEY ON DELETE ON UPDATE
77
DomainsDomains
Domain is a set of permissible values for an attribute. Defining a domain requires:
Name Data type Default value Domain constraint or condition
CREATE DOMAIN depttyp AS CHAR(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
Syntax:
CREATE DOMAIN <domain name> AS <data type>
DEFAULT <value> CHECK <condition>
77
SQL IndexesSQL Indexes
When a primary key is declared, DBMS automatically creates a unique index
Often need additional indexes
Using the CREATE INDEX command, SQL indexes can be created on the basis of any selected attribute
77CREATE INDEX P_CODEXON PRODUCT(P_CODE);
CREATE UNIQUE INDEX P_CODEXON PRODUCT(P_CODE);
Composite index Index based on two or more attributesCREATE INDEX meetingX ON SECTION ( Sem, Year, Time, Room);
Often used to prevent data duplication
SQL IndexesSQL Indexes
77
Advanced Data Definition CommandsAdvanced Data Definition Commands
All changes in the table structure are made by using the ALTER command
Followed by a keyword that produces specific change
Three options are available
ADD
MODIFY
DROP
77
Advanced Data Management Commands
Advanced Data Management Commands
Changing Table Structures
ALTER TABLE <table name>MODIFY (<column name> <new column characteristics>);
ALTER TABLE <table name>ADD (<column name> <new column characteristics>);
77
Changing a Column’s Data TypeChanging a Column’s Data Type
ALTER can be used to change data type
Some RDBMSs (such as Oracle) do not permit changes to data types unless the column to be changed is empty
ALTER TABLE PRODUCTMODIFY (V_CODE CHAR(5));
77
Changing a Column’s Data Characteristics
Changing a Column’s Data Characteristics
Use ALTER to change data characteristics
If 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
ALTER TABLE PRODUCTMODIFY (P_PRICE DECIMAL(9,2));
77
Adding or Dropping a ColumnAdding or Dropping a Column
Use ALTER to add a column
Do not include the NOT NULL clause for new column
ALTER TABLE PRODUCTADD (P_SALECODE CHAR(1));
Use ALTER to drop a column
Some RDBMSs impose restrictions on the deletion of an attribute
ALTER TABLE StudentsDROP COLUMN Year;
77 Primary and Foreign Key Designation
ALTER TABLE PRODUCTADD PRIMARY KEY (P_CODE);
ALTER TABLE PRODUCTADD 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
Advanced Data Management Commands
77 Deleting a Table from the Database
DROP TABLE <table name>;
DROP TABLE PART;
Advanced Data Management Commands
Advanced Data Management Commands
77
ViewsViews A view is a virtual table - looks like it exists but doesn’t View has a name (like a table) and you can do many of
the same things to views as tables Content of a view is derived from the contents of some
real table(s). (“base tables”) View is defined via a SELECT statement … see upcoming
A user may see the view and think it is the actual DB
77
Example ViewsExample Views
CREATE VIEW CS_STUD
AS SELECT *
FROM STUDENT
WHERE MAJOR = ‘CS’; CREATE VIEW PRIV_STUD
AS SELECT SSN,FNAME,LNAME,YEAR,MAJOR
FROM STUDENT CREATE VIEW ENROLL_STATS (DEPT,LARGE,SMALL,AVE,TOTAL)
AS SELECT DEPT, MAX(ENROLL),MIN(ENROLL),AVG(ENROLL), SUM(ENROLL)
FROM SECTIONS
GROUP BY DEPT
77
Example ViewsExample Views
CREATE VIEW STUD_ENROLL_INFO
AS SELECT ENROLLMENTS.INDEX,STUDENT.SSN, FNAME,LNAME, YEAR, MAJOR, GPA
FROM ENROLLMENTS,STUDENT
WHERE ENROLLMENTS.STUDENT = STUDENT.SSN;
77
Updates involving ViewsUpdates involving Views NOT ALL views are updatable (e.g. ENROLL_STATS,
STUD_ENROLL_INFO) If a given update is ambiguous as to what the user would
want, it is hard for the DBMS to decide for them Updatable:
a view based on a single table is updatable if the view includes the PK
Not Updateable: views involving joins view involving grouping and aggregate functions
77
Advantages of ViewsAdvantages of Views
(Some) Logical Independence Show user only the info they need to see Views can simplify the user’s interaction with DB
Top Related