Professional Documents
Culture Documents
3 A
AIM:
Create a continent database by your name as Firstname_Lastname and execute
various DDL, DML and DCL SQL queries.
THEORY:
DDL COMMANDS
1. CREATE
CREATE command is used to create a table, schema or an index.
Syntax:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
....);
Example:
CREATE TABLE CUSTOMERS(
InsuranceID INT,
Name VARCHAR(50),
DOB DATE,
NIN INT,
Location VARCHAR(255)
);
2. ALTER
ALTER command is used to add, modify or delete columns or constraints from the
database table.
Syntax
ALTER TABLE Table_name ADD column_name datatype;
Example
ALTER TABLE CUSTOMERS ADD email_id VARCHAR(50);
3. TRUNCATE:
TRUNCATE command is used to delete the data present in the table but this will
not delete the table.
Syntax
TRUNCATE table table_name;
Example
TRUNCATE table CUSTOMERS;
4. DROP
DROP command is used to delete the table along with its data.
Syntax
DROP TABLE table_name;
Example
DROP TABLE CUSTOMERS;
5. RENAME
RENAME command is used to rename the table name.
Syntax
ALTER TABLE table_name1 RENAME to new_table_name1;
Example
ALTER TABLE CUSTOMERS RENAME to CUSTOMERINFO;
DML Commands
1. INSERT
INSERT Statement is used to insert new records into the database table.
Syntax
INSERT INTO table_name (column1, column2, column3, ...) VALUES
(value1, value2, value3, ...);
Note: Column names are optional.
Example
Both the below ways are correct.
Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example
UPDATE CUSTOMERS SET email_id = 'mango.lo@xyz.com' WHERE
InsuranceID='123';
4. DELETE
DELETE statement is used to delete the existing records present in the database
table.
Syntax
DELETE FROM table_name where condition;
Example
DELETE FROM CUSTOMERS where InsuranceID='123';
DCL Commands
1. GRANT
GRANT statement is used to provide access privileges to users to access the
database.
Syntax
GRANT privileges ON object TO user;
Note: Privileges can be SELECT, INSERT, UPDATE, DELETE, TRUNCATE,
REFERENCES, TRIGGER, CREATE, ALL. You can also specify combination of these
privileges in a statement.
Syntax:
REVOKE privileges ON object FROM user;
Example
REVOKE DELETE, UPDATE ON ORDERS FROM customer1;
OUTPUT:
"id" "name"
1 "Australia"
2 "Asia"
3 "Europe"
4 "North America"
5 "South America"
6 "Antarctica"
7 "Africa"
OUTPUT:
"id" "name" "population" "continent_id"
1 "India" 10000000 2
2 "Brazil" 30000 5
3 "China" 1000000 2
4 "USA" 80000 4
5 "Australia" 120000 1
6 "Germany" 230000 3
7 "Dhruvil Land" 0 6
OUTPUT:
"id" "name" “country_id" "population"
1 "Maharashtra" 1 60000
2 "Sao paulo" 2 15000
3 "Rio de Janeiro" 2 15000
4 "Henan" 3 70000
5 "Texas" 4 66094
6 "Victoria" 5 2038
7 "Bavaria" 6 40000
8 "La La land" 7 0
JOINS:
select country.name, country.population, continents.name
from continents inner join country
on continents.id = country.continent_id;
OUTPUT:
"name" "population" "continent name"
"India" 10000000 "Asia"
"Brazil" 30000 "South America"
"China" 1000000 "Asia"
"USA" 80000 "North America"
"Australia" 120000 "Australia"
"Germany" 230000 "Europe"
"Dhruvil Land" 0 "Antarctica"
CONCLUSION:
We learned how to perform various DML, DDL and DCL queries on country,
continents and states table in PostgreSQL.