CREATE

CREATE is an SQL command used to create a database or a table. Specifically, this command constructs new tables, databases, views, or index in SQL. Different database systems use several forms of CREATE like CREATE DATABASE, CREATE TABLE, CREATE INDEX, CREATE VIEW, etc. for distinct purposes. By invoking the right parameters and inputs, developers can use CREATE command to effectively structure their database system.

Example

CREATE DATABASE IF NOT EXISTS testDB;

Output

Query OK, 1 row affected (0.01 sec)

Explanation

This SQL command creates a new database named testDB, if there isn’t already a database with that name existing. The IF NOT EXISTS clause ensures no error occurs if the testDB already exists. The output Query OK, 1 row affected (0.01 sec) indicates that the execution of the command was successful.

Example

CREATE TABLE employees (
ID INT PRIMARY KEY,
name VARCHAR (100),
birth_year INT,
job_title VARCHAR (100)
);

Output

CREATE TABLE

Explanation

In this example, the CREATE TABLE statement creates a new table named ‘employees’ in the database. The new table has four columns: ID, name, birth_year, and job_title. The ID column is a key primary and it does not accept NULL values. The name and job_title columns are of variable-length character type, specified by VARCHAR (100), and the birth_year is of integer type. The output message CREATE TABLE confirms the successful creation of the table.

Example

CREATE DATABASE TestDatabase;

Output

Command(s) completed successfully.

Explanation

The SQL statement creates a new database in SQL Server. The database is named TestDatabase. The output message validates that the command has been executed and the database has been created successfully.

Example

CREATE TABLE employees (
employee_id NUMBER(6),
first_name VARCHAR2(20),
last_name VARCHAR2(25),
email VARCHAR2(25),
phone_number VARCHAR2(20),
hire_date DATE,
job_id VARCHAR2(10),
salary NUMBER(8, 2),
manager_id NUMBER(6),
department_id NUMBER(4)
);

Output

Table EMPLOYEES created.

Explanation

The CREATE TABLE statement defined a new table called employees in the database. The table has the columns employee_id, first_name, last_name, email, phone_number, hire_date, job_id, salary, manager_id, and department_id with the specified data types and sizes. After executing the CREATE TABLE statement, the system returns a message indicating that the table was successfully created.

Example

CREATE TABLE employees (
id INTEGER PRIMARY KEY,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE
);

Output

Query OK, 0 rows affected (0.01 sec)

Explanation

The above SQL code creates a new table named employees in the database. The table has four columns: id, first_name, last_name, and email. The id is an INTEGER type and is the primary key of the table which uniquely identifies every row. The first_name, last_name, and email columns are of TEXT type. The NOT NULL constraint makes sure that these columns cannot have NULL values. The UNIQUE constraint on the email column ensures that all entries in this column are unique.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.