0% found this document useful (0 votes)
20 views31 pages

UNIT-2 Ty semII

Second unit of rdbms

Uploaded by

khanjunaid7796
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views31 pages

UNIT-2 Ty semII

Second unit of rdbms

Uploaded by

khanjunaid7796
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

UNIT-2

DATABASE AND TABLE


OPERATIONS
2.1Database Operations -1.Creating a Database
2.Dropping the Database
 Before doing anything else with the data, you need to create a
database. A database is a container of data. It stores contacts,
vendors, customers or any kind of data that you can think of.
 a database is a collection of objects that are used to store and
manipulate data such as tables, database views, triggers,
and stored procedures.
 To create a database you use the CREATE
DATABASE statement as follows:
 CREATE DATABASE database_name;

 Followed by the CREATE DATABASE statement is database


name that you want to create. It is recommended that the
database name should be as meaningful and descriptive as
possible
Eg.--CREATE DATABASE employee;
 In PostgreSQL, we can create a database in two ways:
 PostgreSQL Create Database using pgAdmin
 PSQL Create Database Command Line (SQL Shell)
 Creating Database using pgAdmin
 To create a database in pgAdmin, we are going to
follow the below steps:
 Step 1:--Firstly, we will open the pgAdmin in our local
system and then in the Object tree, we will right-click
on the Databases and select Create then
select database
 Databases → Create → Database
Step 2:-After that, the create database window will open where
we need to provide some necessary details (Database name,
Comment) for creating a database and then click on the Save
Step 3
The database is created and display
in Object tree as we can see in the
below screenshot:
 Step 4:-And the right-hand side window will give us the
SQL which is used to create the Database as we can
see in the below image:
 Create Database Command Line (SQL Shell)
 Step1:-Firstly, we will open the SQL shell in our local
system. For this, we will go to the home button and
search for pSQL and open it.
 Step2:-Once the SQL shell is opened, we will press the
enter key for the 4-5 times and then provide
the password for the user (which we created earlier) to
connect the database as shown in the below screenshot:


 Step 3:-After that, enter the below command to create a
database
 CREATE DATABASE Javatpoint;

Step 4:-To get a list of all databases created earlier, we will


enter the below command:
\l
Step 5:-To connect to a database, we will enter the below
command:
\c javatpoint
2.Dropping the Database
 The Drop/delete command is used to eternally delete all the
file entries and data directory from the PostgreSQL platform.
Therefore, we have to use this command very carefully.
 In PostgreSQL, we can drop the database in two ways:
 Drop Database PgAdmin
 Drop Database Using SQL Shell
 Step 1:-Firstly, we will open the pgAdmin in our local system.
 Select the database (Javatpoint) by a left-click on it.
 Then right-click on the Javatpoint
 After that, click on Delete/dropoption from the given drop-
down list to delete the database.

Step 2:-Once we click on
the Delete/Drop option, one
confirmation pop-up will appear
on the screen, where we click on
the Yes button to drop the
database.
 Drop Database Using SQL Shell (Command Line)
 DROP DATABASE [ IF EXISTS] name;
 Where the command contains the following
parameters:
 IF EXISTS:--It is an optional parameter; where the
warning is displayed in the place of an error if the
database does not exist.
 Name:--Here, we will reference the database name
that we want to drop it.
 Now, we are going to use this command in the
command line:
 Step 1:-Open the SQL shell and type the below
command to see the existing database.
 \l
 Step 2:-For dropping the database, we will enter the below command:
 Drop database Javatpoint;
 While using this above command, we may encounter this below error:
 ERROR: database "Javatpoint" is being accessed by
other users Detail: There is 1 other session using the database.
 To delete the Javatpoint database, we need to follow the below process:
 First, we have to Revoke the connection with the help of below command:
 REVOKE CONNECT ON DATABASE Javatpoint from public;
 And then press the Enter key.
 Once the connection is Revoked, we will enter the following query:
 SELECT pg_terminate_backend(pg_stat_activity.pid)
 FROM pg_stat_activity
 WHERE pg_stat_activity.datname = 'Javatpoint';
Then enter the drop database query and use the \l command to verify
whether the database is deleted or not as we can see in the below
screenshot:
2.2Table Operations – 1. Create 2. Alter3.
Drop
1.Create Table:-In PostgreSQL, the Create table command is
used to create a new table in any of the given databases.
 In this section, we are going to learn how we can create a
table in PostgreSQL.
 Syntax of creating table in PostgreSQL

 CREATE TABLE table_name(

 column1 datatype,
 column2 datatype,
 column3 datatype,
 .....
 columnN datatype,
 PRIMARY KEY(one or more columns )
 );
 In PostgreSQL, we can create a table in two ways:
 PostgreSQL Create Table using pgAdmin
 PostgreSQL Create Table using SQL Shell
 PostgreSQL Create Table using pgAdmin
 We are going to follow the below process to create a
table in pgAdmin:
 Step1:-Firstly, we will open the latest
version pgAdmin in our local system, and we will go to
the object tree and select the database, in which we
want to create a table.
 Step2:-After that, left-click on the
selected database(javatpoint), and then we can see
the Catalogs and Schemas.
Step3:-Then we will right-click on
the Public under Schema section,
select Create option from the given drop-
down, and click on the Table from the
given list.
 Step4:--Once we click on the Table, the Create-
table window will appear on the screen where we will
enter all the necessary details like Table name. In our
case, we will create a table called Employee.
 Step5:--After that, we will move to the Column tab in
the same window then click on the + sign to add
columns in a particular table.
 And we can select the Data types from the given
drop-down list as well as we can change the
columns Not-null preference and also set the Primary
key.
 And then click on Save to complete the process of
creating a table as we can see in the below
screenshot:
 And we can see that the Employee table is created
under the Table section.

 PostgreSQL Create Table using psql:
 Step1:-Firstly, we will open the psql in our local system,
and we will connect to the database where we want to
create a table.
 We will create a table in the javatpoint database, which
we created earlier in the PostgreSQL tutorial.
 Step2:-For connecting a database, we will enter the
command:-----\c javatpoint
 Step3:--Now, we will enter the below command to
create a table in the javatpoint database.
 create table Student(Stu_id int, Stu_Name text,
Stu_Age int, Stu_address char(30));
 As we can see in the below screenshot that the table is
created in javatpoint database:
 Step4:-We can use the below command to check the
tables (relations) in a particular database.

 2.Alter Table:-We use PostgreSQL alter table command to
change the current table structure.
 The syntax of the alter table is given below:
 ALTER TABLE table_name action;
 The below table will show the following ALTER TABLE commands
modifications:
Description Commands
We will use the ALTER TABLE ADD ALTER TABLE table_name ADD
COLUMN to add a new column to a COLUMN new_column_name TYPE;
table.
We will use the ALTER TABLE DROP ALTER TABLE table_name DROP
COLUMN command for deleting an COLUMN column_name;
existing column.
For modifying the column's default ALTER TABLE table_name ALTER
value, we can use the ALTER TABLE COLUMN column_name [SET DEFAULT
ALTER COLUMN SET DEFAULT or value | DROP DEFAULT];
DROP DEFAULT command.
Description Commands

We will use ALTER TABLE ADD ALTER TABLE table_name ADD


CONSTRAINT command for CONSTRAINT constraint_name
adding a constraint. constraint_definition;

We will use the alter table ALTER TABLE table_name RENAME


rename column to command COLUMN column_name TO
for renaming a remaining new_column_name;
column.

For renaming a table, we will use ALTER


the ALTER TABLE RENAME TABLE table_name RENAME
TO command. TO new_table_name;

For adding the CHECK ALTER TABLE table_name ADD


constraint, we will use the ALTER CHECK expression;
TABLE, ADD CHECK command.

To change the NOT NULL ALTER TABLE table_name ALTER


constraint, we will then COLUMN column_name [SET NOT
use ALTER TABLE ALTER NULL| DROP NOT NULL];
COLUMN command.
 For example Add a column:--
 After that we will add a new column
named Latitude with the help of below command:
 ALTER TABLE Station
 ADD COLUMN Latitude REAL;
 Output:--Once we execute the above command, we
will get the below message window:
 To Drop a column:-To delete the Latitude column from
the Station table, we will use the below command:
 ALTER TABLE Station
 DROP COLUMN Latitude;
 Output:-We will get the below message after
executing the above statement:

 3.Drop table:-In PostgreSQL, we can use the Drop
table command to delete the existing table or which
we don't need anymore. This command deletes the
complete data of a table along with the whole
structure or definition permanently from the database.
 The syntax for Dropping a table
 DROP TABLE table_name;
 Here the table name parameter is used to define the
name of the table, which we will delete from the
database. And the drop table is the keyword which is
used to drop a table.
 In PostgreSQL, we can drop the table in two ways:
 Dropping a table from psql
 Drop table in pgAdmin
 Dropping a table from psql
 To drop/delete or remove in psql, we are going to
follow the below steps:
 Step1:--We had created the table Student and
Department in the previous section of the tutorial. First,
we will confirm these tables with the help of below
command:
 Javatpoint=# \d
 After using the above command, the following output
will occur:
 Step2:--This means that the Student and
Department tables are available in the selected
database. Thus, we will let drop them with the help of
below command:
 javatpoint=# drop table student, department;

Step3:--And if we again check for the list of relations:


javatpoint=# \d
relations found.
javatpoint=#
The above message specifies that drop command is
executed successfully.

You might also like