View
250
Download
0
Category
Preview:
Citation preview
8/12/2019 day2 session1
1/45
Entry Level Technology Program
Satyam Learning Center ORACLE 1 of 1
DDL and DML statements
8/12/2019 day2 session1
2/45
Entry Level Technology Program
Satyam Learning Center ORACLE 2 of 1
Naming Rules
Table names and column names:
Must begin with a letter
Must be 130 characters long
Must contain only AZ, az, 09, _, $, and #
Must not duplicate the name of another objectowned by the same user
Must not be an Oracle server reserved word
8/12/2019 day2 session1
3/45
Entry Level Technology Program
Satyam Learning Center ORACLE 3 of 1
The CREATE TABLEStatement
You must have:
CREATE TABLE privilege
A storage area
You specify:
Table nameColumn name, column data type, and column
size.
CREATE TABLE [schema.]table(columndatatype[DEFAULT expr][, ...]);
8/12/2019 day2 session1
4/45
Entry Level Technology Program
Satyam Learning Center ORACLE 4 of 1
The DEFAULTOption
Specify a default value for a column duringan insert.
Literal values, expressions, or SQL functionsare legal values.
Another columns name or a pseudocolumnare illegal values.
The default data type must match thecolumn data type.
... hire_date DATE DEFAULT SYSDATE, ...
8/12/2019 day2 session1
5/45
Entry Level Technology Program
Satyam Learning Center ORACLE 5 of 1
Create the table.
Confirm table creation.
Maximum we can place
1000 columns in Oracle9itable.
Creating Tables
CREATE TABLE dept(deptno NUMBER(2),dname VARCHAR2(14),
loc VARCHAR2(13));Table created.
DESCRIBE dept
8/12/2019 day2 session1
6/45
Entry Level Technology Program
Satyam Learning Center ORACLE 6 of 1
Tables in the OracleDatabase
User Tables:
Are a collection of tables created and
maintained by the userContain user information
Data Dictionary:
Is a collection of tables created and
maintained by the Oracle Server
Contain database information
8/12/2019 day2 session1
7/45
Entry Level Technology Program
Satyam Learning Center ORACLE 7 of 1
TheALTERTABLEStatement
Use the ALTER TABLE statement to:
Add a new column
Modify an existing column
Define a default value for the new column
Drop a column ( Only from 8i) Rename a column ( Only from 9i)
8/12/2019 day2 session1
8/45
Entry Level Technology Program
Satyam Learning Center ORACLE 8 of 1
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
In this lesson, you should have learned how to use DDLstatements to create, alter, drop, and rename tables.
8/12/2019 day2 session1
9/45
Entry Level Technology Program
Satyam Learning Center ORACLE 9 of 1
Including Constraints
8/12/2019 day2 session1
10/45
Entry Level Technology Program
Satyam Learning Center ORACLE 10 of 1
What are Constraints?
Constraints enforce rules at the table level.
Constraints prevent the deletion of a table if there aredependencies.
The following constraint types are valid:
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEYCHECK
8/12/2019 day2 session1
11/45
Entry Level Technology Program
Satyam Learning Center ORACLE 11 of 1
Constraint Guidelines
Name a constraint or the Oracle server generates aname by using the SYS_Cnformat.
Create a constraint either:At the same time as the table is created, or
After the table has been created
Define a constraint at the column or table level.
View a constraint in the data dictionary.
8/12/2019 day2 session1
12/45
Entry Level Technology Program
Satyam Learning Center ORACLE 12 of 1
Defining Constraints
CREATE TABLE [schema.]table
(columndatatype[DEFAULT expr]
[column_constraint],
...
[table_constraint][,...]);
CREATE TABLE EMP(EMPNO NUMBER(6),
ENAME VARCHAR2(20),
...JOB VARCHAR2(10) NOT NULL,
CONSTRAINT emp_emp_id_pk
PRIMARY KEY (EMPNO));
8/12/2019 day2 session1
13/45
Entry Level Technology Program
Satyam Learning Center ORACLE 13 of 1
Defining Constraints
Column constraint level
Table constraint level
column[CONSTRAINT constraint_name] constraint_type,
column,...
[CONSTRAINT constraint_name] constraint_type
(column, ...),
We cant use NOT NULL constraint at Table level.
8/12/2019 day2 session1
14/45
Entry Level Technology Program
Satyam Learning Center ORACLE 14 of 1
CREATE TABLE EMP(
EMPNO NUMBER(6),
ENAME VARCHAR2(25) NOT NULL,SAL NUMBER(8,2),
COMM NUMBER(2,2),
HIREDATE DATE
CONSTRAINT emp_hire_date_nn
NOT NULL,
...
TheNOTNULLConstraint
Is defined at the column level:
Systemnamed
Usernamed
8/12/2019 day2 session1
15/45
Entry Level Technology Program
Satyam Learning Center ORACLE 15 of 1
The UNIQUEConstraint
Defined at either the table level or the columnlevel:
CREATE TABLE EMP(EMPNO NUMBER(6),
ENAME VARCHAR2(25) NOT NULL,EMAIL VARCHAR2(25),
SAL NUMBER(8,2),
COMM NUMBER(2,2),HIREDATE DATE NOT NULL,
...
CONSTRAINT emp_email_uk UNIQUE(email));
8/12/2019 day2 session1
16/45
Entry Level Technology Program
Satyam Learning Center ORACLE 16 of 1
CREATE TABLE DEPT(
DEPTNO NUMBER(4),DNAME VARCHAR2(30)
CONSTRAINT dept_name_nn NOT NULL,
MGR NUMBER(6),
LOC NUMBER(4),
CONSTRAINT dept_id_pk PRIMARY KEY(DEPTNO));
The PRIMARYKEYConstraint
Defined at either the table level or the columnlevel:
8/12/2019 day2 session1
17/45
Entry Level Technology Program
Satyam Learning Center ORACLE 17 of 1
The FOREIGNKEYConstraint
Defined at either the table level or the column level:
CREATE TABLE EMP(
EMPNO NUMBER(6),
ENAME VARCHAR2(25) NOT NULL,EMAIL VARCHAR2(25),
SAL NUMBER(8,2),
COMM NUMBER(2,2),
HIREDATE DATE NOT NULL,
...
DEPTNO NUMBER(4),CONSTRAINT emp_dept_fk FOREIGN KEY (DEPTNO)
REFERENCES DEPT(DEPTNO),
CONSTRAINT emp_email_uk UNIQUE(email));
8/12/2019 day2 session1
18/45
Entry Level Technology Program
Satyam Learning Center ORACLE 18 of 1
FOREIGNKEYConstraint
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: Deletes the dependent rows
in the child table when a row in the parent tableis deleted.
ON DELETE SET NULL: Converts dependent
foreign key values to null
8/12/2019 day2 session1
19/45
Entry Level Technology Program
Satyam Learning Center ORACLE 19 of 1
The CHECKConstraint
Defines a condition that each row must satisfy
The following expressions are not allowed:
References to CURRVAL, NEXTVAL, LEVEL, andROWNUM pseudocolumns
Calls to SYSDATE, UID, USER, and USERENVfunctions
Queries that refer to other values in other rows
..., sal NUMBER(2)
CONSTRAINT emp_sal_min
CHECK (sal > 0),...
8/12/2019 day2 session1
20/45
Entry Level Technology Program
Satyam Learning Center ORACLE 20 of 1
Adding a Constraint Syntax
Use the ALTER TABLE statement to:
Add or drop a constraint, but not modify its structure
Enable or disable constraints
Add a NOT NULL constraint by using the MODIFY clause
ALTER TABLE table
ADD [CONSTRAINT constraint] type (column);
8/12/2019 day2 session1
21/45
Entry Level Technology Program
Satyam Learning Center ORACLE 21 of 1
Adding a Constraint
Add a FOREIGN KEY constraint to the EMPLOYEEStable indicating that a manager must already exist asa valid employee in the EMPLOYEES table.
ALTER TABLE EMP
ADD CONSTRAINT emp_manager_fk
FOREIGN KEY(MGR)
REFERENCES EMP(EMPNO);
Table altered.
8/12/2019 day2 session1
22/45
Entry Level Technology Program
Satyam Learning Center ORACLE 22 of 1
Dropping a Constraint
Remove the manager constraint from theEMPLOYEES table.
Remove the PRIMARY KEY constraint on theDEPARTMENTS table and drop the associatedFOREIGN KEY constraint on the
EMPLOYEES.DEPARTMENT_ID column.
ALTER TABLE EMP
DROP CONSTRAINT emp_manager_fk;
Table altered.
ALTER TABLE DEPT
DROP PRIMARY KEY CASCADE;Table altered.
8/12/2019 day2 session1
23/45
Entry Level Technology Program
Satyam Learning Center ORACLE 23 of 1
Cascading Constraints
The CASCADE CONSTRAINTS clause is used alongwith the DROP COLUMN clause.
The CASCADE CONSTRAINTS clause drops allreferential integrity constraints that refer to theprimary and unique keys defined on the droppedcolumns.
The CASCADE CONSTRAINTS clause also drops allmulticolumn constraints defined on the droppedcolumns.
8/12/2019 day2 session1
24/45
Entry Level Technology Program
Satyam Learning Center ORACLE 24 of 1
Cascading Constraints
Example:
ALTER TABLE test1
DROP (pk) CASCADE CONSTRAINTS;
Table altered.
ALTER TABLE test1
DROP (pk, fk, col1) CASCADE CONSTRAINTS;
Table altered.
8/12/2019 day2 session1
25/45
Entry Level Technology Program
Satyam Learning Center ORACLE 25 of 1
SELECT constraint_name, constraint_type,search_condition
FROM user_constraints
WHERE table_name = EMP';
Viewing Constraints
Query the USER_CONSTRAINTStable to view allconstraint definitions and names.
View the columns associated with the constraintnames in the USER_CONS_COLUMNS view.
SELECT constraint_name, column_name
FROM user_cons_columns
WHERE table_name = 'EMP';
8/12/2019 day2 session1
26/45
Entry Level Technology Program
Satyam Learning Center ORACLE 26 of 1
Consider a training institute conductingdifferent courses, into which the students are
joining for various courses ( Also, assume thecase where same student can join in more
than one course) The students may pay the fee in installments
Identify the tables, attributes and define them
with relations.
Case Study on Integrity Constraints
8/12/2019 day2 session1
27/45
8/12/2019 day2 session1
28/45
Entry Level Technology Program
Satyam Learning Center ORACLE 28 of 1
Data Manipulation Language
A DML statement is executed when you:
Add new rows to a table
Modify existing rows in a table
Remove existing rows from a table
A transactionconsists of a collection of DML
statements that form a logical unit of work.
8/12/2019 day2 session1
29/45
Entry Level Technology Program
Satyam Learning Center ORACLE 29 of 1
The INSERTStatement
Syntax
Add new rows to a table by using the INSERTstatement.
Only one row is inserted at a time with this syntax.
INSERT INTO table [(column [, column...])]
VALUES (value [, value...]);
8/12/2019 day2 session1
30/45
Entry Level Technology Program
Satyam Learning Center ORACLE 30 of 1
Inserting New Rows
Insert a new row containing values for each column. List values in the default order of the columns in the
table.
Optionally, list the columns in the INSERT clause.
Enclose character and date values within singlequotation marks.
INSERT INTO DEPT(DEPTNO, DNAME,
MGR, LOC)
VALUES (70, 'Public Relations', 100, 1700);
1 row created.
8/12/2019 day2 session1
31/45
Entry Level Technology Program
Satyam Learning Center ORACLE 31 of 1
INSERT INTO departments
VALUES (100, 'Finance', NULL, NULL);
1 row created.
INSERT INTO DEPT (DEPTNO,
DNAME, )VALUES (30, 'Purchasing');
1 row created.
Inserting Rows with NullValues
Implicit method: Omit the column from the column list.
Explicit method: Specify the NULLkeyword in the
VALUESclause.
8/12/2019 day2 session1
32/45
Entry Level Technology Program
Satyam Learning Center ORACLE 32 of 1
INSERT INTO EMP (EMPNO,
ENAME,
EMAIL, PHONE_NUMBER,HIREDATE, JOB, SAL,
COMM,MGR,
DEPTNO)
VALUES (113,
'Louis', 'Popp',
'LPOPP', '515.124.4567',
SYSDATE, 'AC_ACCOUNT', 6900,
NULL, 205, 100);
1 row created.
Inserting Special Values
The SYSDATE function records the current date
and time.
8/12/2019 day2 session1
33/45
Entry Level Technology Program
Satyam Learning Center ORACLE 33 of 1
INSERT INTO EMP
VALUES (114,'Den', 'Raphealy',
'DRAPHEAL', '515.127.4561',TO_DATE('FEB 3, 1999', 'MON DD, YYYY'),
'AC_ACCOUNT', 11000, NULL, 100, 30);
1 row created.
Inserting Specific DateValues
Add a new employee.
Copying Rows
8/12/2019 day2 session1
34/45
Entry Level Technology Program
Satyam Learning Center ORACLE 34 of 1
Write your INSERT statement with a subquery.
Do not use the VALUES clause.
Match the number of columns in the INSERT clause to thosein the subquery.
INSERT INTO sales_reps(id, name, salary, COMM)
SELECT EMPNO, ENAME, SAL, COMM
FROM EMPWHERE JOB LIKE '%ANA%';
4 rows created.
Copying Rowsfrom Another Table
8/12/2019 day2 session1
35/45
Entry Level Technology Program
Satyam Learning Center ORACLE 35 of 1
The UPDATEStatement
Syntax
Modify existing rows with the UPDATE statement.
Update more than one row at a time, if required.
UPDATE table
SET column= value[, column = value, ...]
[WHERE condition];
8/12/2019 day2 session1
36/45
Entry Level Technology Program
Satyam Learning Center ORACLE 36 of 1
UPDATE EMP
SET DEPTNO = 20
WHERE EMPNO = 7900;1 row updated.
Specific row or rows are modified if youspecify the WHERE clause.
All rows in the table are modified if you omitthe WHERE clause.
Updating Rows in a Table
UPDATE copy_emp
SET DEPTNO = 10;
22 rows updated.
U d ti Two C l ith S b
8/12/2019 day2 session1
37/45
Entry Level Technology Program
Satyam Learning Center ORACLE 37 of 1
UPDATE EMP
SET JOB = (SELECT JOB
FROM EMPWHERE EMPNO = 7698),
SAL = (SELECT SAL
FROM EMP
WHERE EMPNO = 7698)
WHERE EMPNO = 7839;
1 row updated.
Updating TwoColumns with a Subquery
Update employee 7839s job and salary to matchthat of employee 7698.
Updating Rows Based
8/12/2019 day2 session1
38/45
Entry Level Technology Program
Satyam Learning Center ORACLE 38 of 1
UPDATE copy_emp
SET DEPTNO = (SELECT DEPTNOFROM EMP
WHERE EMPNO = 7900)
WHERE JOB = (SELECT JOB
FROM EMP
WHERE EMPNO = 7902);
1 row updated.
Updating Rows Basedon Another Table
Use subqueries in UPDATE statements to update rows in a
table based on values from another table.
8/12/2019 day2 session1
39/45
Entry Level Technology Program
Satyam Learning Center ORACLE 39 of 1
UPDATE employees
*
ERROR at line 1:
ORA-02291: integrity constraint (HR.EMP_DEPT_FK)
violated - parent key not found
UPDATE EMP
SET DEPTNO = 50
WHERE DEPTNO = 10;
Updating Rows:Integrity Constraint Error
8/12/2019 day2 session1
40/45
Entry Level Technology Program
Satyam Learning Center ORACLE 40 of 1
The DELETEStatement
You can remove existing rows from a table by using
the DELETE statement.
DELETE [FROM] table
[WHERE condition];
8/12/2019 day2 session1
41/45
Entry Level Technology Program
Satyam Learning Center ORACLE 41 of 1
Specific rows are deleted if you specify theWHERE clause.
All rows in the table are deleted if you omitthe WHERE clause.
Deleting Rows from a Table
DELETE FROM DEPT
WHERE DNAME = 'Finance';1 row deleted.
DELETE FROM copy_emp;
22 rows deleted.
8/12/2019 day2 session1
42/45
8/12/2019 day2 session1
43/45
Entry Level Technology Program
Satyam Learning Center ORACLE 43 of 1
Deleting Rows:Integrity Constraint Error
DELETE FROM DEPT
WHERE DEPTNO = 20;
DELETE FROM departments
*
ERROR at line 1:
ORA-02292: integrity constraint (HR.EMP_DEPT_FK)
violated - child record found
U i S b i
8/12/2019 day2 session1
44/45
Entry Level Technology Program
Satyam Learning Center ORACLE 44 of 1
Using a Subquery in an INSERT
Statement
INSERT INTO(SELECT EMPNO, ENAME,
EMAIL, HIREDATE, JOB, SAL,DEPTNO
FROM EMPWHERE DEPTNO = 30)
VALUES (99999, 'Taylor', 'DTAYLOR',TO_DATE('07-JUN-99', 'DD-MON-RR'),'ST_CLERK', 5000, 50);
1 row created.
i b i
8/12/2019 day2 session1
45/45
Entry Level Technology Program
Using a Subquery in anINSERTStatement
Verify the results
SELECT EMPNO, ENAME, EMAIL, HIREDATE,
JOB, SAL, DEPTNO
FROM EMP
WHERE DEPTNO = 20;
Recommended