Page 259 - Information_Practice_Fliipbook_Class11
P. 259
9.5 SQL Statements and Functions
This section will discuss several SQL statements and functions for defining and manipulating data. Each SQL statement
terminates with a semicolon.
9.5.1 CREATE DATABASE statement
Creating a database is the first step in database management. CREATE DATABASE statement creates a database. The
syntax for the CREATE DATABASE statement is as follows:
CREATE DATABASE database_name;
In the above description of CREATE DATABASE statement, CREATE is an SQL keyword and database_name is the
name of the database to be specified by the user.
While describing the SQL statements, we shall write the SQL keywords in courier font uppercase letters in boldface.
We will write the user-defined names and user inputs in courier font but not boldface.
Now, we are ready to create the COMPANY database using the CREATE DATABASE statement:
CREATE DATABASE COMPANY;
To display the list of databases that have been created so far, we use the SHOW DATABASES statement:
SHOW DATABASES;
SHOW DATABASES: Displays names of all the existing databases.
Before we perform any operations on a database, it has to be opened for use. SQL statement USE <database_name>
serves this purpose:
USE database_name;
USE database_name: Makes a database active for use.
To select the database COMPANY for creating and manipulating tables in the COMPANY database, we execute the
statement:
USE COMPANY;
9.5.2 CREATE TABLE statement
Next, let us create tables corresponding to different entities (EMPLOYEE, DEPARTMENT, PROJECT, and WORKS_ON).
CREATE TABLE statement creates tables (relations). The syntax for the CREATE TABLE statement is:
CREATE TABLE table_name(
attribute1 data_type [constraint]
[, attribute2 data_type [constraint]]
⋮
[, attributeN data_type [constraint]]
table_constraints]
);
In the description of an SQL statement, a pair of square brackets ([]) denotes optional parts. Thus, a CREATE TABLE
statement may include any number of attributes or constraints. Note that the attributes and constraints are specified
within parenthesis separated by commas.
Database Concepts and the Structured Query Language 245

