Download presentation
Presentation is loading. Please wait.
Published byFrancis Walsh Modified over 8 years ago
1
Altering Tables and Constraints
2
12-2 Database Systems Objectives 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.
3
12-3 Database Systems Overview 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.
4
12-4 Database Systems Adding 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 table ADD(column datatype [DEFAULT expr][NOT NULL] [, column datatype]...); ALTER TABLE table ADD(column datatype [DEFAULT expr][NOT NULL] [, column datatype]...);
5
12-5 Database Systems Adding a Column: Example Add a COMMENTS column to the DEPT table. The new column becomes the last column. SQL> ALTER TABLEdept 2 ADD(comments VARCHAR2(255)); 2 ADD(comments VARCHAR2(255)); Table altered. SQL> ALTER TABLEdept 2 ADD(comments VARCHAR2(255)); 2 ADD(comments VARCHAR2(255)); Table altered.
6
12-6 Database Systems Modifying 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 TABLEtable MODIFY(column datatype [DEFAULT expr][NOT NULL] [, column datatype]...); ALTER TABLEtable MODIFY(column datatype [DEFAULT expr][NOT NULL] [, column datatype]...);
7
12-7 Database Systems Modifying 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 TABLEemp 2 MODIFY (job VARCHAR2(50)); 2 MODIFY (job VARCHAR2(50)); Table altered. SQL> ALTER TABLEemp 2 MODIFY (job VARCHAR2(50)); 2 MODIFY (job VARCHAR2(50)); Table altered.
8
12-8 Database Systems Adding 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 TABLEtable 2 ADD [CONSTRAINT constraint_name] type (column); 2 ADD [CONSTRAINT constraint_name] type (column); SQL> ALTER TABLEtable 2 ADD [CONSTRAINT constraint_name] type (column); 2 ADD [CONSTRAINT constraint_name] type (column);
9
12-9 Database Systems Adding 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> ALTER TABLEemp 2 ADD CONSTRAINTemp_mgr_id_fk 2 ADD CONSTRAINTemp_mgr_id_fk 3 FOREIGN KEY (mgr) 3 FOREIGN KEY (mgr) 4 REFERENCES emp(empno); 4 REFERENCES emp(empno); Table altered. SQL> ALTER TABLEemp 2 ADD CONSTRAINTemp_mgr_id_fk 2 ADD CONSTRAINTemp_mgr_id_fk 3 FOREIGN KEY (mgr) 3 FOREIGN KEY (mgr) 4 REFERENCES emp(empno); 4 REFERENCES emp(empno); Table altered.
10
12-10 Database Systems Dropping 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 TABLEemp 2 DROP CONSTRAINTemp_mgr_id_fk; 2 DROP CONSTRAINTemp_mgr_id_fk; Table altered. SQL> ALTER TABLEemp 2 DROP CONSTRAINTemp_mgr_id_fk; 2 DROP CONSTRAINTemp_mgr_id_fk; Table altered. SQL> ALTER TABLEdept 2 DROP PRIMARY KEY CASCADE; 2 DROP PRIMARY KEY CASCADE; Table altered. SQL> ALTER TABLEdept 2 DROP PRIMARY KEY CASCADE; 2 DROP PRIMARY KEY CASCADE; Table altered.
11
12-11 Database Systems Disabling 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 TABLEemp 2 DISABLE CONSTRAINTemp_empno_pk CASCADE; 2 DISABLE CONSTRAINTemp_empno_pk CASCADE; Table altered. SQL> ALTER TABLEemp 2 DISABLE CONSTRAINTemp_empno_pk CASCADE; 2 DISABLE CONSTRAINTemp_empno_pk CASCADE; Table altered.
12
12-12 Database Systems Enabling 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 TABLEemp 2 ENABLE CONSTRAINTemp_empno_pk; 2 ENABLE CONSTRAINTemp_empno_pk; Table altered. SQL> ALTER TABLEemp 2 ENABLE CONSTRAINTemp_empno_pk; 2 ENABLE CONSTRAINTemp_empno_pk; Table altered.
13
12-13 Database Systems Dropping 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 table [CASCADE CONSTRAINTS] [purge];
14
12-14 Database Systems Changing 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; Table renamed. SQL> RENAME ord TO order; Table renamed. You must be the owner of the object. You must be the owner of the object.
15
12-15 Database Systems Truncating 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; Table truncated. SQL> TRUNCATE TABLE item; Table truncated.
16
12-16 Database Systems Adding Comments to a Table You can add comments to a table or column by using the COMMENT command. SQL> COMMENT ON TABLE emp 2 IS 'Employee Information'; 2 IS 'Employee Information'; Comment created. SQL> COMMENT ON TABLE emp 2 IS 'Employee Information'; 2 IS 'Employee Information'; 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
17
12-17 Database Systems Tables 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
18
12-18 Database Systems Data 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
19
12-19 Database Systems Querying 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
20
12-20 Database Systems Querying the Data Dictionary: Examples List all data dictionary views accessible to the user. SQL> SELECT* 2 FROMDICTIONARY; 2 FROMDICTIONARY; SQL> SELECT* 2 FROMDICTIONARY; 2 FROMDICTIONARY; Display the structure of the USER_OBJECTS view. SQL> DESCRIBE user_objects Display all the names of tables that you own. SQL> SELECTobject_name 2 FROMuser_objects 2 FROMuser_objects 3 WHEREobject_type = 'TABLE'; 3 WHEREobject_type = 'TABLE'; SQL> SELECTobject_name 2 FROMuser_objects 2 FROMuser_objects 3 WHEREobject_type = 'TABLE'; 3 WHEREobject_type = 'TABLE';
21
12-21 Database Systems Viewing Constraints Query the USER_CONSTRAINTS table to view all constraint definitions and names. SQL> select constraint_name, constraint_type, search_condition fromuser_constraints where table_name = ‘EMP’; where table_name = ‘EMP’; SQL> select constraint_name, constraint_type, search_condition fromuser_constraints where table_name = ‘EMP’; where table_name = ‘EMP’; CONSTRAINT_NAMECSEARCH_CONDITION ----------------- - ------------------- SYS_C00674CEMPNO IS NOT NULL SYS_C00675CDEPTNO IS NOT NULL EMP_EMPNO_PKP...... CONSTRAINT_NAMECSEARCH_CONDITION ----------------- - ------------------- SYS_C00674CEMPNO IS NOT NULL SYS_C00675CDEPTNO IS NOT NULL EMP_EMPNO_PKP......
22
12-22 Database Systems Viewing the Columns Associated with Constraints Query the USER_CONS_COLUMNS view for all constraint names and column names. SQL> select constraint_name, column_name fromuser_cons_columns where table_name = ‘EMP’; where table_name = ‘EMP’; SQL> select constraint_name, column_name fromuser_cons_columns where table_name = ‘EMP’; where table_name = ‘EMP’; CONSTRAINT_NAMECOLUMN_NAME ----------------- --------------------- EMP_DEPTNO_FKDEPTNO EMP_EMPNO_PKEMPNO EMP_MGR_FKMGR SYS_C00674EMPNO SYS_C00675DEPTNO CONSTRAINT_NAMECOLUMN_NAME ----------------- --------------------- EMP_DEPTNO_FKDEPTNO EMP_EMPNO_PKEMPNO EMP_MGR_FKMGR SYS_C00674EMPNO SYS_C00675DEPTNO
23
12-23 Database Systems Summary Command CREATE TABLE ALTER TABLE DROP TABLE RENAME TRUNCATE COMMENTDescription Creates a table and indicated constraints. Modifies table structures and constraints. Removes the rows and table structure. Changes the name of a table, view, sequence, or synonym. Removes all rows from a table and releases the storage space. Adds comments to a table or view.
24
12-24 Database Systems Practice Overview Adding constraints to existing tables Adding more column to a table Displaying information in data dictionary views
25
12-25 Database Systems Practice 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.
26
12-26 Database Systems Practice 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.
27
12-27 Database Systems Practice 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.
28
12-28 Database Systems Practice 4 Modify the EMPLOYEE table. Add a SALARY column of NUMBER data type with precision 7.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.