Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Using DDL Statements to Create and Manage Tables
Objectives <ul><li>After completing this lesson, you should be able to do the following: </li></ul><ul><ul><li>Categorize ...
Database Objects Logically represents subsets of data from one or more tables  View  Generates numeric values Sequence  Ba...
Naming Rules <ul><li>Table names and column names: </li></ul><ul><ul><li>Must begin with a letter </li></ul></ul><ul><ul><...
<ul><ul><li>You must have: </li></ul></ul><ul><ul><ul><li>CREATE TABLE  privilege </li></ul></ul></ul><ul><ul><ul><li>A st...
Referencing Another User’s Tables <ul><ul><li>Tables belonging to other users are not in the user’s schema. </li></ul></ul...
<ul><ul><li>Specify a default value for a column during an insert. </li></ul></ul><ul><ul><li>Literal values, expressions,...
Creating Tables <ul><ul><li>Create the table. </li></ul></ul><ul><ul><li>Confirm table creation. </li></ul></ul>DESCRIBE d...
Data Types Raw binary data RAW and LONG RAW   Binary data (up to 4 GB) BLOB Binary data stored in an external file (up to ...
 
Datetime Data Types <ul><li>You can use several datetime data types: </li></ul>Stored as an interval of years and months I...
Datetime Data Types <ul><ul><li>The  TIMESTAMP  data type is an extension of the  DATE  data type. </li></ul></ul><ul><ul>...
 
Datetime Data Types <ul><ul><li>The  INTERVAL   YEAR   TO   MONTH  data type stores a period of time using the  YEAR  and ...
 
INTERVAL DAY TO SECOND  Data Type <ul><li>INTERVAL DAY TO SECOND  stores a period of time in terms of days, hours, minutes...
Including Constraints <ul><ul><li>Constraints enforce rules at the table level. </li></ul></ul><ul><ul><li>Constraints pre...
Constraint Guidelines <ul><ul><li>You can name a constraint, or the Oracle server generates a name by using the  SYS_C n  ...
Defining Constraints <ul><ul><li>Syntax: </li></ul></ul><ul><ul><li>Column-level constraint: </li></ul></ul><ul><ul><li>Ta...
Defining Constraints <ul><ul><li>Column-level constraint:  </li></ul></ul><ul><ul><li>Table-level constraint: </li></ul></...
NOT NULL  Constraint <ul><li>Ensures that null values are not permitted for the column: </li></ul>NOT NULL  constraint (No...
UNIQUE  Constraint EMPLOYEES  UNIQUE  constraint INSERT INTO Not allowed: already exists Allowed …
UNIQUE  Constraint <ul><li>Defined at either the table level or the column level:  </li></ul>CREATE TABLE employees( emplo...
PRIMARY KEY  Constraint DEPARTMENTS   PRIMARY KEY INSERT INTO Not allowed (null value) Not allowed  (50 already exists) …
FOREIGN KEY  Constraint DEPARTMENTS   EMPLOYEES FOREIGN KEY INSERT INTO Not allowed (9 does not exist) Allowed PRIMARY KEY...
FOREIGN KEY  Constraint <ul><li>Defined at either the table level or the column level: </li></ul>CREATE TABLE employees( e...
FOREIGN KEY  Constraint: Keywords <ul><ul><li>FOREIGN KEY : Defines the column in the child table at the table-constraint ...
CHECK  Constraint <ul><ul><li>Defines a condition that each row must satisfy </li></ul></ul><ul><ul><li>The following expr...
CREATE TABLE : Example CREATE TABLE employees ( employee_id  NUMBER(6) CONSTRAINT  emp_employee_id  PRIMARY KEY , first_na...
Violating Constraints  <ul><li>Department 55 does not exist. </li></ul>UPDATE employees * ERROR at line 1: ORA-02291: inte...
Violating Constraints <ul><li>You cannot delete a row that contains a primary key that is used as a foreign key in another...
Creating a Table by Using a Subquery <ul><ul><li>Create a table and insert rows by combining the  CREATE TABLE  statement ...
Creating a Table by Using a Subquery CREATE TABLE  dept80 AS    SELECT  employee_id, last_name,  salary*12 ANNSAL,  hire_d...
ALTER TABLE  Statement <ul><li>Use the  ALTER   TABLE  statement to: </li></ul><ul><ul><li>Add a new column </li></ul></ul...
Dropping a Table <ul><ul><li>All data and structure in the table are deleted. </li></ul></ul><ul><ul><li>Any pending trans...
Summary <ul><li>In this lesson, you should have learned how to use the  CREATE TABLE  statement to create a table and incl...
Practice 9: Overview <ul><li>This practice covers the following topics: </li></ul><ul><ul><li>Creating new tables </li></u...
 
 
 
Upcoming SlideShare
Loading in …5
×

Les09

1,256 views

Published on

Oracle_10g_SQL_Fundamentals_I

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Les09

  1. 1. Using DDL Statements to Create and Manage Tables
  2. 2. Objectives <ul><li>After completing this lesson, you should be able to do the following: </li></ul><ul><ul><li>Categorize the main database objects </li></ul></ul><ul><ul><li>Review the table structure </li></ul></ul><ul><ul><li>List the data types that are available for columns </li></ul></ul><ul><ul><li>Create a simple table </li></ul></ul><ul><ul><li>Understand how constraints are created at the time of table creation </li></ul></ul><ul><ul><li>Describe how schema objects work </li></ul></ul>
  3. 3. Database Objects Logically represents subsets of data from one or more tables View Generates numeric values Sequence Basic unit of storage; composed of rows Table Gives alternative names to objects Synonym Improves the performance of some queries Index Description Object
  4. 4. Naming Rules <ul><li>Table names and column names: </li></ul><ul><ul><li>Must begin with a letter </li></ul></ul><ul><ul><li>Must be 1–30 characters long </li></ul></ul><ul><ul><li>Must contain only A–Z, a–z, 0–9, _, $, and # </li></ul></ul><ul><ul><li>Must not duplicate the name of another object owned by the same user </li></ul></ul><ul><ul><li>Must not be an Oracle server reserved word </li></ul></ul>
  5. 5. <ul><ul><li>You must have: </li></ul></ul><ul><ul><ul><li>CREATE TABLE privilege </li></ul></ul></ul><ul><ul><ul><li>A storage area </li></ul></ul></ul><ul><ul><li>You specify: </li></ul></ul><ul><ul><ul><li>Table name </li></ul></ul></ul><ul><ul><ul><li>Column name, column data type, and column size </li></ul></ul></ul>CREATE TABLE Statement CREATE TABLE [ schema .] table ( column datatype [DEFAULT expr ][, ...]);
  6. 6. Referencing Another User’s Tables <ul><ul><li>Tables belonging to other users are not in the user’s schema. </li></ul></ul><ul><ul><li>You should use the owner’s name as a prefix to those tables. </li></ul></ul>USERB USERA SELECT * FROM userB.employees; SELECT * FROM userA.employees;
  7. 7. <ul><ul><li>Specify a default value for a column during an insert. </li></ul></ul><ul><ul><li>Literal values, expressions, or SQL functions are legal values. </li></ul></ul><ul><ul><li>Another column’s name or a pseudocolumn are illegal values. </li></ul></ul><ul><ul><li>The default data type must match the column data type. </li></ul></ul>DEFAULT Option ... hire_date DATE DEFAULT SYSDATE, ... CREATE TABLE hire_dates (id NUMBER(8), hire_date DATE DEFAULT SYSDATE ); Table created.
  8. 8. Creating Tables <ul><ul><li>Create the table. </li></ul></ul><ul><ul><li>Confirm table creation. </li></ul></ul>DESCRIBE dept CREATE TABLE dept (deptno NUMBER(2), dname VARCHAR2(14), loc VARCHAR2(13), create_date DATE DEFAULT SYSDATE ); Table created.
  9. 9. Data Types Raw binary data RAW and LONG RAW Binary data (up to 4 GB) BLOB Binary data stored in an external file (up to 4 GB) BFILE Date and time values DATE Variable-length character data (up to 2 GB) LONG Character data (up to 4 GB) CLOB A base-64 number system representing the unique address of a row in its table ROWID Fixed-length character data CHAR( size ) Variable-length numeric data NUMBER( p , s) Variable-length character data VARCHAR2( size ) Description Data Type
  10. 11. Datetime Data Types <ul><li>You can use several datetime data types: </li></ul>Stored as an interval of years and months INTERVAL YEAR TO MONTH Stored as an interval of days, hours, minutes, and seconds INTERVAL DAY TO SECOND Date with fractional seconds TIMESTAMP Description Data Type
  11. 12. Datetime Data Types <ul><ul><li>The TIMESTAMP data type is an extension of the DATE data type. </li></ul></ul><ul><ul><li>It stores the year, month, and day of the DATE data type plus hour, minute, and second values as well as the fractional second value. </li></ul></ul><ul><ul><li>You can optionally specify the time zone. </li></ul></ul>TIMESTAMP[(fractional_seconds_precision)] TIMESTAMP[(fractional_seconds_precision)] WITH TIME ZONE TIMESTAMP[(fractional_seconds_precision)] WITH LOCAL TIME ZONE
  12. 14. Datetime Data Types <ul><ul><li>The INTERVAL YEAR TO MONTH data type stores a period of time using the YEAR and MONTH datetime fields: </li></ul></ul><ul><ul><li>The INTERVAL DAY TO SECOND data type stores a period of time in terms of days, hours, minutes, and seconds: </li></ul></ul>INTERVAL YEAR [(year_precision)] TO MONTH INTERVAL DAY [(day_precision)] TO SECOND [(fractional_seconds_precision)]
  13. 16. INTERVAL DAY TO SECOND Data Type <ul><li>INTERVAL DAY TO SECOND stores a period of time in terms of days, hours, minutes, and seconds. </li></ul>INTERVAL '4 5:12:10.222' DAY TO SECOND(3) Indicates 4 days, 5 hours, 12 minutes, 10 seconds, and 222 thousandths of a second. INTERVAL '4 5:12' DAY TO MINUTE Indicates 4 days, 5 hours and 12 minutes. INTERVAL '400 5' DAY(3) TO HOUR Indicates 400 days 5 hours. INTERVAL '11:12:10.2222222' HOUR TO SECOND(7) indicates 11 hours, 12 minutes, and 10.2222222 seconds.
  14. 17. Including Constraints <ul><ul><li>Constraints enforce rules at the table level. </li></ul></ul><ul><ul><li>Constraints prevent the deletion of a table if there are dependencies. </li></ul></ul><ul><ul><li>The following constraint types are valid: </li></ul></ul><ul><ul><ul><li>NOT NULL </li></ul></ul></ul><ul><ul><ul><li>UNIQUE </li></ul></ul></ul><ul><ul><ul><li>PRIMARY KEY </li></ul></ul></ul><ul><ul><ul><li>FOREIGN KEY </li></ul></ul></ul><ul><ul><ul><li>CHECK </li></ul></ul></ul>
  15. 18. Constraint Guidelines <ul><ul><li>You can name a constraint, or the Oracle server generates a name by using the SYS_C n format. </li></ul></ul><ul><ul><li>Create a constraint at either of the following times: </li></ul></ul><ul><ul><ul><li>At the same time as the table is created </li></ul></ul></ul><ul><ul><ul><li>After the table has been created </li></ul></ul></ul><ul><ul><li>Define a constraint at the column or table level. </li></ul></ul><ul><ul><li>View a constraint in the data dictionary. </li></ul></ul>
  16. 19. Defining Constraints <ul><ul><li>Syntax: </li></ul></ul><ul><ul><li>Column-level constraint: </li></ul></ul><ul><ul><li>Table-level constraint: </li></ul></ul>CREATE TABLE [ schema .] table ( column datatype [DEFAULT expr ] [ column_constraint ], ... [ table_constraint ][,...] ); column,... [CONSTRAINT constraint_name ] constraint_type ( column , ...), column [CONSTRAINT constraint_name ] constraint_type ,
  17. 20. Defining Constraints <ul><ul><li>Column-level constraint: </li></ul></ul><ul><ul><li>Table-level constraint: </li></ul></ul>CREATE TABLE employees( employee_id NUMBER(6) CONSTRAINT emp_emp_id_pk PRIMARY KEY, first_name VARCHAR2(20), ...); CREATE TABLE employees( employee_id NUMBER(6), first_name VARCHAR2(20), ... job_id VARCHAR2(10) NOT NULL, CONSTRAINT emp_emp_id_pk PRIMARY KEY (EMPLOYEE_ID) ); 1 2
  18. 21. NOT NULL Constraint <ul><li>Ensures that null values are not permitted for the column: </li></ul>NOT NULL constraint (No row can contain a null value for this column.) Absence of NOT NULL constraint (Any row can contain a null value for this column.) NOT NULL constraint …
  19. 22. UNIQUE Constraint EMPLOYEES UNIQUE constraint INSERT INTO Not allowed: already exists Allowed …
  20. 23. UNIQUE Constraint <ul><li>Defined at either the table level or the column level: </li></ul>CREATE TABLE employees( employee_id NUMBER(6), last_name VARCHAR2(25) NOT NULL, email VARCHAR2(25), salary NUMBER(8,2), commission_pct NUMBER(2,2), hire_date DATE NOT NULL, ... CONSTRAINT emp_email_uk UNIQUE(email));
  21. 24. PRIMARY KEY Constraint DEPARTMENTS PRIMARY KEY INSERT INTO Not allowed (null value) Not allowed (50 already exists) …
  22. 25. FOREIGN KEY Constraint DEPARTMENTS EMPLOYEES FOREIGN KEY INSERT INTO Not allowed (9 does not exist) Allowed PRIMARY KEY … …
  23. 26. FOREIGN KEY Constraint <ul><li>Defined at either the table level or the column level: </li></ul>CREATE TABLE employees( employee_id NUMBER(6), last_name VARCHAR2(25) NOT NULL, email VARCHAR2(25), salary NUMBER(8,2), commission_pct NUMBER(2,2), hire_date DATE NOT NULL, ... department_id NUMBER(4), CONSTRAINT emp_dept_fk FOREIGN KEY (department_id) REFERENCES departments(department_id), CONSTRAINT emp_email_uk UNIQUE(email));
  24. 27. FOREIGN KEY Constraint: Keywords <ul><ul><li>FOREIGN KEY : Defines the column in the child table at the table-constraint level </li></ul></ul><ul><ul><li>REFERENCES : Identifies the table and column in the parent table </li></ul></ul><ul><ul><li>ON DELETE CASCADE : Deletes the dependent rows in the child table when a row in the parent table is deleted </li></ul></ul><ul><ul><li>ON DELETE SET NULL : Converts dependent foreign key values to null </li></ul></ul>
  25. 28. CHECK Constraint <ul><ul><li>Defines a condition that each row must satisfy </li></ul></ul><ul><ul><li>The following expressions are not allowed: </li></ul></ul><ul><ul><ul><li>References to CURRVAL , NEXTVAL , LEVEL , and ROWNUM pseudocolumns </li></ul></ul></ul><ul><ul><ul><li>Calls to SYSDATE , UID , USER , and USERENV functions </li></ul></ul></ul><ul><ul><ul><li>Queries that refer to other values in other rows </li></ul></ul></ul>..., salary NUMBER(2) CONSTRAINT emp_salary_min CHECK (salary > 0),...
  26. 29. CREATE TABLE : Example CREATE TABLE employees ( employee_id NUMBER(6) CONSTRAINT emp_employee_id PRIMARY KEY , first_name VARCHAR2(20) , last_name VARCHAR2(25) CONSTRAINT emp_last_name_nn NOT NULL , email VARCHAR2(25) CONSTRAINT emp_email_nn NOT NULL CONSTRAINT emp_email_uk UNIQUE , phone_number VARCHAR2(20) , hire_date DATE CONSTRAINT emp_hire_date_nn NOT NULL , job_id VARCHAR2(10) CONSTRAINT emp_job_nn NOT NULL , salary NUMBER(8,2) CONSTRAINT emp_salary_ck CHECK (salary>0) , commission_pct NUMBER(2,2) , manager_id NUMBER(6) , department_id NUMBER(4) CONSTRAINT emp_dept_fk REFERENCES departments (department_id));
  27. 30. Violating Constraints <ul><li>Department 55 does not exist. </li></ul>UPDATE employees * ERROR at line 1: ORA-02291: integrity constraint (HR.EMP_DEPT_FK) violated - parent key not found UPDATE employees SET department_id = 55 WHERE department_id = 110;
  28. 31. Violating Constraints <ul><li>You cannot delete a row that contains a primary key that is used as a foreign key in another table. </li></ul>DELETE FROM departments WHERE department_id = 60; DELETE FROM departments * ERROR at line 1: ORA-02292: integrity constraint (HR.EMP_DEPT_FK) violated - child record found
  29. 32. Creating a Table by Using a Subquery <ul><ul><li>Create a table and insert rows by combining the CREATE TABLE statement and the AS subquery option. </li></ul></ul><ul><ul><li>Match the number of specified columns to the number of subquery columns. </li></ul></ul><ul><ul><li>Define columns with column names and default values. </li></ul></ul>CREATE TABLE table [( column , column ...)] AS subquery;
  30. 33. Creating a Table by Using a Subquery CREATE TABLE dept80 AS SELECT employee_id, last_name, salary*12 ANNSAL, hire_date FROM employees WHERE department_id = 80; Table created. DESCRIBE dept80
  31. 34. ALTER TABLE Statement <ul><li>Use the ALTER TABLE statement to: </li></ul><ul><ul><li>Add a new column </li></ul></ul><ul><ul><li>Modify an existing column </li></ul></ul><ul><ul><li>Define a default value for the new column </li></ul></ul><ul><ul><li>Drop a column </li></ul></ul>
  32. 35. Dropping a Table <ul><ul><li>All data and structure in the table are deleted. </li></ul></ul><ul><ul><li>Any pending transactions are committed. </li></ul></ul><ul><ul><li>All indexes are dropped. </li></ul></ul><ul><ul><li>All constraints are dropped. </li></ul></ul><ul><ul><li>You cannot roll back the DROP TABLE statement. </li></ul></ul>DROP TABLE dept80; Table dropped.
  33. 36. Summary <ul><li>In this lesson, you should have learned how to use the CREATE TABLE statement to create a table and include constraints. </li></ul><ul><ul><li>Categorize the main database objects </li></ul></ul><ul><ul><li>Review the table structure </li></ul></ul><ul><ul><li>List the data types that are available for columns </li></ul></ul><ul><ul><li>Create a simple table </li></ul></ul><ul><ul><li>Understand how constraints are created at the time of table creation </li></ul></ul><ul><ul><li>Describe how schema objects work </li></ul></ul>
  34. 37. Practice 9: Overview <ul><li>This practice covers the following topics: </li></ul><ul><ul><li>Creating new tables </li></ul></ul><ul><ul><li>Creating a new table by using the CREATE TABLE AS syntax </li></ul></ul><ul><ul><li>Verifying that tables exist </li></ul></ul><ul><ul><li>Dropping tables </li></ul></ul>

×