Table operations¶
Beyond the basic CRUD (create, read, update, delete) operations, there are a number of other operations that can be performed on tables.
Creating a table¶
To create a new table in a database, we use the CREATE TABLE
statement. The syntax is as follows:
- Numeric data types:
INT
- IntegerFLOAT
- Floating point numberDECIMAL
- Fixed precision numberBOOLEAN
- Boolean value (True or False)
- Character data types:
CHAR(n)
- Fixed length character stringVARCHAR(n)
- Variable length character string
We can also specify constraints on the columns, the most important being the primary key (see the next section for more details on constraints).
In the example, we have specified that the id
column is the primary key. We have also specified that
the name
column cannot contain NULL values, and that the price
column has a default value of 0.
Deleting a table¶
Similarly, to delete a table from a database, we use the DROP TABLE
statement. The syntax is as follows:
Altering a table¶
There are a number of operations that can be performed on a table after it has been created. These
operations are performed using the ALTER TABLE
statement.
- Add a new column to the table:
-
Drop column
c
from the table -
Rename a table from
t1
tot2
-
Rename column c1 to c2
-
Remove all data from a table (but keep the table structure):
Table constraints¶
Table constraints are rules that are enforced on columns of a table. These are used to limit the type of data that can go into the table. The following constraints are commonly used in SQL:
NOT NULL
- Ensures that a column cannot have aNULL
valueUNIQUE
- Ensures that all values in a column are different-
PRIMARY KEY
- A combination ofNOT NULL
plusUNIQUE
. Uniquely identifies each row in a table.Note
Each table can have only one primary key, and although it is not mandatory, it is a very good practice to have a primary key in every table. Columns that can potentially be primary keys are known as candidate keys; we can choose any candidate key to be the primary key.
Note
Primary keys can also be "composite", i.e., a combination of columns that uniquely identify each row.
-
FOREIGN KEY
- Foreign keys are used to link two tables together. A foreign key in one table points to a primary key in another table.Note
For example, if we have a table called
Orders
that contains information about orders made by customers, we can create a foreign key in theOrders
table that points to theCustomerID
column in theCustomers
table. This way, we can easily find the customer who made each order. -
DEFAULT
- Sets a default value for a column if no value is specified CREATE INDEX
- Used to create and retrieve data from the database very quickly
Note
Table indexes are used to speed up the retrieval of data from a table. Usually, the data in a table is stored in an unordered manner. When we create an index, the database stores the data in a sorted manner, which makes it much faster to retrieve data from the table. However, this comes at the cost of slower insertions, updates and deletions, since the database has to maintain the sorted order of the data. Hence, we should only create indexes on columns that we frequently use to retrieve data from the table.
To add a constraint on a table, we also use the ALTER TABLE
statement. The syntax is as follows:
Example queries¶
- To add a primary key constraint on the
id
column of thet
table, we can use the following statement: - To add a foreign key constraint on the
customer_id
column of theorders
table, we can use the following statement: - To add a default value of 0 to the
price
column of thet
table, we can use the following statement: - To add a
NOT NULL
constraint on thename
column of thet
table, we can use the following statement: - To add a
UNIQUE
constraint on thename
column of thet
table, we can use the following statement: - To add an index on the
name
column of thet
table, we can use the following statement:
To drop a constraint we use the following syntax:
For example, to drop theNOT NULL
constraint on the name
column of the t
table, we can use the following statement: