Table of Contents
Introduction
Tables are the fundamental structure in any relational database. They store data in rows and columns, allowing us to organize and query our data efficiently. Creating a table involves specifying the table name, defining the columns, and setting any constraints or data types.
Steps to Create a Table
To create a table in SQL CLI, follow these steps:
- Open your CLI and connect to the desired database.
- Use the
CREATE TABLE
statement to define the table’s structure. - Specify the table name and the columns within parentheses.
- For each column, provide a name, data type, and any additional constraints.
- Execute the SQL statement to create the table.
Example
Let’s say we want to create a table called “Employees” with the following columns:
id
(integer)name
(text)age
(integer)department
(text)
Here’s an example SQL command to create this table:
CREATE TABLE Employees (
id INT PRIMARY KEY,
name TEXT NOT NULL,
age INT,
department TEXT
);
In this example, we specify the primary key constraint on the id
column, make the name
column not nullable with the NOT NULL
constraint, and leave the age
and department
columns nullable.
Conclusion
Creating a table in SQL CLI is a straightforward process that involves defining the table’s structure with columns, data types, and constraints. By following the steps outlined in this blog post and using the appropriate SQL syntax, you can easily create tables to store and organize your data.
#SQL #Database