Download presentation
Presentation is loading. Please wait.
Published byBryce Page Modified over 9 years ago
2
Copyright Oracle Corporation, 1998. All rights reserved. Introduction
3
4-2 Copyright Oracle Corporation, 1998. All rights reserved. Log in to SQL*Plus. Edit your SQL statement. Execute SQL from SQL*Plus. Save SQL statements to files and append SQL statements to files. Execute saved files. Log in to SQL*Plus. Edit your SQL statement. Execute SQL from SQL*Plus. Save SQL statements to files and append SQL statements to files. Execute saved files. Overview of SQL*Plus
4
4-3 Copyright Oracle Corporation, 1998. All rights reserved. Logging Into SQL*Plus Use the following username in Lab 60Use the following username in Lab 60 – User name :scott – Password: csdb – Host string :orcl Use the following username in Lab 60Use the following username in Lab 60 – User name :scott – Password: csdb – Host string :orcl
5
4-4 Copyright Oracle Corporation, 1998. All rights reserved. Logging In to SQL*Plus From Windows environment:From Windows environment: From Windows environment:From Windows environment:
6
4-5 Copyright Oracle Corporation, 1998. All rights reserved. SQL Statements SELECT CREATEALTERDROPRENAMETRUNCATEINSERTUPDATEDELETECOMMITROLLBACKSAVEPOINTGRANTREVOKESELECT CREATEALTERDROPRENAMETRUNCATEINSERTUPDATEDELETECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data retrieval Data manipulation language (DML) Data definition language (DDL) Transaction control Data control language (DCL)
7
4-6 Copyright Oracle Corporation, 1998. All rights reserved. Writing SQL Statements SQL statements are not case sensitive. SQL statements can be on one or more lines. Keywords cannot be abbreviated or split across lines. Tabs and indents are used to enhance readability. SQL statements are not case sensitive. SQL statements can be on one or more lines. Keywords cannot be abbreviated or split across lines. Tabs and indents are used to enhance readability.
8
4-7 Copyright Oracle Corporation, 1998. All rights reserved. Data Definition Language (DDL)
9
4-8 Copyright Oracle Corporation, 1998. All rights reserved. Objectives After completing this lesson, you should be able to do the following: Describe the main database objects Create tables Describe the data types that can be used when specifying column definition Alter table definitions Drop, rename, and truncate tables After completing this lesson, you should be able to do the following: Describe the main database objects Create tables Describe the data types that can be used when specifying column definition Alter table definitions Drop, rename, and truncate tables
10
4-9 Copyright Oracle Corporation, 1998. All rights reserved. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns View Logically represents subsets of data from one or more tables Sequence Generates primary key values IndexImproves the performance of some queries Synonym Gives alternative names to objects
11
4-10 Copyright Oracle Corporation, 1998. All rights reserved. Naming Conventions Must begin with a letter Can be 1–30 characters long Must contain only A–Z, a–z, 0–9, _, $, and # Must not duplicate the name of another object owned by the same user Must not be an Oracle Server reserved word Must begin with a letter Can be 1–30 characters long Must contain only A–Z, a–z, 0–9, _, $, and # Must not duplicate the name of another object owned by the same user Must not be an Oracle Server reserved word
12
4-11 Copyright Oracle Corporation, 1998. All rights reserved. Creating Schema
13
4-12 Copyright Oracle Corporation, 1998. All rights reserved. Schema Schema is used to group together tables and other constructs that belong to the same database application. An SQL schema is identified by a schema name, and includes an authorization to indicate the user or account who owns the schema. Schema is used to group together tables and other constructs that belong to the same database application. An SQL schema is identified by a schema name, and includes an authorization to indicate the user or account who owns the schema. CREATE SCHEMA schema AUTHORIZATION user;
14
4-13 Copyright Oracle Corporation, 1998. All rights reserved. Creating Tables
15
4-14 Copyright Oracle Corporation, 1998. All rights reserved. The CREATE TABLE Statement You must have : – CREATE TABLE privilege – A storage area You specify: – Table name – Column name, column datatype, and column size You must have : – CREATE TABLE privilege – A storage area You specify: – Table name – Column name, column datatype, and column size CREATE TABLE [schema.]table (column datatype [DEFAULT expr];
16
4-15 Copyright Oracle Corporation, 1998. All rights reserved. The DEFAULT Option Specify a default value for a column during an insert. … hiredate DATE DEFAULT SYSDATE, … Legal values are literal value, expression, or SQL function. Illegal values are another column’s name or pseudocolumn. The default datatype must match the column datatype. Legal values are literal value, expression, or SQL function. Illegal values are another column’s name or pseudocolumn. The default datatype must match the column datatype.
17
4-16 Copyright Oracle Corporation, 1998. All rights reserved. Creating Tables SQL> CREATE TABLE dept 2(deptno NUMBER(2), 3 dname VARCHAR2(14), 4 loc VARCHAR2(13)); Table created. Create the table. Confirm table creation. SQL> DESCRIBE dept Name Null? Type --------------------------- -------- --------- DEPTNO NOT NULL NUMBER(2) DNAME VARCHAR2(14) LOC VARCHAR2(13)
18
4-17 Copyright Oracle Corporation, 1998. All rights reserved. Querying the Data Dictionary Describe tables owned by the user. View distinct object types owned by the user. View tables, views, synonyms, and sequences owned by the user. SQL> SELECT* 2FROMuser_tables; SQL> SELECTDISTINCT object_type 2FROM user_objects; SQL> SELECT* 2FROMuser_catalog;
19
4-18 Copyright Oracle Corporation, 1998. All rights reserved. Data types DatatypeDescription VARCHAR2(size)Variable-length character data CHAR(size) Fixed-length character data NUMBER(p,s) Variable-length numeric data DATE Date and time values LONG Variable-length character data up to 2 gigabytes
20
4-19 Copyright Oracle Corporation, 1998. All rights reserved. ALTER TABLE
21
4-20 Copyright Oracle Corporation, 1998. All rights reserved. The ALTER TABLE Statement Use the ALTER TABLE statement to: Add a new column, Drop existing Column, Modify an existing column, Rename existing column. Use the ALTER TABLE statement to: Add a new column, Drop existing Column, Modify an existing column, Rename existing column.
22
4-21 Copyright Oracle Corporation, 1998. All rights reserved. The ALTER TABLE Statement Add a new column: ALTER TABLE table ADD (column datatype [DEFAULT expr] [, column datatype]...);
23
4-22 Copyright Oracle Corporation, 1998. All rights reserved. Adding a Column DEPT30 EMPNO ENAME ANNSAL HIREDATE ------ ------------------ 7698BLAKE 3420001-MAY-81 7654MARTIN 1500028-SEP-81 7499ALLEN 1920020-FEB-81 7844TURNER 1800008-SEP-81... “…add a new column into DEPT30 table…” DEPT30 EMPNO ENAME ANNSAL HIREDATE ------ ------------------ 7698BLAKE 3420001-MAY-81 7654MARTIN 1500028-SEP-81 7499ALLEN 1920020-FEB-81 7844TURNER 1800008-SEP-81... JOB New column
24
4-23 Copyright Oracle Corporation, 1998. All rights reserved. Adding a Column You use the ADD clause to add columns: 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... 6 rows selected. 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... 6 rows selected. SQL> ALTER TABLE dept30 2 ADD (job VARCHAR2(9)); Table altered. The new column becomes the last column.
25
4-24 Copyright Oracle Corporation, 1998. All rights reserved. Modify a Column Modify an existing column (add a default value, change data type): ALTER TABLE table MODIFY (column datatype [DEFAULT expr] [, column datatype]...);
26
4-25 Copyright Oracle Corporation, 1998. All rights reserved. Modifying a Column Change a column's data type: ALTER TABLEdept30 MODIFY(ename VARCHAR2(15)); Table altered.
27
4-26 Copyright Oracle Corporation, 1998. All rights reserved. Rename a Column Rename existing column: ALTER TABLE table RENAME Column (column to column);
28
4-27 Copyright Oracle Corporation, 1998. All rights reserved. Rename a Column You can change a column's name: ALTER TABLEdept30 Rename Column ename to employee_name; Table altered. ALTER TABLEdept30 Rename Column ename to employee_name; Table altered.
29
4-28 Copyright Oracle Corporation, 1998. All rights reserved. Drop a Column Drop existing column: ALTER TABLE table Drop COLUMN column;
30
4-29 Copyright Oracle Corporation, 1998. All rights reserved. Drop a Column Drop existing column: ALTER TABLEdept30 Drop column ename; Table altered. ALTER TABLEdept30 Drop column ename; Table altered.
31
4-30 Copyright Oracle Corporation, 1998. All rights reserved. Drop Table
32
4-31 Copyright Oracle Corporation, 1998. All rights reserved. Dropping a Table All data and structure in the table is deleted. All indexes are dropped. You cannot roll back this statement. All data and structure in the table is deleted. All indexes are dropped. You cannot roll back this statement. SQL> DROP TABLE dept30; Table dropped.
33
4-32 Copyright Oracle Corporation, 1998. All rights reserved. Changing Object Name
34
4-33 Copyright Oracle Corporation, 1998. All rights reserved. Changing the Name of an Object To change the name of a table, view, sequence, or synonym, you execute the RENAME statement. You must be the owner of the object. To change the name of a table, view, sequence, or synonym, you execute the RENAME statement. You must be the owner of the object. SQL> RENAME dept TO department; Table renamed.
35
4-34 Copyright Oracle Corporation, 1998. All rights reserved. Truncating Tables
36
4-35 Copyright Oracle Corporation, 1998. All rights reserved. Truncating a Table The TRUNCATE TABLE statement: – Removes all rows from a table – Releases the storage space used by that table Cannot roll back row removal when using TRUNCATE Alternatively, remove rows by using the DELETE statement The TRUNCATE TABLE statement: – Removes all rows from a table – Releases the storage space used by that table Cannot roll back row removal when using TRUNCATE Alternatively, remove rows by using the DELETE statement SQL> TRUNCATE TABLE department; Table truncated.
37
4-36 Copyright Oracle Corporation, 1998. All rights reserved. Summary Statement Description CREATE TABLE Creates a table ALTER TABLE Modifies table structures DROP TABLE Removes the rows and table structure RENAME Changes the name of a table, view, sequence, or synonym TRUNCATE Removes all rows from a table and releases the storage space
38
4-37 Copyright Oracle Corporation, 1998. All rights reserved. Practice Overview Creating new tables Creating a new table by using the CREATE TABLE AS syntax Modifying column definitions Verifying that the tables exist Dropping tables Altering tables Creating new tables Creating a new table by using the CREATE TABLE AS syntax Modifying column definitions Verifying that the tables exist Dropping tables Altering tables
39
4-38 Copyright Oracle Corporation, 1998. All rights reserved. ConstraintsConstraints
40
4-39 Copyright Oracle Corporation, 1998. All rights reserved. What Are Constraints? Constraints enforce rules at the table level. Constraints prevent the deletion of a table if there are dependencies. The following constraint types are valid in Oracle: – NOT NULL – UNIQUE Key – PRIMARY KEY – FOREIGN KEY – CHECK Constraints enforce rules at the table level. Constraints prevent the deletion of a table if there are dependencies. The following constraint types are valid in Oracle: – NOT NULL – UNIQUE Key – PRIMARY KEY – FOREIGN KEY – CHECK
41
4-40 Copyright Oracle Corporation, 1998. All rights reserved. Constraint Guidelines Name a constraint or the Oracle Server will generate a name by using the SYS_Cn format. Create a constraint: – At the same time as the table is created, – After the table has been created. Define a constraint at the column or table level. View a constraint in the data dictionary. Name a constraint or the Oracle Server will generate a name by using the SYS_Cn format. Create a constraint: – At the same time as the table is created, – After the table has been created. Define a constraint at the column or table level. View a constraint in the data dictionary.
42
4-41 Copyright Oracle Corporation, 1998. All rights reserved. Defining Constraints CREATE TABLE [schema.]table (column datatype [DEFAULT expr] [column_constraint], … [table_constraint]); CREATE TABLE emp( empno NUMBER(4), ename VARCHAR2(10), … deptno NUMBER(7,2) NOT NULL, CONSTRAINT emp_empno_pk PRIMARY KEY (EMPNO));
43
4-42 Copyright Oracle Corporation, 1998. All rights reserved. Defining Constraints Column constraint level Table constraint level Column constraint level Table constraint level column [CONSTRAINT constraint_name] constraint_type, column,... [CONSTRAINT constraint_name] constraint_type (column,...), column,... [CONSTRAINT constraint_name] constraint_type (column,...),
44
4-43 Copyright Oracle Corporation, 1998. All rights reserved. The NOT NULL Constraint Ensures that null values are not permitted for the column EMP EMPNO ENAME JOB... COMM DEPTNO 7839KINGPRESIDENT 10 7698BLAKEMANAGER 30 7782CLARKMANAGER 10 7566JONESMANAGER 20... NOT NULL constraint (no row may contain a null value for this column) Absence of NOT NULL constraint (any row can contain null for this column) NOT NULL constraint
45
4-44 Copyright Oracle Corporation, 1998. All rights reserved. The NOT NULL Constraint Defined at the column level SQL> CREATE TABLE emp( 2 empno NUMBER(4), 3enameVARCHAR2(10) NOT NULL, 4jobVARCHAR2(9), 5mgrNUMBER(4), 6hiredateDATE, 7salNUMBER(7,2), 8 commNUMBER(7,2), 9deptnoNUMBER(7,2) NOT NULL);
46
4-45 Copyright Oracle Corporation, 1998. All rights reserved. The UNIQUE Key Constraint DEPT DEPTNO DNAME LOC ------ ------------------ 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON UNIQUE key constraint 50SALESDETROIT 60BOSTON Insert into Not allowed (DNAME already exists) Not allowed (DNAME SALES already exists)Allowed
47
4-46 Copyright Oracle Corporation, 1998. All rights reserved. The UNIQUE Key Constraint Defined at either the table level or the column level SQL> CREATE TABLE dept( 2 deptno NUMBER(2), 3dname VARCHAR2(14), 4loc VARCHAR2(13), 5CONSTRAINT dept_dname_uk UNIQUE(dname));
48
4-47 Copyright Oracle Corporation, 1998. All rights reserved. The PRIMARY KEY Constraint DEPT DEPTNO DNAME LOC ------ ------------------ 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON PRIMARY KEY Insert into 20MARKETINGDALLAS FINANCENEW YORK Not allowed (DEPTNO20 already exists) Not allowed (DEPTNO 20 already exists) Not allowed (DEPTNO is null)
49
4-48 Copyright Oracle Corporation, 1998. All rights reserved. The PRIMARY KEY Constraint Defined at either the table level or the column level SQL> CREATE TABLE dept( 2 deptno NUMBER(2), 3dname VARCHAR2(14), 4loc VARCHAR2(13), 5CONSTRAINT dept_dname_uk UNIQUE (dname), 6CONSTRAINT dept_deptno_pk PRIMARY KEY(deptno));
50
4-49 Copyright Oracle Corporation, 1998. All rights reserved. The FOREIGN KEY Constraint DEPT DEPTNO DNAME LOC ------ ------------------ 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS... PRIMARY KEY EMP EMPNO ENAME JOB... COMM DEPTNO 7839KINGPRESIDENT 10 7698BLAKEMANAGER 30... FOREIGN KEY 7571FORDMANAGER... 200 9 7571FORDMANAGER... 200 Insert into Not allowed (DEPTNO9 does not exist in the DEPT table Not allowed (DEPTNO 9 does not exist in the DEPT tableAllowed
51
4-50 Copyright Oracle Corporation, 1998. All rights reserved. The FOREIGN KEY Constraint Defined at either the table level or the column level SQL> CREATE TABLE emp( 2 empno NUMBER(4), 3enameVARCHAR2(10) NOT NULL, 4jobVARCHAR2(9), 5mgrNUMBER(4), 6hiredateDATE, 7salNUMBER(7,2), 8 commNUMBER(7,2), 9deptnoNUMBER(7,2) NOT NULL, 10CONSTRAINT emp_deptno_fk FOREIGN KEY (deptno) 11REFERENCES dept (deptno));
52
4-51 Copyright Oracle Corporation, 1998. All rights reserved. FOREIGN KEY Constraint Keywords FOREIGN KEY: Defines the column in the child table at the table constraint level REFERENCES: Identifies the table and column in the parent table ON DELETE CASCADE: Allows deletion in the parent table and deletion of the dependent rows in the child table FOREIGN KEY: Defines the column in the child table at the table constraint level REFERENCES: Identifies the table and column in the parent table ON DELETE CASCADE: Allows deletion in the parent table and deletion of the dependent rows in the child table
53
4-52 Copyright Oracle Corporation, 1998. All rights reserved. The CHECK Constraint Defines a condition that each row must satisfy Expressions that are not allowed: – References to pseudocolumns CURRVAL, NEXTVAL, LEVEL, and ROWNUM – Calls to SYSDATE, UID, USER, and USERENV functions – Queries that refer to other values in other rows Defines a condition that each row must satisfy Expressions that are not allowed: – References to pseudocolumns CURRVAL, NEXTVAL, LEVEL, and ROWNUM – Calls to SYSDATE, UID, USER, and USERENV functions – Queries that refer to other values in other rows..., deptnoNUMBER(2), CONSTRAINT emp_deptno_ck CHECK (DEPTNO BETWEEN 10 AND 99),...
54
4-53 Copyright Oracle Corporation, 1998. All rights reserved. Adding a Constraint Add or drop, but not modify a constraint Enable or disable constraints Add a NOT NULL constraint by using the MODIFY clause. Add or drop, but not modify a constraint Enable or disable constraints Add a NOT NULL constraint by using the MODIFY clause. ALTER TABLE table ADD [CONSTRAINT constraint] type (column); ALTER TABLE table ADD [CONSTRAINT constraint] type (column);
55
4-54 Copyright Oracle Corporation, 1998. All rights reserved. Adding a Constraint 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 TABLE emp 2 ADD CONSTRAINT emp_mgr_fk 3 FOREIGN KEY(mgr) REFERENCES emp(empno); Table altered.
56
4-55 Copyright Oracle Corporation, 1998. All rights reserved. Dropping a Constraint Remove the manager constraint from the EMP table. SQL> ALTER TABLE emp 2 DROP CONSTRAINT emp_mgr_fk; Table altered. SQL> ALTER TABLE emp 2 DROP CONSTRAINT emp_mgr_fk; Table altered. Remove the PRIMARY KEY constraint on the DEPT table and drop the associated FOREIGN KEY constraint on the EMP.DEPTNO column. SQL> ALTER TABLEdept 2 DROP PRIMARY KEY CASCADE; Table altered. SQL> ALTER TABLEdept 2 DROP PRIMARY KEY CASCADE; Table altered.
57
4-56 Copyright Oracle Corporation, 1998. All rights reserved. Summary Create the following types of constraints: – NOT NULL – UNIQUE key – PRIMARY KEY – FOREIGN KEY – CHECK Query the USER_CONSTRAINTS table to view all constraint definitions and names. Create the following types of constraints: – NOT NULL – UNIQUE key – PRIMARY KEY – FOREIGN KEY – CHECK Query the USER_CONSTRAINTS table to view all constraint definitions and names.
58
4-57 Copyright Oracle Corporation, 1998. All rights reserved.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.