You are on page 1of 7

SGD Assignment No.

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.

INSERT INTO CUSTOMERS (InsuranceID, Name, DOB, NIN,


Location,email_id) VALUES ('123', 'Mango','2000-01-
01','56789','LO','Mango@xyz.com');
INSERT INTO CUSTOMERS VALUES ('123', 'Mango','2000-01-
01','56789','LO','Mango@xyz.com');
2. SELECT
Select statement is used to select data from database tables.
Syntax:
SELECT column1, column2, ...
FROM table_name;
Example
SELECT * FROM CUSTOMERS;
3. UPDATE
UPDATE statement is used to modify the existing values of records present in the
database table.

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.

GRANT Connect to Database


GRANT CONNECT ON DATABASE database_name TO username;

GRANT Usage on Schema


GRANT USAGE ON SCHEMA database_name TO username;

Grant access to all tables in the database


GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA schema_name TO
username;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA schema_name TO
username;
GRANT ALL PRIVILEGES ON DATABASE database_name TO username;
Grant permission to create database
ALTER USER username CREATEDB;

Grant superuser access to a user


ALTER USER myuser WITH SUPERUSER;
2. REVOKE
REVOKE statement is used to withdraw the access priviliges given to a user by
GRANT statement.

Syntax:
REVOKE privileges ON object FROM user;
Example
REVOKE DELETE, UPDATE ON ORDERS FROM customer1;

CODE FOR CREATING GIVEN TABLES:


insert into continents(name)
values ('Australia'), ('Asia'), ('Europe'),('North
America'),('South America'),('Antarctica'),('Africa');

SELECT * FROM public.continents


ORDER BY id ASC

OUTPUT:
"id" "name"
1 "Australia"
2 "Asia"
3 "Europe"
4 "North America"
5 "South America"
6 "Antarctica"
7 "Africa"

insert into country(name, population, continent_id) values


('India', 10000000, 2),
('Brazil', 30000, 5),
('China', 1000000, 2),
('USA', 80000, 4),
('Australia', 120000, 1),
('Germany', 230000, 3),
('Dhruvil Land', 0, 6);

SELECT * FROM public.country


ORDER BY id ASC

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

insert into states(name, country_id, population) values


('Maharashtra', 1, 60000),
('Sao paulo', 2, 15000),
('Rio de Janeiro', 2, 15000),
('Henan',3, 70000),
('Texas',4, 66094),
('Victoria', 5, 2038),
('Bavaria', 6, 40000),
('La La land',7, 0);
SELECT * FROM public.states
ORDER BY id ASC

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.

You might also like