Altering Tables and Constraints. 12-2 Database Systems Objectives Add and modify columns. Add,...
-
Upload
francis-walsh -
Category
Documents
-
view
231 -
download
1
description
Transcript of Altering Tables and Constraints. 12-2 Database Systems Objectives Add and modify columns. Add,...
12-12-22
Database Systems
ObjectivesObjectives
• Add and modify columns.• Add, enable, disable, or remove constraints.• Drop a table. • Remove all rows leaving the table definition intact.• Change object names.• Add comments to objects and view comments
from the data dictionary.• Oracle data dictionaries.
12-12-33
Database Systems
OverviewOverview
• ALTER TABLE command allows you to– Add and modify columns.– Add or remove constraints.– Enable or disable constraints.
• DROP TABLE command removes the rows and table structure.
• Other commands affecting a table are RENAME, TRUNCATE, COMMENT.
• An automatic commit occurs when you issue these DDL commands.
12-12-44
Database Systems
Adding a Column: SyntaxAdding a Column: Syntax
• Add a new column.• Define a default value for the new column.• Specify that the column must contain a value.
• New columns becomes the last column in the table.
• If a table contains rows when a column is added, the new column is initially null for all the rows.
ALTER TABLE ALTER TABLE tabletableADDADD ((column datatype column datatype [DEFAULT [DEFAULT exprexpr][NOT NULL]][NOT NULL]
[, [, column datatypecolumn datatype]...);]...);
12-12-55
Database Systems
Adding a Column: ExampleAdding a Column: Example
• Add a COMMENTS column to the DEPT table.
• The new column becomes the last column.
SQL> ALTER TABLESQL> ALTER TABLE deptdept 2 ADD2 ADD (comments VARCHAR2(255));(comments VARCHAR2(255));Table altered.Table altered.
12-12-66
Database Systems
Modifying a Column: SyntaxModifying a Column: Syntax• Change a column's datatype, size, default value, and NOT
NULL column constraint.
Guidelines– Increase a number column's width or precision.– Decrease a column's width if the column contains
null values or if the table has no rows.– Change the default value for subsequent additions.– Define a NOT NULL constraint only if there are no
rows in the table.
ALTER TABLEALTER TABLE tabletableMODIFYMODIFY ((column datatype column datatype [DEFAULT [DEFAULT exprexpr][NOT NULL]][NOT NULL]
[, [, column datatypecolumn datatype]...);]...);
12-12-77
Database Systems
Modifying a ColumnModifying a Column
• Change the datatype if the column contains null values.
• Change the default value to affect only subsequent insertions into the table.
Example• Extend the maximum length of the JOB column in
the EMP table to 50 characters.
SQL> ALTER TABLESQL> ALTER TABLE empemp 2 MODIFY 2 MODIFY (job VARCHAR2(50));(job VARCHAR2(50));Table altered.Table altered.
12-12-88
Database Systems
Adding a Constraint: SyntaxAdding a Constraint: Syntax
• Add or drop, but not modify, a constraint.• Enable or disable constraints.• Add a NOT NULL constraint by using the MODIFY
clause.
SQL> ALTER TABLESQL> ALTER TABLE tabletable 2 ADD [CONSTRAINT 2 ADD [CONSTRAINT constraint_nameconstraint_name] ] type type ((columncolumn););
12-12-99
Database Systems
Adding a Constraint: ExampleAdding a Constraint: Example
Add a foreign key constraint to the EMP table indicating that a manager must already exist as a valid employee in the EMP table.
SQL> SQL> ALTER TABLEALTER TABLE empemp 2 ADD CONSTRAINT2 ADD CONSTRAINT emp_mgr_id_fkemp_mgr_id_fk 3 FOREIGN KEY (mgr)3 FOREIGN KEY (mgr) 4 REFERENCES 4 REFERENCES emp(empno);emp(empno);Table altered.Table altered.
12-12-1010
Database Systems
Dropping a Constraint: ExamplesDropping a Constraint: Examples
• Remove the manager constraint from the EMP table.
• Remove the PRIMARY KEY constraint on the DEPT table and drop the associated FOREIGN KEY constraint on the EMP.DEPTNO column.
SQL> ALTER TABLESQL> ALTER TABLE empemp 2 DROP CONSTRAINT2 DROP CONSTRAINT emp_mgr_id_fk;emp_mgr_id_fk;Table altered.Table altered.
SQL> ALTER TABLESQL> ALTER TABLE deptdept 2 DROP 2 DROP PRIMARY KEY CASCADE;PRIMARY KEY CASCADE;Table altered.Table altered.
12-12-1111
Database Systems
Disabling ConstraintsDisabling Constraints
• Execute the DISABLE clause of the ALTER TABLE command to deactivate an integrity constraint.
• Apply the CASCADE option to disable dependent integrity constraints.
• You can use the DISABLE clause in both the CREATE TABLE statement and the ALTER TABLE statement.
SQL> ALTER TABLESQL> ALTER TABLE empemp 2 DISABLE CONSTRAINT2 DISABLE CONSTRAINT emp_empno_pk CASCADE;emp_empno_pk CASCADE;Table altered.Table altered.
12-12-1212
Database Systems
Enabling ConstraintsEnabling Constraints
• Activate an integrity constraint currently disabled in the table definition by using the ENABLE clause.
• A UNIQUE or PRIMARY KEY index is automatically created if you enable a UNIQUE or PRIMARY KEY constraint.
SQL> ALTER TABLESQL> ALTER TABLE empemp 2 ENABLE CONSTRAINT2 ENABLE CONSTRAINT emp_empno_pk;emp_empno_pk;Table altered.Table altered.
12-12-1313
Database Systems
Dropping a Table: SyntaxDropping a Table: Syntax
• All data and structure in the table is deleted.• Any pending transactions are committed.• All indexes are dropped.• The CASCADE CONSTRAINTS option removes
dependent integrity constraints.• You cannot roll back this command.
DROP TABLE DROP TABLE tabletable [CASCADE CONSTRAINTS] [CASCADE CONSTRAINTS] [purge];[purge];
12-12-1414
Database Systems
Changing the Name of an ObjectChanging the Name of an Object
• Execute the RENAME command to change the name of a table, view, sequence, or synonym.
SQL> RENAME ord TO order;SQL> RENAME ord TO order;Table renamed.Table renamed.
• You must be the owner of the object.You must be the owner of the object.
12-12-1515
Database Systems
Truncating a TableTruncating a Table
• The TRUNCATE command– Removes all rows from a table.– Releases the storage space used by that table.– Is a DDL command.
• Cannot roll back row removal when using TRUNCATE.
• Alternatively, remove rows by using the DELETE command. It does not release storage space.
SQL> TRUNCATE TABLE item;SQL> TRUNCATE TABLE item;Table truncated.Table truncated.
12-12-1616
Database Systems
Adding Comments to a TableAdding Comments to a Table• You can add comments to a table or column by
using the COMMENT command.
SQL> COMMENT ON TABLE emp SQL> COMMENT ON TABLE emp 2 IS 'Employee Information';2 IS 'Employee Information';Comment created.Comment created.
• To clear the comment, use the empty string.• Comments can be viewed through the following
data dictionary views:– ALL_COL_COMMENTS– USER_COL_COMMENTS– ALL_TAB_COMMENTS– USER_TAB_COMMENTS
12-12-1717
Database Systems
Tables Within the Oracle DatabaseTables Within the Oracle Database
• User tables– Collection of tables created and maintained by
the user– Contain user information
• Data dictionary– Collection of tables created and maintained by
the Oracle Server– Contain database information
12-12-1818
Database Systems
Data Dictionary DescriptionData Dictionary Description
• Created when a database is created• Updated and maintained by the Oracle Server• Query data dictionary views• Information stored in the data dictionary
– Names of Oracle Server users– Privileges granted to users– Database object names– Table constraints– Auditing information
12-12-1919
Database Systems
Querying the Data DictionaryQuerying the Data Dictionary
• Four classes of views (prefixes)– USER_ Objects owned by user– ALL_ Objects user has access
rights– DBA_ All database objects– V$_ Server performance
• Other views– DICTIONARY– TABLE_PRIVILEGES– IND
12-12-2020
Database Systems
Querying the Data Dictionary: ExamplesQuerying the Data Dictionary: Examples• List all data dictionary views accessible to the
user.SQL> SELECTSQL> SELECT ** 2 FROM2 FROM DICTIONARY;DICTIONARY;
• Display the structure of the USER_OBJECTS view.SQL> DESCRIBE user_objectsSQL> DESCRIBE user_objects
• Display all the names of tables that you own.SQL> SELECTSQL> SELECT object_nameobject_name 2 FROM2 FROM user_objectsuser_objects 3 WHERE3 WHERE object_type = 'TABLE';object_type = 'TABLE';
12-12-2121
Database Systems
Viewing ConstraintsViewing Constraints
• Query the USER_CONSTRAINTS table to view all constraint definitions and names.
SQL> select SQL> select constraint_name, constraint_name, constraint_type, constraint_type, search_condition search_condition
fromfrom user_constraintsuser_constraints where table_name = ‘EMP’;where table_name = ‘EMP’;
CONSTRAINT_NAMECONSTRAINT_NAME CC SEARCH_CONDITIONSEARCH_CONDITION----------------- - ------------------------------------ - -------------------SYS_C00674SYS_C00674 CC EMPNO IS NOT NULLEMPNO IS NOT NULLSYS_C00675SYS_C00675 CC DEPTNO IS NOT NULLDEPTNO IS NOT NULLEMP_EMPNO_PKEMP_EMPNO_PK PP............
12-12-2222
Database Systems
Viewing the Columns Associated with Viewing the Columns Associated with ConstraintsConstraints
• Query the USER_CONS_COLUMNS view for all constraint names and column names.
SQL> select constraint_name, column_name SQL> select constraint_name, column_name fromfrom user_cons_columnsuser_cons_columns
where table_name = ‘EMP’;where table_name = ‘EMP’;
CONSTRAINT_NAMECONSTRAINT_NAME COLUMN_NAMECOLUMN_NAME----------------- -------------------------------------- ---------------------EMP_DEPTNO_FKEMP_DEPTNO_FK DEPTNODEPTNOEMP_EMPNO_PKEMP_EMPNO_PK EMPNOEMPNOEMP_MGR_FKEMP_MGR_FK MGRMGRSYS_C00674SYS_C00674 EMPNOEMPNOSYS_C00675SYS_C00675 DEPTNODEPTNO
12-12-2323
Database Systems
SummarySummary
Command
CREATE TABLE
ALTER TABLE
DROP TABLE
RENAME
TRUNCATE
COMMENT
DescriptionDescription
Creates a table and indicated constraints.Creates a table and indicated constraints.
Modifies table structures and constraints.Modifies table structures and constraints.
Removes the rows and table structure.Removes the rows and table structure.
Changes the name of a table, view, sequence, Changes the name of a table, view, sequence, or synonym.or synonym.
Removes all rows from a table and releases Removes all rows from a table and releases the storage space.the storage space.
Adds comments to a table or view.Adds comments to a table or view.
12-12-2424
Database Systems
Practice OverviewPractice Overview
• Adding constraints to existing tables• Adding more column to a table• Displaying information in data dictionary views
12-12-2525
Database Systems
Practice 1Practice 1
• View all constraints associated with tables EMPLOYEE and DEPARTMENT you created in last class from USER_CONSTRAINTS data dictionary.
• Drop the PRIMARY KEY constraint in EMPLOYEE table.
• Add a table level PRIMARY KEY constraint in EMPLOYEE table for ID column.
12-12-2626
Database Systems
Practice 2Practice 2
• View all constraints associated with tables EMPLOYEE from USER_CONSTRAINTS data dictionary.
• Drop the FOREIGN KEY constraint in EMPLOYEE table.
• Add a FOREIGN KEY constraint in EMPLOYEE table to ensure that no employee is assigned to a non-existing department.
12-12-2727
Database Systems
Practice 3Practice 3
• Display the object names and types from USER_OBJECTS data dictionary view for tables EMPLOYEE and DEPARTMENT. You may format the columns for readability. Notice the indexes created for the tables.