0% found this document useful (0 votes)
102 views

The SQL Create Database Statement: Syntax

The document discusses various SQL statements used to create, alter, backup and drop databases and tables. It describes the CREATE DATABASE, DROP DATABASE, BACKUP DATABASE, CREATE TABLE, DROP TABLE, ALTER TABLE and TRUNCATE TABLE statements. It also covers SQL constraints that can be used to limit the type of data that can be inserted into tables and ensures referential integrity.

Uploaded by

Hemant Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

The SQL Create Database Statement: Syntax

The document discusses various SQL statements used to create, alter, backup and drop databases and tables. It describes the CREATE DATABASE, DROP DATABASE, BACKUP DATABASE, CREATE TABLE, DROP TABLE, ALTER TABLE and TRUNCATE TABLE statements. It also covers SQL constraints that can be used to limit the type of data that can be inserted into tables and ensures referential integrity.

Uploaded by

Hemant Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

The SQL CREATE DATABASE Statement

The CREATE DATABASE statement is used to create a new SQL database.

Syntax

CREATE DATABASE databasename;

CREATE DATABASE Example


The following SQL statement creates a database called "testDB":

Example

CREATE DATABASE testDB;

Tip: Make sure you have admin privilege before creating any database. Once a database is
created, you can check it in the list of databases with the following SQL command: SHOW
DATABASES;

The SQL DROP DATABASE Statement


The DROP DATABASE statement is used to drop an existing SQL database.

Syntax

DROP DATABASE databasename;

Note: Be careful before dropping a database. Deleting a database will result in loss of complete
information stored in the database!

DROP DATABASE Example


The following SQL statement drops the existing database "testDB":

Example

DROP DATABASE testDB;

Tip: Make sure you have admin privilege before dropping any database.

The SQL BACKUP DATABASE Statement


The BACKUP DATABASE statement is used in SQL Server to create a full back up of an existing
SQL database.

Syntax
BACKUP DATABASE databasename
TO DISK = 'filepath';

The SQL BACKUP WITH DIFFERENTIAL Statement


A differential back up only backs up the parts of the database that have changed since the last
full database backup.

Syntax

BACKUP DATABASE databasename


TO DISK = 'filepath'
WITH DIFFERENTIAL;

BACKUP DATABASE Example


The following SQL statement creates a full back up of the existing database "testDB" to the D
disk:

Example

BACKUP DATABASE testDB


TO DISK = 'D:\backups\testDB.bak';

Tip: Always back up the database to a different drive than the actual database. Then, if you get a
disk crash, you will not lose your backup file along with the database.

BACKUP WITH DIFFERENTIAL Example


The following SQL statement creates a differential back up of the database "testDB":

Example

BACKUP DATABASE testDB


TO DISK = 'D:\backups\testDB.bak'
WITH DIFFERENTIAL;

The SQL CREATE TABLE Statement


The CREATE TABLE statement is used to create a new table in a database.

Syntax

CREATE TABLE table_name (


column1 datatype,
column2 datatype,
column3 datatype,
....
);

The column parameters specify the names of the columns of the table.

The datatype parameter specifies the type of data the column can hold (e.g. varchar, integer, date,
etc.).

SQL CREATE TABLE Example


The following example creates a table called "Persons" that contains five columns: PersonID,
LastName, FirstName, Address, and City:

Example

CREATE TABLE Persons (


PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

Create Table Using Another Table


A copy of an existing table can also be created using CREATE TABLE.

The new table gets the same column definitions. All columns or specific columns can be
selected.

If you create a new table using an existing table, the new table will be filled with the existing
values from the old table.

Syntax

CREATE TABLE new_table_name AS


SELECT column1, column2,...
FROM existing_table_name
WHERE ....;

The following SQL creates a new table called "TestTables" (which is a copy of the "Customers"
table):

Example

CREATE TABLE TestTable AS


SELECT customername, contactname
FROM customers;
The SQL DROP TABLE Statement
The DROP TABLE statement is used to drop an existing table in a database.

Syntax

DROP TABLE table_name;

Note: Be careful before dropping a table. Deleting a table will result in loss of complete
information stored in the table!

SQL DROP TABLE Example


The following SQL statement drops the existing table "Shippers":

Example

DROP TABLE Shippers;

SQL TRUNCATE TABLE


The TRUNCATE TABLE statement is used to delete the data inside a table, but not the table itself.

Syntax

TRUNCATE TABLE table_name;

SQL ALTER TABLE Statement


The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.

The ALTER TABLE statement is also used to add and drop various constraints on an existing table.

ALTER TABLE - ADD Column


To add a column in a table, use the following syntax:

ALTER TABLE table_name


ADD column_name datatype;

The following SQL adds an "Email" column to the "Customers" table:

Example

ALTER TABLE Customers


ADD Email varchar(255);
ALTER TABLE - DROP COLUMN
To delete a column in a table, use the following syntax (notice that some database systems don't
allow deleting a column):

ALTER TABLE table_name


DROP COLUMN column_name;

The following SQL deletes the "Email" column from the "Customers" table:

Example

ALTER TABLE Customers


DROP COLUMN Email;

ALTER TABLE - ALTER/MODIFY COLUMN


To change the data type of a column in a table, use the following syntax:

SQL Server / MS Access:

ALTER TABLE table_name


ALTER COLUMN column_name datatype;

My SQL / Oracle (prior version 10G):

ALTER TABLE table_name


MODIFY COLUMN column_name datatype;

Oracle 10G and later:

ALTER TABLE table_name


MODIFY column_name datatype;

SQL Create Constraints


Constraints can be specified when the table is created with the CREATE TABLE statement, or after
the table is created with the ALTER TABLE statement.

Syntax

CREATE TABLE table_name (


column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);
SQL Constraints
SQL constraints are used to specify rules for the data in a table.

Constraints are used to limit the type of data that can go into a table. This ensures the accuracy
and reliability of the data in the table. If there is any violation between the constraint and the data
action, the action is aborted.

Constraints can be column level or table level. Column level constraints apply to a column, and
table level constraints apply to the whole table.

The following constraints are commonly used in SQL:

 NOT NULL - Ensures that a column cannot have a NULL value


 UNIQUE - Ensures that all values in a column are different
 PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row
in a table
 FOREIGN KEY - Prevents actions that would destroy links between tables
 CHECK - Ensures that the values in a column satisfies a specific condition
 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

You might also like