In today’s article, we will talk about What is a Table in PostgreSQL and How to Create it.
We opened our database to collect our data in one place, but we do not know in which data type and which names they will be kept, so we will learn the concept of tables.
Creating a table is one of the most important factors of the database.
When creating a table, it is of great importance that what data we will draw from where, with which table we will connect it, and in which data type we will create it.
If you make the table design wrong, problems may arise in the future and you may have an extra workload.
Therefore, great care must be taken while creating/designing the table.
While creating the table, we need to create it in the following format according to the need.
1 2 3 4 5 |
CREATE TABLE table_name( column_name data type and column constraints, Column_Name data type Table constraints ) INHERITS |
column, table constraint and INHERITS are optional.
Things to consider when creating a table:
The table name is written after the CREATE TABLE clause and parentheses are opened to indicate operations related to the columns.
The column name and the appropriate data type for the column are written between the parentheses.
If more than one column is to be added, a comma is placed between the columns.
If the column will be a constraint, we need to write the constraints after the data type. (ex: restrictions such as if no data is added to the column)
If you want to create table-based restrictions while creating the table, you need to write the table restrictions.
If you want to inherits from another table, that is, copy the table from another table or view, etc., you need to write the relevant object after this parameter.
After each operation, it must be added.
When creating a table, you must create it with lowercase letters.
Let’s make an example by adhering to the features that need attention.
1 2 3 4 5 6 7 8 9 |
CREATE TABLE staff ( id integer not null, Name varchar(50), Surname varchar(50), department varchar(60), job_start_date timestamp, salary money ) |
If you want to create a table according to the select query and automatically transfer the data into it, you can do it like the plgpsql sentence below.
1 |
create table viewtable as select*from vw_a_olan_aktörler |