You are on page 1of 5

Any SQL command ending with semicolon is called query

Data Definition in SQL

CREATE TABLE
ALTER TABLE

DROP TABLE
TRUNCATE TABLE
RENAME TABLE

 Creating Tables
o DDL command CREATE TABLE is used to create table.
o Syntax
 CREATE TABLE tablename
(
columnname1 type(size),
columnname2 type(size),
………………
columnnameN type(size)
);
o Example

CREATE TABLE student1

(
rollno int,
name varchar(15),
age int,
city varchar(15)
);

describe student1

 Modifying the structure of the already existing table


o DDL ALTER TABLE command is used
 Add new columns
 ALTER TABLE tablename
ADD newcolumnname datatpye(size);
 Change the datatype / increase or decrease size of columns
 ALTER TABLE tablename
MODIFY oldcolumnname newdatatype(newsize)
NOTE: decrease the size only if no data present or some data not
lost
 Deleting any column
 ALTER TABLE tablename
DROP columnname;
 Changing default values
 Add constraints

 Deleting a Table
o DROP TABLE is a DDL command
o Syntax:
o DROP TABLE tablename

DROP TABLE TRUNCATE TABLE DELETE TABLE


DDL DDL DML
Delete data+structure Delete ALL data only BUT not Delete data but some or all
structure data depending upon
condition
Cannot be rollback (undo) Cannot be rollback Rollback
DROP TABLE tablename; TRUNCATE TABLE tablename DELETE TABLE tablename
WHER condition;

 Changing name of the table


o RENAME DDL command is used
o Syntax
 RENAME TABLE oldtablename TO newtablename;

DM

Data Manipulation Language

Language the used to perform operations on DATA or manipulate data.

1. Adding / Storing New Data


 DML Insert command is used to add new data.
 Syntax 1: Inserting data in all columns of the table
 INSERT INTO tablename VALUES(value1,value2,….valueN);
 One insert query will add one tuple/row
 Imp. Points
1. The values are separated by comma.
2. Character data or date and time data enclosed in single inverted
commas.
3. The number of values must match with number of columns in table.
4. The type of values must match with type of column in table.
5. The order of values must match with column order in table.
 Syntax 2: Inserting only some of the values
 One insert query add one rows
 Why?
1. Values missing
2. Value not available or not known

 FIRST WAY:
1. INSERT INTO tablename(col1,col2…) VALUES(value1,value2,
….valueN);
Here specify name of columns for which value is known
2. Imp. Points
a. The values are separated by comma.
b. Character data or date and time data enclosed in single
inverted commas.
c. The number of values must match with number of columns
specified in insert query
d. The type of values must match with type of column in insert
query.
e. The order of values must match with column order in insert
query.
 Second Way
1. Syntax 1 is used and for values that is not know we add NULL

 Syntax 3: specific to MYSQL. Insert multiple rows at one time


 INSERT INTO tablename VALUES(value1,value2,….valueN) ,
VALUES(value1,value2,….valueN),
VALUES(value1,value2,….valueN),
VALUES(value1,value2,….valueN);

 Syntax4: specific to MYSQL. Insert multiple rows at one time and use row()
 INSERT INTO tablename VALUES ROW(value1,value2,….valueN) ,
ROW(value1,value2,….valueN),
ROW(value1,value2,….valueN),
ROW(value1,value2,….valueN);

2. Modifying the existing or OLD data


 DML UPDATE command is used.
 Syntax 1: Update all rows / data
 UPDATE table
SET columnname=newvalue, columnname=newValus;
 Syntax 2: Update some rows based on condition
 UPDATE table
SET columname=newVALUE, columnname=newValus
WHERE condition;
Only rows that fulfill condition will be changed or modify
Set specify the name of column whose value is to be modified or
change

WHERE specify name of column on which condition is apply

ALTER TABLE UPDATE TABLE


DDL DML
Modify the structure of Modify the data
Table
ALTER TABLE tablename UPDATE TABLE tablename
ADD columnname dt(size); SET columnname=value
Change in the column Change in the column data
definition

3. Deleting existing , outdated or OLD data


 DML DELETE command is used.
 Syntax 1: delete all rows / data
 DELETE FROM tablename;
 Syntax 2: delete some of rows based on condition
 DELETE FROM tablename

WHERE condition;

4. Retrieval of Data
 Based on relational algebra and calculus
 SELECT DML command is used
Constraints in ORACLE

 Rules , restrictions that we can apply to make sure data is always correct, valid and
accurate.
 It is also known as constraints.
 We can apply constraints using DDL
 CREATE TABLE
 ALTER TABLE
 Types of Constraints (total 6)
 NOT NULL constraints
 DEFAULT constraints

NOT NULL Constraints

 States that each column must have atleast one value (can be valid or invalid)
 It is compulsory to add a value in column.

DEFAULT constraint

 States that if value is missing or not available or not known inlace of NULL insert
some valid value known as default value.

You might also like