SQLite – 4 – Creating Tables

Creating tables in SQLite is a fundamental step in designing a relational database. Tables serve as the primary containers for storing structured data in an SQLite database. In this discussion, we will explore the process of creating tables in SQLite, including the SQL command used for this purpose, and provide practical examples to illustrate the concepts.

SQL Command for Creating Tables:

To create a table in SQLite, you use the CREATE TABLE statement. This statement defines the table’s structure, specifying the names of its columns, the data types for each column, and any constraints that enforce data integrity. The basic syntax for creating a table in SQLite is as follows:

CREATE TABLE table_name ( column1_name datatype constraints, column2_name datatype constraints, ... );

Let’s break down this syntax:

  • CREATE TABLE: This keyword is used to indicate that you are creating a new table.
  • table_name: Replace this with the desired name for your table.
  • ( ... ): Inside the parentheses, you define the table’s columns and their attributes.
  • column_name: Specify the name of each column.
  • datatype: Define the data type for each column, such as INTEGER, TEXT, REAL, etc.
  • constraints: You can apply constraints to columns, like PRIMARY KEY, UNIQUE, NOT NULL, DEFAULT, etc.

Example of Creating a Table:

Suppose we want to create a table called Employees to store information about employees in a company. The table should have the following columns: EmployeeID, FirstName, LastName, Email, Salary, and JoiningDate. Here’s an example of how we can create this table:

CREATE TABLE Employees ( EmployeeID INTEGER PRIMARY KEY, FirstName TEXT NOT NULL, LastName TEXT NOT NULL, Email TEXT UNIQUE, Salary REAL DEFAULT 0.0, JoiningDate DATE );

In this example:

  • We use CREATE TABLE to initiate the table creation process with the name Employees.
  • Inside the parentheses, we define the columns and their attributes.
  • EmployeeID is defined as an INTEGER and marked as the primary key, ensuring it contains unique values.
  • FirstName and LastName are defined as TEXT and marked as NOT NULL, ensuring that these fields cannot be left empty.
  • Email is defined as TEXT with a UNIQUE constraint, ensuring each email address is unique across records.
  • Salary is defined as REAL with a default value of 0.0, which means if no value is provided during insertion, it will default to 0.0.
  • JoiningDate is defined as DATE to store date values.

Once this CREATE TABLE statement is executed, the Employees table is created with the specified structure, and it is ready to store data.

Adding Data to the Table:

After creating a table, you can insert data into it using the INSERT INTO statement. For example:

INSERT INTO Employees (FirstName, LastName, Email, Salary, JoiningDate) VALUES ('John', 'Doe', 'johndoe@example.com', 55000.00, '2023-01-15');

This SQL statement inserts a new employee record into the Employees table, providing values for each column.

Modifying Tables:

You can also modify the structure of existing tables using the ALTER TABLE statement. For example, to add a new column called Department to the Employees table, you can use the following command:

ALTER TABLE Employees ADD COLUMN Department TEXT;

This statement adds a new column named Department with a data type of TEXT to the Employees table.

Conclusion:

Creating tables in SQLite is a fundamental step in designing a database to store structured data. The CREATE TABLE statement allows you to define the structure of your tables, including column names, data types, and constraints. Once a table is created, you can insert, update, or delete data as needed. Additionally, you can modify existing tables using the ALTER TABLE statement to add, modify, or remove columns. Understanding how to create and manage tables is essential for effective database design and data management in SQLite.