UNIT-2 Ty semII
UNIT-2 Ty semII
Step 3:-After that, enter the below command to create a
database
CREATE DATABASE Javatpoint;
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