Presentation is loading. Please wait.

Presentation is loading. Please wait.

8-1 Creating Tables. 8-2 Creating Tables: Syntax CREATE TABLE table (column datatype[column_constraint], column datatype[column_constraint], column datatype[column_constraint],...

Similar presentations


Presentation on theme: "8-1 Creating Tables. 8-2 Creating Tables: Syntax CREATE TABLE table (column datatype[column_constraint], column datatype[column_constraint], column datatype[column_constraint],..."— Presentation transcript:

1 8-1 Creating Tables

2 8-2 Creating Tables: Syntax CREATE TABLE table (column datatype[column_constraint], column datatype[column_constraint], column datatype[column_constraint],... [table_constraint]); [table_constraint]); CREATE TABLE table (column datatype[column_constraint], column datatype[column_constraint], column datatype[column_constraint],... [table_constraint]); [table_constraint]);

3 8-3 Datatypes Datatype VARCHAR2(size) CHAR(size) NUMBER NUMBER(p,s) DATELONGDescription Variable length character values Fixed length character values Floating point numbers Number values Date and time values Variable length character values up to 2 GB

4 8-4 Constraints Enforce rules at the table level. Enforce rules at the table level. Prevent the deletion of a table if there are dependencies. Prevent the deletion of a table if there are dependencies. The following constraint types are valid : The following constraint types are valid : – NOT NULL – UNIQUE – PRIMARY KEY – FOREIGN KEY – CHECK

5 8-5 Table Instance Chart: S_DEPT Column nameIDNAMEREGION_ID Key typePKFK NN/UKNN, UNN, U2U2 FK tableS_REGION FK columnID DatatypeNUMBERCHARNUMBER Length7257 Sample data10Finance1 31Sales1 32Sales2 33Sales3

6 8-6 Create Table: Example SQL> CREATE TABLE s_dept 2 (idNUMBER(7) 3 CONSTRAINT s_dept_id_pk PRIMARY KEY, 4 nameVARCHAR2(25) 5 CONSTRAINT s_dept_name_nn NOT NULL, 6 region_idNUMBER(7) 7 CONSTRAINT s_dept_region_id_fk REFERENCES 8 s_region (id), 9 CONSTRAINT s_dept_name_region_id_uk UNIQUE 10 (name, region_id));

7 8-7 Creating a Table by Using a Subquery: Syntax Create a table and insert rows by combining the CREATE TABLE command and AS subquery option. Create a table and insert rows by combining the CREATE TABLE command and AS subquery option. Match number of specified columns to number of subquery columns. Match number of specified columns to number of subquery columns. Define columns with column names, default values, and integrity constraints only. Define columns with column names, default values, and integrity constraints only. CREATE TABLE table [column(, column...)] [column(, column...)] AS subquery; AS subquery; CREATE TABLE table [column(, column...)] [column(, column...)] AS subquery; AS subquery;

8 8-8 Creating a Table by Using a Subquery: Example Create a table containing all employees in department number 41 in the S_EMP table. Create a table containing all employees in department number 41 in the S_EMP table. Do not forget that only the NOT NULL constraint is copied. Do not forget that only the NOT NULL constraint is copied. CREATE TABLE emp_41 AS SELECTid, last_name, userid, start_date FROMs_emp WHEREdept_id = 41;

9 8-9 Removing a Table: Example DROP Table emp; Table dropped. DROP Table emp; Table dropped.


Download ppt "8-1 Creating Tables. 8-2 Creating Tables: Syntax CREATE TABLE table (column datatype[column_constraint], column datatype[column_constraint], column datatype[column_constraint],..."

Similar presentations


Ads by Google