English 中文(简体)
SQLite - CREATE Table
  • 时间:2024-11-03

SQLite - CREATE Table


Previous Page Next Page  

SQLite CREATE TABLE statement is used to create a new table in any of the given database. Creating a basic table involves naming the table and defining its columns and each column s data type.

Syntax

Following is the basic syntax of CREATE TABLE statement.

CREATE TABLE database_name.table_name(
   column1 datatype PRIMARY KEY(one or more columns),
   column2 datatype,
   column3 datatype,
   .....
   columnN datatype
);

CREATE TABLE is the keyword telpng the database system to create a new table. The unique name or identifier for the table follows the CREATE TABLE statement. Optionally, you can specify database_name along with table_name.

Example

Following is an example which creates a COMPANY table with ID as the primary key and NOT NULL are the constraints showing that these fields cannot be NULL while creating records in this table.

sqpte> CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
);

Let us create one more table, which we will use in our exercises in subsequent chapters.

sqpte> CREATE TABLE DEPARTMENT(
   ID INT PRIMARY KEY      NOT NULL,
   DEPT           CHAR(50) NOT NULL,
   EMP_ID         INT      NOT NULL
);

You can verify if your table has been created successfully using SQLite command .tables command, which will be used to pst down all the tables in an attached database.

sqpte>.tables
COMPANY     DEPARTMENT

Here, you can see the COMPANY table twice because its showing COMPANY table for main database and test.COMPANY table for test apas created for your testDB.db. You can get complete information about a table using the following SQLite .schema command.

sqpte>.schema COMPANY
CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
);
Advertisements