SlideShare a Scribd company logo
1 of 25
SQL Database Language
Lab2: SQL Commands DDL & DML
DDL Commands
 DDL statements are used to build and modify the
structure of your tables and other objects in the
database.
 CREATE – Creates objects in the database
 ALTER – Alters objects of the database
 DROP – Deletes objects of the database
 TRUNCATE – Deletes all records from a table and resets table
identity to initial value.
 When you execute a DDL statement, it takes effect
immediately.
DDL Commands – CREATE TABLE
 The CREATE TABLE statement is used to
create a table in a database.
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);
DDL Commands – Data Types
Data Type Description
VARCHAR2(SIZE)
Where size is the number of characters to store.
Variable-length string.
CHAR(SIZE)
Where size is the number of characters to store. Fixed-
length strings. Space padded.
NUMBER(P,S)
Where p is the precision and s is the scale.
For example, number(7,2) is a number that has 5 digits
before the decimal and 2 digits after the decimal.
DECIMAL(P,S) Where p is the precision and s is the scale.
INT Integer value
DATE Stores year, month, and day values(DD-MON-YY)
TIMESTAMP Stores year, month, day, hour, minute, and second values
DDL Commands – Large Objects Data
Types
 Large Objects (LOBs) are a set of data types that
are designed to hold large amounts of data.
 An LOB can hold up to a maximum size ranging
from 8 terabytes to 128 terabytes depending on
how your database is configured.
 Storing data in LOBs enables you to access and
manipulate the data efficiently in your application.
Refer
to
this
site
for
more
information
(Oracle
Documentation):
https://docs.oracle.com/cd/E18283_01/appdev.112/e18294/adlob_intro.ht
m
DDL Commands – Large Objects Data
Types
Large Object
(LOB) Data Type
Description
BLOB
Binary Large Object
Stores any kind of data in binary format. Typically used for
multimedia data such as images, audio, and video.
CLOB
Character Large
Object
Stores string data in the database character set format. Used for
large strings or documents that use the database character set
exclusively. Characters in the database character set are in a fixed
width format.
NCLOB
National Character
Set Large Object
Stores string data in National Character Set format. Used for large
strings or documents in the National Character Set. Supports
characters of varying width format.
BFILE
External Binary File
A binary file stored outside of the database in the host operating
system file system, but accessible from database tables. BFILEs
can be accessed from your application on a read-only basis.
Use BFILEs to store static data, such as image data, that is not
manipulated in applications.
Refer
to
this
site
for
more
information
(Oracle
Documentation):
https://docs.oracle.com/cd/E18283_01/appdev.112/e18294/adlob_intro.ht
m
DDL Commands – CREATE TABLE
 Example on CREATE TABLE:
DDL Commands – ALTER TABLE
 The ALTER TABLE statement is used to add,
delete, or modify columns in an existing table. It is
also used to rename a table or a column.
 To add a column in a table, use the following
syntax:
ALTER TABLE table_name
ADD column_name datatype
DDL Commands – ALTER TABLE
 To modify the data type of a column in a table, use the
following syntax, also you can modify more than one column
at once.
ALTER TABLE table_name
MODIFY column_name datatype
ALTER TABLE table_name
MODIFY (column_name1 datatype,
column_name2 datatype,
………………..
column_nameN datatype);
Modify single column Modify multiple columns
DDL Commands – ALTER TABLE
 To delete a column in a table, use the following
syntax
ALTER TABLE table_name
DROP COLUMN column_name
DDL Commands – ALTER TABLE
 To rename a column or a table , use the following
syntax:
ALTER TABLE table_name
RENAME COLUMN old_name to
new_name;
ALTER TABLE table_name
RENAME TO new_table_name;
Rename a column
Rename a table
DDL Commands – DROP TABLE
 DROP TABLE statement allows you to delete a
table from the SQL database.
DROP TABLE table_name;
DDL Commands – Constraints
 SQL constraints are used to specify rules
for the data in a table.
 If there is any violation between the
constraint and the data action, the action is
aborted by the constraint.
 Constraints can be specified when the table
is created (in a CREATE TABLE statement)
or after the table is created (in an ALTER
TABLE statement).
Adding / Deleting Constraints
 There are two ways of adding
constraints:
◦ Within table creation
◦ Using ALTER query
CREATE TABLE student(
ID Number(8),
CPR Number(9),
Name Varchar2(15),
GPA Number(3,2),
Major Varchar2(5) Not Null,
Year INT Default 1,
PRIMARY KEY (ID),
UNIQUE (CPR) );
CREATE TABLE table_name
(
column_name1 data_type(size) constraint_name,
column_name2 data_type(size) constraint_name,
column_name3 data_type(size) constraint_name,
....
);
Adding / Deleting Constraints
ALTER TABLE student
ADD CONSTRAINT student_id_pk PRIMARY KEY (id);
ALTER TABLE student
ADD CONSTRAINT student_cpr_un UNIQUE (cpr);
ALTER TABLE student
MODIFY major NOT NULL;
ALTER TABLE student
MODIFY year DEFAULT 1;
 Using ALTER query
ALTER TABLE table_name
DROP CONSTRAINT constraint_name;
ALTER TABLE student
DROP CONSTRAINT student_id_pk;
Adding / Deleting Constraints
 Constraints can be enabled or
disabled instead of dropping and
adding again.
Enabling / Disabling
Constraints
ALTER TABLE table_name
DISABLE CONSTRAINT constraint_name;
ALTER TABLE student
DISABLE CONSTRAINT student_id_pk;
ALTER TABLE table_name
ENABLE CONSTRAINT constraint_name;
ALTER TABLE student
ENABLE CONSTRAINT student_id_pk;
DDL Commands – Constraints- NOT
NULL
 The NOT NULL constraint enforces a column to
NOT accept NULL values.
 The NOT NULL constraint enforces a field to
always contain a value. This means that you
cannot insert a new record, or update a record
without adding a value to this field.
DDL Commands – Constraints- UNIQUE
 The UNIQUE constraint uniquely identifies each
record in a database table.
 The UNIQUE and PRIMARY KEY constraints both
provide a guarantee for uniqueness for a column or
set of columns.
 A PRIMARY KEY constraint automatically has a
UNIQUE constraint defined on it.
 Note that you can have many UNIQUE constraints
per table, but only one PRIMARY KEY constraint.
DDL Commands – Constraints- PRIMARY
KEY
 The PRIMARY KEY constraint uniquely identifies
each record in a database table.
 Primary keys must contain UNIQUE values.
 A primary key column cannot contain NULL values.
 Most tables should have a primary key, and each
table can have only ONE primary key.
DDL Commands – Constraints- FOREIGN
KEY
 A FOREIGN KEY in one table
points to a PRIMARY KEY in
another table.
 The FOREIGN KEY constraint is
used to prevent actions that
would destroy links between
tables.
 The FOREIGN KEY constraint
also prevents invalid data from
being inserted into the foreign
key column, because it has to be
one of the values contained in
the table it points to.
DEPT table
EMP table
Primary Key
Foreign Key
DML Commands – INSERT INTO
 The INSERT INTO statement is used to insert new
records in a table.
INSERT INTO table_name
VALUES (value1,value2,value3,...);
INSERT INTO table_name (column1,column3,column6,...)
VALUES (value1,value3,value6,...);
 It is also possible to only insert data in specific
columns.
DML Commands – UPDATE
 The UPDATE statement is used to update existing
records in a table.
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
 NOTE: The WHERE clause specifies which record or records that should be
updated. If you omit the WHERE clause, all records will be updated!
DML Commands – DELETE
 The DELETE statement is used to delete rows in a table.
DELETE FROM table_name
WHERE some_column=some_value;
 NOTE: The WHERE clause specifies which record or records that should be
deleted . If you omit the WHERE clause, all records will be deleted!
 It is possible to delete all rows in a table without deleting the
table.
DELETE FROM table_name;
Lab2 ddl commands

More Related Content

What's hot (20)

joins in database
 joins in database joins in database
joins in database
 
Mysql
MysqlMysql
Mysql
 
12 SQL
12 SQL12 SQL
12 SQL
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
Constraints In Sql
Constraints In SqlConstraints In Sql
Constraints In Sql
 
SQL commands
SQL commandsSQL commands
SQL commands
 
Chapter 4 Structured Query Language
Chapter 4 Structured Query LanguageChapter 4 Structured Query Language
Chapter 4 Structured Query Language
 
SQL JOINS
SQL JOINSSQL JOINS
SQL JOINS
 
Basic SQL and History
 Basic SQL and History Basic SQL and History
Basic SQL and History
 
SQL Joins.pptx
SQL Joins.pptxSQL Joins.pptx
SQL Joins.pptx
 
database language ppt.pptx
database language ppt.pptxdatabase language ppt.pptx
database language ppt.pptx
 
SQL(DDL & DML)
SQL(DDL & DML)SQL(DDL & DML)
SQL(DDL & DML)
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
Triggers
TriggersTriggers
Triggers
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
sql function(ppt)
sql function(ppt)sql function(ppt)
sql function(ppt)
 
4. plsql
4. plsql4. plsql
4. plsql
 
Sql joins
Sql joinsSql joins
Sql joins
 

Similar to Lab2 ddl commands

Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...SakkaravarthiS1
 
Sql smart reference_by_prasad
Sql smart reference_by_prasadSql smart reference_by_prasad
Sql smart reference_by_prasadpaddu123
 
Sql smart reference_by_prasad
Sql smart reference_by_prasadSql smart reference_by_prasad
Sql smart reference_by_prasadpaddu123
 
MySQL notes - Basic Commands and Definitions
MySQL notes - Basic Commands and DefinitionsMySQL notes - Basic Commands and Definitions
MySQL notes - Basic Commands and DefinitionsDeepakDeedarSingla
 
Relational Database Language.pptx
Relational Database Language.pptxRelational Database Language.pptx
Relational Database Language.pptxSheethal Aji Mani
 
Getting Started with MySQL I
Getting Started with MySQL IGetting Started with MySQL I
Getting Started with MySQL ISankhya_Analytics
 
Sql basics and DDL statements
Sql basics and DDL statementsSql basics and DDL statements
Sql basics and DDL statementsMohd Tousif
 
Sql ch 12 - creating database
Sql ch 12 - creating databaseSql ch 12 - creating database
Sql ch 12 - creating databaseMukesh Tekwani
 
Dms 22319 micro project
Dms 22319 micro projectDms 22319 micro project
Dms 22319 micro projectARVIND SARDAR
 
Lecture on DBMS & MySQL.pdf v. C. .
Lecture on DBMS & MySQL.pdf v.  C.     .Lecture on DBMS & MySQL.pdf v.  C.     .
Lecture on DBMS & MySQL.pdf v. C. .MayankSinghRawat6
 
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptxMy lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptxEliasPetros
 

Similar to Lab2 ddl commands (20)

Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
 
lovely
lovelylovely
lovely
 
SQL2.pptx
SQL2.pptxSQL2.pptx
SQL2.pptx
 
Introduction to SQL..pdf
Introduction to SQL..pdfIntroduction to SQL..pdf
Introduction to SQL..pdf
 
Sql smart reference_by_prasad
Sql smart reference_by_prasadSql smart reference_by_prasad
Sql smart reference_by_prasad
 
Sql smart reference_by_prasad
Sql smart reference_by_prasadSql smart reference_by_prasad
Sql smart reference_by_prasad
 
MySQL notes - Basic Commands and Definitions
MySQL notes - Basic Commands and DefinitionsMySQL notes - Basic Commands and Definitions
MySQL notes - Basic Commands and Definitions
 
Relational Database Language.pptx
Relational Database Language.pptxRelational Database Language.pptx
Relational Database Language.pptx
 
SQL | DML
SQL | DMLSQL | DML
SQL | DML
 
SQL
SQLSQL
SQL
 
Oracle 11g SQL Overview
Oracle 11g SQL OverviewOracle 11g SQL Overview
Oracle 11g SQL Overview
 
Getting Started with MySQL I
Getting Started with MySQL IGetting Started with MySQL I
Getting Started with MySQL I
 
Sql basics and DDL statements
Sql basics and DDL statementsSql basics and DDL statements
Sql basics and DDL statements
 
Sql ch 12 - creating database
Sql ch 12 - creating databaseSql ch 12 - creating database
Sql ch 12 - creating database
 
Dms 22319 micro project
Dms 22319 micro projectDms 22319 micro project
Dms 22319 micro project
 
MySQL Essential Training
MySQL Essential TrainingMySQL Essential Training
MySQL Essential Training
 
Lecture on DBMS & MySQL.pdf v. C. .
Lecture on DBMS & MySQL.pdf v.  C.     .Lecture on DBMS & MySQL.pdf v.  C.     .
Lecture on DBMS & MySQL.pdf v. C. .
 
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptxMy lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
 
Sql intro & ddl 1
Sql intro & ddl 1Sql intro & ddl 1
Sql intro & ddl 1
 
Sql intro & ddl 1
Sql intro & ddl 1Sql intro & ddl 1
Sql intro & ddl 1
 

More from Balqees Al.Mubarak (9)

Using scripts
Using scriptsUsing scripts
Using scripts
 
Single row functions
Single row functionsSingle row functions
Single row functions
 
Sequences and indexes
Sequences and indexesSequences and indexes
Sequences and indexes
 
Oracle views
Oracle viewsOracle views
Oracle views
 
Lab5 sub query
Lab5   sub queryLab5   sub query
Lab5 sub query
 
Lab4 join - all types listed
Lab4   join - all types listedLab4   join - all types listed
Lab4 join - all types listed
 
Lab3 aggregating data
Lab3   aggregating dataLab3   aggregating data
Lab3 aggregating data
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
 
Using social network sites
Using social network sites Using social network sites
Using social network sites
 

Recently uploaded

ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 

Recently uploaded (20)

ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 

Lab2 ddl commands

  • 1. SQL Database Language Lab2: SQL Commands DDL & DML
  • 2. DDL Commands  DDL statements are used to build and modify the structure of your tables and other objects in the database.  CREATE – Creates objects in the database  ALTER – Alters objects of the database  DROP – Deletes objects of the database  TRUNCATE – Deletes all records from a table and resets table identity to initial value.  When you execute a DDL statement, it takes effect immediately.
  • 3. DDL Commands – CREATE TABLE  The CREATE TABLE statement is used to create a table in a database. CREATE TABLE table_name ( column_name1 data_type(size), column_name2 data_type(size), column_name3 data_type(size), .... );
  • 4. DDL Commands – Data Types Data Type Description VARCHAR2(SIZE) Where size is the number of characters to store. Variable-length string. CHAR(SIZE) Where size is the number of characters to store. Fixed- length strings. Space padded. NUMBER(P,S) Where p is the precision and s is the scale. For example, number(7,2) is a number that has 5 digits before the decimal and 2 digits after the decimal. DECIMAL(P,S) Where p is the precision and s is the scale. INT Integer value DATE Stores year, month, and day values(DD-MON-YY) TIMESTAMP Stores year, month, day, hour, minute, and second values
  • 5. DDL Commands – Large Objects Data Types  Large Objects (LOBs) are a set of data types that are designed to hold large amounts of data.  An LOB can hold up to a maximum size ranging from 8 terabytes to 128 terabytes depending on how your database is configured.  Storing data in LOBs enables you to access and manipulate the data efficiently in your application. Refer to this site for more information (Oracle Documentation): https://docs.oracle.com/cd/E18283_01/appdev.112/e18294/adlob_intro.ht m
  • 6. DDL Commands – Large Objects Data Types Large Object (LOB) Data Type Description BLOB Binary Large Object Stores any kind of data in binary format. Typically used for multimedia data such as images, audio, and video. CLOB Character Large Object Stores string data in the database character set format. Used for large strings or documents that use the database character set exclusively. Characters in the database character set are in a fixed width format. NCLOB National Character Set Large Object Stores string data in National Character Set format. Used for large strings or documents in the National Character Set. Supports characters of varying width format. BFILE External Binary File A binary file stored outside of the database in the host operating system file system, but accessible from database tables. BFILEs can be accessed from your application on a read-only basis. Use BFILEs to store static data, such as image data, that is not manipulated in applications. Refer to this site for more information (Oracle Documentation): https://docs.oracle.com/cd/E18283_01/appdev.112/e18294/adlob_intro.ht m
  • 7. DDL Commands – CREATE TABLE  Example on CREATE TABLE:
  • 8. DDL Commands – ALTER TABLE  The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. It is also used to rename a table or a column.  To add a column in a table, use the following syntax: ALTER TABLE table_name ADD column_name datatype
  • 9. DDL Commands – ALTER TABLE  To modify the data type of a column in a table, use the following syntax, also you can modify more than one column at once. ALTER TABLE table_name MODIFY column_name datatype ALTER TABLE table_name MODIFY (column_name1 datatype, column_name2 datatype, ……………….. column_nameN datatype); Modify single column Modify multiple columns
  • 10. DDL Commands – ALTER TABLE  To delete a column in a table, use the following syntax ALTER TABLE table_name DROP COLUMN column_name
  • 11. DDL Commands – ALTER TABLE  To rename a column or a table , use the following syntax: ALTER TABLE table_name RENAME COLUMN old_name to new_name; ALTER TABLE table_name RENAME TO new_table_name; Rename a column Rename a table
  • 12. DDL Commands – DROP TABLE  DROP TABLE statement allows you to delete a table from the SQL database. DROP TABLE table_name;
  • 13. DDL Commands – Constraints  SQL constraints are used to specify rules for the data in a table.  If there is any violation between the constraint and the data action, the action is aborted by the constraint.  Constraints can be specified when the table is created (in a CREATE TABLE statement) or after the table is created (in an ALTER TABLE statement).
  • 14. Adding / Deleting Constraints  There are two ways of adding constraints: ◦ Within table creation ◦ Using ALTER query CREATE TABLE student( ID Number(8), CPR Number(9), Name Varchar2(15), GPA Number(3,2), Major Varchar2(5) Not Null, Year INT Default 1, PRIMARY KEY (ID), UNIQUE (CPR) ); CREATE TABLE table_name ( column_name1 data_type(size) constraint_name, column_name2 data_type(size) constraint_name, column_name3 data_type(size) constraint_name, .... );
  • 15. Adding / Deleting Constraints ALTER TABLE student ADD CONSTRAINT student_id_pk PRIMARY KEY (id); ALTER TABLE student ADD CONSTRAINT student_cpr_un UNIQUE (cpr); ALTER TABLE student MODIFY major NOT NULL; ALTER TABLE student MODIFY year DEFAULT 1;
  • 16.  Using ALTER query ALTER TABLE table_name DROP CONSTRAINT constraint_name; ALTER TABLE student DROP CONSTRAINT student_id_pk; Adding / Deleting Constraints
  • 17.  Constraints can be enabled or disabled instead of dropping and adding again. Enabling / Disabling Constraints ALTER TABLE table_name DISABLE CONSTRAINT constraint_name; ALTER TABLE student DISABLE CONSTRAINT student_id_pk; ALTER TABLE table_name ENABLE CONSTRAINT constraint_name; ALTER TABLE student ENABLE CONSTRAINT student_id_pk;
  • 18. DDL Commands – Constraints- NOT NULL  The NOT NULL constraint enforces a column to NOT accept NULL values.  The NOT NULL constraint enforces a field to always contain a value. This means that you cannot insert a new record, or update a record without adding a value to this field.
  • 19. DDL Commands – Constraints- UNIQUE  The UNIQUE constraint uniquely identifies each record in a database table.  The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns.  A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.  Note that you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint.
  • 20. DDL Commands – Constraints- PRIMARY KEY  The PRIMARY KEY constraint uniquely identifies each record in a database table.  Primary keys must contain UNIQUE values.  A primary key column cannot contain NULL values.  Most tables should have a primary key, and each table can have only ONE primary key.
  • 21. DDL Commands – Constraints- FOREIGN KEY  A FOREIGN KEY in one table points to a PRIMARY KEY in another table.  The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.  The FOREIGN KEY constraint also prevents invalid data from being inserted into the foreign key column, because it has to be one of the values contained in the table it points to. DEPT table EMP table Primary Key Foreign Key
  • 22. DML Commands – INSERT INTO  The INSERT INTO statement is used to insert new records in a table. INSERT INTO table_name VALUES (value1,value2,value3,...); INSERT INTO table_name (column1,column3,column6,...) VALUES (value1,value3,value6,...);  It is also possible to only insert data in specific columns.
  • 23. DML Commands – UPDATE  The UPDATE statement is used to update existing records in a table. UPDATE table_name SET column1=value1,column2=value2,... WHERE some_column=some_value;  NOTE: The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!
  • 24. DML Commands – DELETE  The DELETE statement is used to delete rows in a table. DELETE FROM table_name WHERE some_column=some_value;  NOTE: The WHERE clause specifies which record or records that should be deleted . If you omit the WHERE clause, all records will be deleted!  It is possible to delete all rows in a table without deleting the table. DELETE FROM table_name;