Lab-lecture-week-05(2)
Lab-lecture-week-05(2)
• You specify:
– Table name
– Column name, column data type, and column size
Creating Tables
– Create the table.
CREATE TABLE dept
(deptno NUMBER(2),
dname VARCHAR2(14),
loc VARCHAR2(13),
create_date DATE DEFAULT SYSDATE);
Table created.
DESCRIBE dept
Data Types
Data Type Description
CHAR(size) Fixed-length character data
VARCHAR2 Variable-length character data
CLOB Character data up to 8 Terabytes
NUMBER(p,s) Variable-length numeric data
DATE Date and Time values
BLOB Binary data up to 8 Terabytes
RAW & Raw binary data
LONG RAW
BFILE Binary data stored in operating-system files
outside the database; up to 8 Terabytes
The DEFAULT Option
• Specify a default value for a column, to be used
during an insert.
• Legal values are literal value, expression, or
SQL function.
• The default datatype must match the column
datatype.
Table created.
SELECT *
FROM tsg0781.Customer;
The ALTER TABLE Statement
• Use the ALTER TABLE statement to:
– Add a new column
– Modify an existing column
– Define a default value for the new column
DEPT30
EMPNO ENAME ANNSAL HIREDATE JOB
------ ---------- --------
7698 BLAKE 34200 01-MAY-81
7654 MARTIN 15000 28-SEP-81
7499 ALLEN 19200 20-FEB-81
7844 TURNER 18000 08-SEP-81
... 15
Adding a Column
• You use the ADD clause to add columns.
Table dropped.
Changing the Name of an Object
• To change the name of a table, view, sequence, or
synonym, you execute the RENAME statement.
Table renamed.
Table truncated.
column,...
[CONSTRAINT constraint_name] constraint_type
(column, ...),
The NOT NULL Constraint
• Ensures that null values are not permitted for the column
EMP
EMPNO ENAME JOB ... COMM DEPTNO
CONSTRAINT_NAME C SEARCH_CONDITION
------------------------ - -------------------------
SYS_C00674 C EMPNO IS NOT NULL
SYS_C00675 C DEPTNO IS NOT NULL
EMP_EMPNO_PK P
...
Viewing the Columns Associated
with Constraints
• View the columns associated with the constraint
names in the USER_CONS_COLUMNS view.
CONSTRAINT_NAME COLUMN_NAME
------------------------- ----------------------
EMP_DEPTNO_FK DEPTNO
EMP_EMPNO_PK EMPNO
EMP_MGR_FK MGR
SYS_C00674 EMPNO
SYS_C00675 DEPTNO
Summary
• Create table, Alter table, Drop table
• Create the following types of constraints:
– NOT NULL
– UNIQUE
– PRIMARY KEY
– FOREIGN KEY
– CHECK
• Query the USER_CONSTRAINTS table to view all
constraint definitions and names.
Lab Activities
• Complete exercise handed out in lab