SlideShare a Scribd company logo
DDL Statements
Objectives
• 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
Database Objects
Object Description
Table Basic unit of storage; composed of rows
and columns
View Logically represents subsets of data from
one or more tables
Sequence Generates primary key values
Index Improves the performance of some queries
Synonym Gives alternative names to objects
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
CREATE TABLE Statement
• You must have :
• CREATE TABLE privilege
• A storage area
• You specify:
• Table name
• Column name, column data type, and column
size
CREATE TABLE [schema.]table
(column data type [DEFAULT expr];
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
pseudo column.
• The default data type must match the column
data type.
Creating Tables
SQL> CREATE TABLE department
2 (deptno NUMBER(2),
3 dname VARCHAR2(14),
4 loc VARCHAR2(13));
Table created.
Table created.
• Create the table.
• Confirm table creation.
SQL> DESCRIBE department
Name Null? Type
--------------------------- -------- ---------
DEPTNO NOT NULL NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
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 *
2 FROM user_tables;
SQL> SELECT DISTINCT object_type
2 FROM user_objects;
SQL> SELECT *
2 FROM user_catalog;
Data types
Data type Description
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
CLOB Single-byte character data up to 4
gigabytes
RAW and LONG RAW Raw binary data
BLOB Binary data up to 4 gigabytes
BFILE Binary data stored in an external
file; up to 4 gigabytes
Create Table Using Subquery
• Create a table and insert rows by combining
the CREATE TABLE statement and AS
subquery option.
• Match the number of specified columns to
the number of subquery columns.
• Define columns with column names and
default values.
CREATE TABLE table
[column(, column...)]
AS subquery;
Name Null? Type
---------------------------- -------- -----
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
ANNSAL NUMBER
HIREDATE DATE
SQL> DESCRIBE dept30
SQL> CREATE TABLE dept30
2 AS
3 SELECT empno, ename, sal*12 ANNSAL, hiredate
4 FROM employee
5 WHERE deptno = 30;
Table created.
Table created.
Create Table Using Subquery
ALTER TABLE Statement
• Add a new column
• Modify an existing column
• Drop an existing column,
• Define a default value for the new column
ALTER TABLE table
ADD (column data type [DEFAULT expr]
[, column data type]...);
ALTER TABLE table
MODIFY (column data type [DEFAULT expr]
[, column data type]...);
ALTER TABLE table
DROP column column_name;
Adding a Column
DEPT30
DEPT30
EMPNO ENAME ANNSAL HIREDATE
------ ---------- --------
7698 BLAKE 34200 01-MAY-81
7654 MARTIN 15000 28-SEP-81
7499 ALLEN 19200 20-FEB-81
7844 TURNER 18000 08-SEP-81
...
“…
“…add a
add a
new
new
column
column
into
into
DEPT30
DEPT30
table…”
table…”
DEPT30
DEPT30
EMPNO ENAME ANNSAL HIREDATE
------ ---------- --------
7698 BLAKE 34200 01-MAY-81
7654 MARTIN 15000 28-SEP-81
7499 ALLEN 19200 20-FEB-81
7844 TURNER 18000 08-SEP-81
...
JOB
JOB
New column
New column
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.
SQL> ALTER TABLE dept30
2 ADD (job VARCHAR2(9));
Table altered.
Table altered.
• The new column becomes the last column.
Modifying a Column
• You can change a column's data type, size,
and default value.
• A change to the default value affects only
subsequent insertions to the table.
ALTER TABLE dept30
MODIFY (ename VARCHAR2(15));
Table altered.
Table altered.
Dropping a Column
• You can remove a column and its contents
entirely from the table.
ALTER TABLE dept30
DROP COLUMN ename;
Table altered.
Table altered.
Dropping a Table
• All data and structure in the table is
deleted.
• Any pending transactions are committed.
• All indexes are dropped.
• You cannot roll back this statement.
SQL> DROP TABLE dept30;
Table dropped.
Table dropped.
Rename 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.
SQL> RENAME dept TO department;
Table renamed.
Table renamed.
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
SQL> TRUNCATE TABLE department;
Table truncated.
Table truncated.
Adding Comments to a Table
• You can add comments to a table or column
by using the COMMENT statement.
• Comments can be viewed through the data
dictionary views.
• ALL_COL_COMMENTS
• USER_COL_COMMENTS
• ALL_TAB_COMMENTS
• USER_TAB_COMMENTS
SQL> COMMENT ON TABLE employee
2 IS 'Employee Information';
Comment created.
Comment created.
Constraints
Objectives
• 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.
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
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.
Defining Constraints
CREATE TABLE [schema.]table
(column data type [DEFAULT expr]
[column_constraint],
…
[table_constraint]);
CREATE TABLE employee(
empno NUMBER(4),
ename VARCHAR2(10),
…
deptno NUMBER(7,2) NOT NULL,
CONSTRAINT emp_empno_pk
PRIMARY KEY (EMPNO));
Defining Constraints
• Column constraint level
• Table constraint level
column [CONSTRAINT constraint_name] constraint_type,
column,...
[CONSTRAINT constraint_name] constraint_type
(column, ...),
The NOT NULL Constraint
•Ensures that null values are not
permitted for the column
EMP
EMP
EMPNO ENAME JOB ... COMM DEPTNO
7839 KING PRESIDENT 10
7698 BLAKE MANAGER 30
7782 CLARK MANAGER 10
7566 JONES MANAGER 20
...
NOT NULL constraint
NOT NULL constraint
(no row may contain
(no row may contain
a null value for
a null value for
this column)
this column)
Absence of NOT NULL
Absence of NOT NULL
constraint
constraint
(any row can contain
(any row can contain
null for this column)
null for this column)
NOT NULL constraint
NOT NULL constraint
The NOT NULL Constraint
•Defined at the column level
SQL> CREATE TABLE employee(
2 empno NUMBER(4),
3 ename VARCHAR2(10) NOT NULL,
4 job VARCHAR2(9),
5 mgr NUMBER(4),
6 hiredate DATE,
7 sal NUMBER(7,2),
8 comm NUMBER(7,2),
9 deptno NUMBER(7,2) NOT NULL);
The UNIQUE Key Constraint
DEPARTMENT
DEPARTMENT
DEPTNO DNAME LOC
------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
UNIQUE key constraint
UNIQUE key constraint
50 SALES DETROIT
60 BOSTON
Insert into
Insert into Not allowed
Not allowed
(DNAME
(DNAMESALES
already exists)
already exists)
Allowed
Allowed
The UNIQUE Key Constraint
•Defined at either the table level or the
column level
SQL> CREATE TABLE department(
2 deptno NUMBER(2),
3 dname VARCHAR2(14),
4 loc VARCHAR2(13),
5 CONSTRAINT dept_dname_uk UNIQUE(dname));
PRIMARY KEY Constraint
DEPARTMENT
DEPARTMENT
DEPTNO DNAME LOC
------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
PRIMARY KEY
PRIMARY KEY
Insert into
Insert into
20 MARKETING DALLAS
FINANCE NEW YORK
Not allowed
Not allowed
(DEPTNO
(DEPTNO20 already
20 already
exists)
exists)
Not allowed
Not allowed
(DEPTNO is null)
(DEPTNO is null)
PRIMARY KEY Constraint
•Defined at either the table level or the
column level
SQL> CREATE TABLE department(
2 deptno NUMBER(2),
3 dname VARCHAR2(14),
4 loc VARCHAR2(13),
5 CONSTRAINT dept_dname_uk UNIQUE (dname),
6 CONSTRAINT dept_deptno_pk PRIMARY KEY(deptno));
FOREIGN KEY Constraint
DEPARTMENT
DEPARTMENT
DEPTNO DNAME LOC
------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
...
PRIMARY
PRIMARY
KEY
KEY
EMPLOYEE
EMPLOYEE
EMPNO ENAME JOB ... COMM DEPTNO
7839 KING PRESIDENT 10
7698 BLAKE MANAGER 30
...
FOREIGN
FOREIGN
KEY
KEY
7571 FORD MANAGER ... 200 9
7571 FORD MANAGER ... 200
Insert into
Insert into
Not allowed
Not allowed
(DEPTNO
(DEPTNO9
9
does not exist
does not exist
in the DEPT
in the DEPT
table
table
Allowed
Allowed
FOREIGN KEY Constraint
•Defined at either the table level or the
column level
SQL> CREATE TABLE employee(
2 empno NUMBER(4),
3 ename VARCHAR2(10) NOT NULL,
4 job VARCHAR2(9),
5 mgr NUMBER(4),
6 hiredate DATE,
7 sal NUMBER(7,2),
8 comm NUMBER(7,2),
9 deptno NUMBER(7,2) NOT NULL,
10 CONSTRAINT emp_deptno_fk FOREIGN KEY (deptno)
11 REFERENCES dept (deptno));
FOREIGN KEY Constraint
• 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
Keywords :
The CHECK Constraint
• Defines a condition that each row must satisfy
..., deptno NUMBER(2),
CONSTRAINT emp_deptno_ck
CHECK (DEPTNO BETWEEN 10 AND 99),...
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
ALTER TABLE table
ADD [CONSTRAINT constraint] type (column);
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 employee
2 ADD CONSTRAINT emp_mgr_fk
3 FOREIGN KEY(mgr) REFERENCES emp(empno);
Table altered.
Table altered.
Dropping a Constraint
• Remove the manager constraint from the
EMP table.
SQL> ALTER TABLE employee
2 DROP CONSTRAINT emp_mgr_fk;
Table altered.
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 TABLE department
2 DROP PRIMARY KEY CASCADE;
Table altered.
Table altered.
Disabling Constraints
• Execute the DISABLE clause of the ALTER
TABLE statement to deactivate an integrity
constraint.
• Apply the CASCADE option to disable
dependent integrity constraints.
SQL> ALTER TABLE employee
2 DISABLE CONSTRAINT emp_empno_pk CASCADE;
Table altered.
Table altered.
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 key or PRIMARY KEY constraint.
SQL> ALTER TABLE employee
2 ENABLE CONSTRAINT emp_empno_pk;
Table altered.
Table altered.
Viewing Constraints
•Query the USER_CONSTRAINTS table to
view all constraint definitions and names.
CONSTRAINT_NAME C SEARCH_CONDITION
------------------------ - -------------------------
SYS_C00674 C EMPNO IS NOT NULL
SYS_C00675 C DEPTNO IS NOT NULL
EMP_EMPNO_PK P
...
SQL> SELECT constraint_name, constraint_type,
2 search_condition
3 FROM user_constraints
4 WHERE table_name = 'EMPLOYEE';
Columns with Constraints
CONSTRAINT_NAME COLUMN_NAME
------------------------- ----------------------
EMP_DEPTNO_FK DEPTNO
EMP_EMPNO_PK EMPNO
EMP_MGR_FK MGR
SYS_C00674 EMPNO
SYS_C00675 DEPTNO
SQL> SELECT constraint_name, column_name
2 FROM user_cons_columns
3 WHERE table_name = 'EMPLOYEE';
•View the columns associated with the
constraint names in the
USER_CONS_COLUMNS view
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.
DML Statements
Objectives
• Insert rows into a table
• Update rows in a table
• Delete rows from a table
• Controlling the Transactions
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 transaction consists of a collection of
DML statements that form a logical unit of
work.
INSERT Statement
• Add new rows to a table by using the
INSERT statement.
• Only one row is inserted at a time with this
syntax.
INSERT INTO table [(column [, column...])]
VALUES (value [, value...]);
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
single quotation marks.
SQL> INSERT INTO department (deptno, dname, loc)
2 VALUES (50, 'DEVELOPMENT', 'DETROIT');
1 row created.
1 row created.
Insert Rows with Null Values
• Implicit method: Omit the column from the
column list.
SQL> INSERT INTO department (deptno, dname )
2 VALUES (60, 'MIS');
1 row created.
1 row created.
• Explicit method: Specify the NULL
keyword.
SQL> INSERT INTO department
2 VALUES (70, 'FINANCE', NULL);
1 row created.
1 row created.
Inserting Special Values
•The SYSDATE and USER function
records the current date and time.
SQL> INSERT INTO employee (empno, ename, job,
2 mgr, hiredate, sal, comm,
3 deptno)
4 VALUES (7196, USER, 'SALESMAN',
5 7782, SYSDATE, 2000, NULL,
6 10);
1 row created.
1 row created.
Inserting Specific Date Values
• Add a new employee.
SQL> INSERT INTO employee
2 VALUES (2296,'AROMANO','SALESMAN',7782,
3 TO_DATE('FEB 3,97', 'MON DD, YY'),
4 1300, NULL, 10);
1 row created.
1 row created.
• Verify your addition.
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ------- -------- ---- --------- ---- ---- ------
2296 AROMANO SALESMAN 7782 03-FEB-97 1300 10
Substitution Variables (&)
•Create an interactive script by using SQL*Plus
substitution parameters.
SQL> INSERT INTO DEPARTMENT(deptno, dname, loc)
2 VALUES (&department_id,
3 '&department_name', '&location');
Enter value for department_id: 80
80
Enter value for department_name: EDUCATION
EDUCATION
Enter value for location: ATLANTA
ATLANTA
1 row created.
Substitution Variables (&&)
•Use the double –ampersand(&&) if you want to
reuse the variable value without prompting the
user each time.
SQL> SELECT empno,ename,job, &&column_name
2 FROM employee
3 ORDER BY &column_name;
Enter value for column_name: deptno
EMPNO ENAME JOB DEPTNO
------- ----------------- -------------- --------
7839 KING PRESIDENT 10
7782 CLARK MANAGER 10
7934 MILLER CLERK 10
. . .
14 rows selected.
Copying from Another Table
• Write your INSERT statement with a
subquery.
• Do not use the VALUES clause.
• Match the number of columns in the
INSERT clause to those in the subquery.
SQL> INSERT INTO managers(id, name, salary, hiredate)
2 SELECT empno, ename, sal, hiredate
3 FROM employee
4 WHERE job = 'MANAGER';
3 rows created.
3 rows created.
UPDATE Statement
• 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];
Updating Rows in a Table
• All rows in the table are modified if you
omit the WHERE clause.
SQL> UPDATE employee
2 SET deptno = 20;
14 rows updated.
14 rows updated.
UPDATE emp
*
ERROR at line 1:
ORA-02291: integrity constraint (USR.EMP_DEPTNO_FK)
violated - parent key not found
SQL> UPDATE employee
2 SET deptno = 55
3 WHERE deptno = 10;
Updating Rows:
•Department number 55 does not exist
•Integrity Constraint Error
DELETE Statement
•You can remove existing rows from a
table by using the DELETE statement.
DELETE [FROM] table
[WHERE condition];
• Specific row or rows are deleted when you
specify the WHERE clause.
• All rows in the table are deleted if you omit
the WHERE clause.
Deleting Rows from a Table
SQL> DELETE FROM department
2 WHERE dname = 'DEVELOPMENT';
1 row deleted.
1 row deleted.
SQL> DELETE FROM department;
4 rows deleted.
4 rows deleted.
Deleting Rows:
SQL> DELETE FROM department
2 WHERE deptno = 10;
DELETE FROM dept
*
ERROR at line 1:
ORA-02292: integrity constraint (USR.EMP_DEPTNO_FK)
violated - child record found
•You cannot delete a row
that contains a
primary key that is used as a foreign key in
another table.
•Integrity Constraint Error

More Related Content

Similar to Structure query language - Data definition language.ppt (20)

PPTX
Lab
neelam_rawat
 
PPTX
DBMS information in detail || Dbms (lab) ppt
gourav kottawar
 
PPTX
SQL _UNIT_DBMS_PRESENTSTATION_SQL _UNIT_DBMS_PRESENTSTATION
deeptanshudas100
 
PPSX
DBMS Chapter-3.ppsx
DharmikPatel745100
 
PPTX
2..basic queries.pptx
MalaikaRahatQurashi
 
PPTX
DBMS UNIT-2.pptx ggggggggggggggggggggggg
Praveen Kumar
 
PPTX
MySQL Essential Training
HudaRaghibKadhim
 
PPTX
ms-sql-server-150223140402-conversion-gate02.pptx
YashaswiniSrinivasan1
 
PDF
Rdbms day3
Nitesh Singh
 
PDF
SQL-8 Table Creation.pdf
HannanKhalid4
 
PPTX
Lesson-02 (1).pptx
ssuserc24e05
 
PPT
Using ddl statements to create and manage tables
Syed Zaid Irshad
 
PPT
MySql slides (ppt)
webhostingguy
 
PDF
DBMS_ddlVFSBFSBS22222222222222222222222222222222222
227567
 
PDF
SQL Overview
Stewart Rogers
 
PPT
SQL - Data Definition Language
Imam340267
 
PPT
Les10.ppt
AlhassanFederated
 
DOCX
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
SakkaravarthiS1
 
DBMS information in detail || Dbms (lab) ppt
gourav kottawar
 
SQL _UNIT_DBMS_PRESENTSTATION_SQL _UNIT_DBMS_PRESENTSTATION
deeptanshudas100
 
DBMS Chapter-3.ppsx
DharmikPatel745100
 
2..basic queries.pptx
MalaikaRahatQurashi
 
DBMS UNIT-2.pptx ggggggggggggggggggggggg
Praveen Kumar
 
MySQL Essential Training
HudaRaghibKadhim
 
ms-sql-server-150223140402-conversion-gate02.pptx
YashaswiniSrinivasan1
 
Rdbms day3
Nitesh Singh
 
SQL-8 Table Creation.pdf
HannanKhalid4
 
Lesson-02 (1).pptx
ssuserc24e05
 
Using ddl statements to create and manage tables
Syed Zaid Irshad
 
MySql slides (ppt)
webhostingguy
 
DBMS_ddlVFSBFSBS22222222222222222222222222222222222
227567
 
SQL Overview
Stewart Rogers
 
SQL - Data Definition Language
Imam340267
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
SakkaravarthiS1
 

Recently uploaded (20)

PPTX
Explore USA’s Best Structural And Non Structural Steel Detailing
Silicon Engineering Consultants LLC
 
PPTX
Introduction to File Transfer Protocol with commands in FTP
BeulahS2
 
PPTX
Alan Turing - life and importance for all of us now
Pedro Concejero
 
PDF
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
PDF
Designing for Tomorrow – Architecture’s Role in the Sustainability Movement
BIM Services
 
PDF
June 2025 Top 10 Sites -Electrical and Electronics Engineering: An Internatio...
elelijjournal653
 
PDF
Python Mini Project: Command-Line Quiz Game for School/College Students
MPREETHI7
 
PDF
NFPA 10 - Estandar para extintores de incendios portatiles (ed.22 ENG).pdf
Oscar Orozco
 
PDF
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
 
PPTX
Precooling and Refrigerated storage.pptx
ThongamSunita
 
PDF
bs-en-12390-3 testing hardened concrete.pdf
ADVANCEDCONSTRUCTION
 
PDF
PROGRAMMING REQUESTS/RESPONSES WITH GREATFREE IN THE CLOUD ENVIRONMENT
samueljackson3773
 
DOCX
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
PDF
Clustering Algorithms - Kmeans,Min ALgorithm
Sharmila Chidaravalli
 
PDF
Module - 4 Machine Learning -22ISE62.pdf
Dr. Shivashankar
 
PPTX
darshai cross section and river section analysis
muk7971
 
PDF
Tesia Dobrydnia - An Avid Hiker And Backpacker
Tesia Dobrydnia
 
PDF
Plant Control_EST_85520-01_en_AllChanges_20220127.pdf
DarshanaChathuranga4
 
PPTX
UNIT 1 - INTRODUCTION TO AI and AI tools and basic concept
gokuld13012005
 
PDF
LLC CM NCP1399 SIMPLIS MODEL MANUAL.PDF
ssuser1be9ce
 
Explore USA’s Best Structural And Non Structural Steel Detailing
Silicon Engineering Consultants LLC
 
Introduction to File Transfer Protocol with commands in FTP
BeulahS2
 
Alan Turing - life and importance for all of us now
Pedro Concejero
 
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
Designing for Tomorrow – Architecture’s Role in the Sustainability Movement
BIM Services
 
June 2025 Top 10 Sites -Electrical and Electronics Engineering: An Internatio...
elelijjournal653
 
Python Mini Project: Command-Line Quiz Game for School/College Students
MPREETHI7
 
NFPA 10 - Estandar para extintores de incendios portatiles (ed.22 ENG).pdf
Oscar Orozco
 
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
 
Precooling and Refrigerated storage.pptx
ThongamSunita
 
bs-en-12390-3 testing hardened concrete.pdf
ADVANCEDCONSTRUCTION
 
PROGRAMMING REQUESTS/RESPONSES WITH GREATFREE IN THE CLOUD ENVIRONMENT
samueljackson3773
 
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
Clustering Algorithms - Kmeans,Min ALgorithm
Sharmila Chidaravalli
 
Module - 4 Machine Learning -22ISE62.pdf
Dr. Shivashankar
 
darshai cross section and river section analysis
muk7971
 
Tesia Dobrydnia - An Avid Hiker And Backpacker
Tesia Dobrydnia
 
Plant Control_EST_85520-01_en_AllChanges_20220127.pdf
DarshanaChathuranga4
 
UNIT 1 - INTRODUCTION TO AI and AI tools and basic concept
gokuld13012005
 
LLC CM NCP1399 SIMPLIS MODEL MANUAL.PDF
ssuser1be9ce
 
Ad

Structure query language - Data definition language.ppt

  • 2. Objectives • 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
  • 3. Database Objects Object Description Table Basic unit of storage; composed of rows and columns View Logically represents subsets of data from one or more tables Sequence Generates primary key values Index Improves the performance of some queries Synonym Gives alternative names to objects
  • 4. 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
  • 5. CREATE TABLE Statement • You must have : • CREATE TABLE privilege • A storage area • You specify: • Table name • Column name, column data type, and column size CREATE TABLE [schema.]table (column data type [DEFAULT expr];
  • 6. 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 pseudo column. • The default data type must match the column data type.
  • 7. Creating Tables SQL> CREATE TABLE department 2 (deptno NUMBER(2), 3 dname VARCHAR2(14), 4 loc VARCHAR2(13)); Table created. Table created. • Create the table. • Confirm table creation. SQL> DESCRIBE department Name Null? Type --------------------------- -------- --------- DEPTNO NOT NULL NUMBER(2) DNAME VARCHAR2(14) LOC VARCHAR2(13)
  • 8. 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 * 2 FROM user_tables; SQL> SELECT DISTINCT object_type 2 FROM user_objects; SQL> SELECT * 2 FROM user_catalog;
  • 9. Data types Data type Description 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 CLOB Single-byte character data up to 4 gigabytes RAW and LONG RAW Raw binary data BLOB Binary data up to 4 gigabytes BFILE Binary data stored in an external file; up to 4 gigabytes
  • 10. Create Table Using Subquery • Create a table and insert rows by combining the CREATE TABLE statement and AS subquery option. • Match the number of specified columns to the number of subquery columns. • Define columns with column names and default values. CREATE TABLE table [column(, column...)] AS subquery;
  • 11. Name Null? Type ---------------------------- -------- ----- EMPNO NOT NULL NUMBER(4) ENAME VARCHAR2(10) ANNSAL NUMBER HIREDATE DATE SQL> DESCRIBE dept30 SQL> CREATE TABLE dept30 2 AS 3 SELECT empno, ename, sal*12 ANNSAL, hiredate 4 FROM employee 5 WHERE deptno = 30; Table created. Table created. Create Table Using Subquery
  • 12. ALTER TABLE Statement • Add a new column • Modify an existing column • Drop an existing column, • Define a default value for the new column ALTER TABLE table ADD (column data type [DEFAULT expr] [, column data type]...); ALTER TABLE table MODIFY (column data type [DEFAULT expr] [, column data type]...); ALTER TABLE table DROP column column_name;
  • 13. Adding a Column DEPT30 DEPT30 EMPNO ENAME ANNSAL HIREDATE ------ ---------- -------- 7698 BLAKE 34200 01-MAY-81 7654 MARTIN 15000 28-SEP-81 7499 ALLEN 19200 20-FEB-81 7844 TURNER 18000 08-SEP-81 ... “… “…add a add a new new column column into into DEPT30 DEPT30 table…” table…” DEPT30 DEPT30 EMPNO ENAME ANNSAL HIREDATE ------ ---------- -------- 7698 BLAKE 34200 01-MAY-81 7654 MARTIN 15000 28-SEP-81 7499 ALLEN 19200 20-FEB-81 7844 TURNER 18000 08-SEP-81 ... JOB JOB New column New column
  • 14. 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. SQL> ALTER TABLE dept30 2 ADD (job VARCHAR2(9)); Table altered. Table altered. • The new column becomes the last column.
  • 15. Modifying a Column • You can change a column's data type, size, and default value. • A change to the default value affects only subsequent insertions to the table. ALTER TABLE dept30 MODIFY (ename VARCHAR2(15)); Table altered. Table altered.
  • 16. Dropping a Column • You can remove a column and its contents entirely from the table. ALTER TABLE dept30 DROP COLUMN ename; Table altered. Table altered.
  • 17. Dropping a Table • All data and structure in the table is deleted. • Any pending transactions are committed. • All indexes are dropped. • You cannot roll back this statement. SQL> DROP TABLE dept30; Table dropped. Table dropped.
  • 18. Rename 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. SQL> RENAME dept TO department; Table renamed. Table renamed.
  • 19. 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 SQL> TRUNCATE TABLE department; Table truncated. Table truncated.
  • 20. Adding Comments to a Table • You can add comments to a table or column by using the COMMENT statement. • Comments can be viewed through the data dictionary views. • ALL_COL_COMMENTS • USER_COL_COMMENTS • ALL_TAB_COMMENTS • USER_TAB_COMMENTS SQL> COMMENT ON TABLE employee 2 IS 'Employee Information'; Comment created. Comment created.
  • 22. Objectives • 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.
  • 23. 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
  • 24. 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.
  • 25. Defining Constraints CREATE TABLE [schema.]table (column data type [DEFAULT expr] [column_constraint], … [table_constraint]); CREATE TABLE employee( empno NUMBER(4), ename VARCHAR2(10), … deptno NUMBER(7,2) NOT NULL, CONSTRAINT emp_empno_pk PRIMARY KEY (EMPNO));
  • 26. Defining Constraints • Column constraint level • Table constraint level column [CONSTRAINT constraint_name] constraint_type, column,... [CONSTRAINT constraint_name] constraint_type (column, ...),
  • 27. The NOT NULL Constraint •Ensures that null values are not permitted for the column EMP EMP EMPNO ENAME JOB ... COMM DEPTNO 7839 KING PRESIDENT 10 7698 BLAKE MANAGER 30 7782 CLARK MANAGER 10 7566 JONES MANAGER 20 ... NOT NULL constraint NOT NULL constraint (no row may contain (no row may contain a null value for a null value for this column) this column) Absence of NOT NULL Absence of NOT NULL constraint constraint (any row can contain (any row can contain null for this column) null for this column) NOT NULL constraint NOT NULL constraint
  • 28. The NOT NULL Constraint •Defined at the column level SQL> CREATE TABLE employee( 2 empno NUMBER(4), 3 ename VARCHAR2(10) NOT NULL, 4 job VARCHAR2(9), 5 mgr NUMBER(4), 6 hiredate DATE, 7 sal NUMBER(7,2), 8 comm NUMBER(7,2), 9 deptno NUMBER(7,2) NOT NULL);
  • 29. The UNIQUE Key Constraint DEPARTMENT DEPARTMENT DEPTNO DNAME LOC ------ ---------- -------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON UNIQUE key constraint UNIQUE key constraint 50 SALES DETROIT 60 BOSTON Insert into Insert into Not allowed Not allowed (DNAME (DNAMESALES already exists) already exists) Allowed Allowed
  • 30. The UNIQUE Key Constraint •Defined at either the table level or the column level SQL> CREATE TABLE department( 2 deptno NUMBER(2), 3 dname VARCHAR2(14), 4 loc VARCHAR2(13), 5 CONSTRAINT dept_dname_uk UNIQUE(dname));
  • 31. PRIMARY KEY Constraint DEPARTMENT DEPARTMENT DEPTNO DNAME LOC ------ ---------- -------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON PRIMARY KEY PRIMARY KEY Insert into Insert into 20 MARKETING DALLAS FINANCE NEW YORK Not allowed Not allowed (DEPTNO (DEPTNO20 already 20 already exists) exists) Not allowed Not allowed (DEPTNO is null) (DEPTNO is null)
  • 32. PRIMARY KEY Constraint •Defined at either the table level or the column level SQL> CREATE TABLE department( 2 deptno NUMBER(2), 3 dname VARCHAR2(14), 4 loc VARCHAR2(13), 5 CONSTRAINT dept_dname_uk UNIQUE (dname), 6 CONSTRAINT dept_deptno_pk PRIMARY KEY(deptno));
  • 33. FOREIGN KEY Constraint DEPARTMENT DEPARTMENT DEPTNO DNAME LOC ------ ---------- -------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS ... PRIMARY PRIMARY KEY KEY EMPLOYEE EMPLOYEE EMPNO ENAME JOB ... COMM DEPTNO 7839 KING PRESIDENT 10 7698 BLAKE MANAGER 30 ... FOREIGN FOREIGN KEY KEY 7571 FORD MANAGER ... 200 9 7571 FORD MANAGER ... 200 Insert into Insert into Not allowed Not allowed (DEPTNO (DEPTNO9 9 does not exist does not exist in the DEPT in the DEPT table table Allowed Allowed
  • 34. FOREIGN KEY Constraint •Defined at either the table level or the column level SQL> CREATE TABLE employee( 2 empno NUMBER(4), 3 ename VARCHAR2(10) NOT NULL, 4 job VARCHAR2(9), 5 mgr NUMBER(4), 6 hiredate DATE, 7 sal NUMBER(7,2), 8 comm NUMBER(7,2), 9 deptno NUMBER(7,2) NOT NULL, 10 CONSTRAINT emp_deptno_fk FOREIGN KEY (deptno) 11 REFERENCES dept (deptno));
  • 35. FOREIGN KEY Constraint • 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 Keywords :
  • 36. The CHECK Constraint • Defines a condition that each row must satisfy ..., deptno NUMBER(2), CONSTRAINT emp_deptno_ck CHECK (DEPTNO BETWEEN 10 AND 99),...
  • 37. 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 ALTER TABLE table ADD [CONSTRAINT constraint] type (column);
  • 38. 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 employee 2 ADD CONSTRAINT emp_mgr_fk 3 FOREIGN KEY(mgr) REFERENCES emp(empno); Table altered. Table altered.
  • 39. Dropping a Constraint • Remove the manager constraint from the EMP table. SQL> ALTER TABLE employee 2 DROP CONSTRAINT emp_mgr_fk; Table altered. 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 TABLE department 2 DROP PRIMARY KEY CASCADE; Table altered. Table altered.
  • 40. Disabling Constraints • Execute the DISABLE clause of the ALTER TABLE statement to deactivate an integrity constraint. • Apply the CASCADE option to disable dependent integrity constraints. SQL> ALTER TABLE employee 2 DISABLE CONSTRAINT emp_empno_pk CASCADE; Table altered. Table altered.
  • 41. 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 key or PRIMARY KEY constraint. SQL> ALTER TABLE employee 2 ENABLE CONSTRAINT emp_empno_pk; Table altered. Table altered.
  • 42. Viewing Constraints •Query the USER_CONSTRAINTS table to view all constraint definitions and names. CONSTRAINT_NAME C SEARCH_CONDITION ------------------------ - ------------------------- SYS_C00674 C EMPNO IS NOT NULL SYS_C00675 C DEPTNO IS NOT NULL EMP_EMPNO_PK P ... SQL> SELECT constraint_name, constraint_type, 2 search_condition 3 FROM user_constraints 4 WHERE table_name = 'EMPLOYEE';
  • 43. Columns with Constraints CONSTRAINT_NAME COLUMN_NAME ------------------------- ---------------------- EMP_DEPTNO_FK DEPTNO EMP_EMPNO_PK EMPNO EMP_MGR_FK MGR SYS_C00674 EMPNO SYS_C00675 DEPTNO SQL> SELECT constraint_name, column_name 2 FROM user_cons_columns 3 WHERE table_name = 'EMPLOYEE'; •View the columns associated with the constraint names in the USER_CONS_COLUMNS view
  • 44. 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.
  • 46. Objectives • Insert rows into a table • Update rows in a table • Delete rows from a table • Controlling the Transactions
  • 47. 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 transaction consists of a collection of DML statements that form a logical unit of work.
  • 48. INSERT Statement • Add new rows to a table by using the INSERT statement. • Only one row is inserted at a time with this syntax. INSERT INTO table [(column [, column...])] VALUES (value [, value...]);
  • 49. 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 single quotation marks. SQL> INSERT INTO department (deptno, dname, loc) 2 VALUES (50, 'DEVELOPMENT', 'DETROIT'); 1 row created. 1 row created.
  • 50. Insert Rows with Null Values • Implicit method: Omit the column from the column list. SQL> INSERT INTO department (deptno, dname ) 2 VALUES (60, 'MIS'); 1 row created. 1 row created. • Explicit method: Specify the NULL keyword. SQL> INSERT INTO department 2 VALUES (70, 'FINANCE', NULL); 1 row created. 1 row created.
  • 51. Inserting Special Values •The SYSDATE and USER function records the current date and time. SQL> INSERT INTO employee (empno, ename, job, 2 mgr, hiredate, sal, comm, 3 deptno) 4 VALUES (7196, USER, 'SALESMAN', 5 7782, SYSDATE, 2000, NULL, 6 10); 1 row created. 1 row created.
  • 52. Inserting Specific Date Values • Add a new employee. SQL> INSERT INTO employee 2 VALUES (2296,'AROMANO','SALESMAN',7782, 3 TO_DATE('FEB 3,97', 'MON DD, YY'), 4 1300, NULL, 10); 1 row created. 1 row created. • Verify your addition. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ------- -------- ---- --------- ---- ---- ------ 2296 AROMANO SALESMAN 7782 03-FEB-97 1300 10
  • 53. Substitution Variables (&) •Create an interactive script by using SQL*Plus substitution parameters. SQL> INSERT INTO DEPARTMENT(deptno, dname, loc) 2 VALUES (&department_id, 3 '&department_name', '&location'); Enter value for department_id: 80 80 Enter value for department_name: EDUCATION EDUCATION Enter value for location: ATLANTA ATLANTA 1 row created.
  • 54. Substitution Variables (&&) •Use the double –ampersand(&&) if you want to reuse the variable value without prompting the user each time. SQL> SELECT empno,ename,job, &&column_name 2 FROM employee 3 ORDER BY &column_name; Enter value for column_name: deptno EMPNO ENAME JOB DEPTNO ------- ----------------- -------------- -------- 7839 KING PRESIDENT 10 7782 CLARK MANAGER 10 7934 MILLER CLERK 10 . . . 14 rows selected.
  • 55. Copying from Another Table • Write your INSERT statement with a subquery. • Do not use the VALUES clause. • Match the number of columns in the INSERT clause to those in the subquery. SQL> INSERT INTO managers(id, name, salary, hiredate) 2 SELECT empno, ename, sal, hiredate 3 FROM employee 4 WHERE job = 'MANAGER'; 3 rows created. 3 rows created.
  • 56. UPDATE Statement • 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];
  • 57. Updating Rows in a Table • All rows in the table are modified if you omit the WHERE clause. SQL> UPDATE employee 2 SET deptno = 20; 14 rows updated. 14 rows updated.
  • 58. UPDATE emp * ERROR at line 1: ORA-02291: integrity constraint (USR.EMP_DEPTNO_FK) violated - parent key not found SQL> UPDATE employee 2 SET deptno = 55 3 WHERE deptno = 10; Updating Rows: •Department number 55 does not exist •Integrity Constraint Error
  • 59. DELETE Statement •You can remove existing rows from a table by using the DELETE statement. DELETE [FROM] table [WHERE condition];
  • 60. • Specific row or rows are deleted when you specify the WHERE clause. • All rows in the table are deleted if you omit the WHERE clause. Deleting Rows from a Table SQL> DELETE FROM department 2 WHERE dname = 'DEVELOPMENT'; 1 row deleted. 1 row deleted. SQL> DELETE FROM department; 4 rows deleted. 4 rows deleted.
  • 61. Deleting Rows: SQL> DELETE FROM department 2 WHERE deptno = 10; DELETE FROM dept * ERROR at line 1: ORA-02292: integrity constraint (USR.EMP_DEPTNO_FK) violated - child record found •You cannot delete a row that contains a primary key that is used as a foreign key in another table. •Integrity Constraint Error