The CREATE TABLE Statement: Mis - I - SQL Work Sheet - 1
The CREATE TABLE Statement: Mis - I - SQL Work Sheet - 1
SQL is followed by a unique set of rules and guidelines called Syntax. All the SQL statements
start with any of the keywords like SELECT, INSERT, UPDATE, DELETE, ALTER, DROP, CREATE.
The most important point to be noted here is that SQL is case insensitive, which means SELECT
and select have same meaning in SQL statements.
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. char() varchar(),
number, date, etc.).
Exercise : 1
............................................................................................................................................................................................................................
Syntax
DROP TABLE table_name;
Note : You can try it out later if you want to delete the table from the database.
............................................................................................................................................................................................................................
The INSERT INTO Statement
The INSERT INTO statement is used to insert new records in a table.
Syntax
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
If you are adding values for all the columns of the table , make sure the order of the values is in
the same order as the columns in the table.
Exercise : 2
............................................................................................................................................................................................................................
Syntax
SELECT column1, column2, ...
FROM table_name;
Here, column1, column2, ... are the field names of the table you want to select data from. If you
want to select all the fields available in the table, use the following syntax:
Syntax
SELECT * FROM table_name;
Exercise : 3
SELECT *
FROM STUDENT;
Inside a table, a column often contains many duplicate values; and sometimes you only want to
list the different (distinct) values.
Syntax
SELECT DISTINCT column1, column2, ...
FROM table_name;
Exercise : 4
............................................................................................................................................................................................................................
Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Note: Be careful when updating records in a table! Notice the WHERE clause in the UPDATE
statement. The WHERE clause specifies which record(s) that should be updated. If you omit the
WHERE clause, all records in the table will be updated
Exercise : 5
UPDATE STUDENT
SET AGE =31
WHERE ID=001;
............................................................................................................................................................................................................................
Syntax
DELETE FROM table_name WHERE condition;
Note: Be careful when deleting records in a table! Notice the WHERE clause in the DELETE
statement. The WHERE clause specifies which record(s) should be deleted. If you omit the
WHERE clause, all records in the table will be deleted!
Exercise : 6
Syntax
DELETE FROM table_name;
Note : You can try it out later if you want to delete the data inside the table.
............................................................................................................................................................................................................................
The WHERE clause is used to extract only those records that fulfill a specified condition.
Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Note: The WHERE clause is not only used in SELECT statement, it is also used in UPDATE,
DELETE statement, etc.!
Operator Description
= Equal
<> Not equal.
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between a certain range
IN To specify multiple
possible values for a
column
Exercise : 7
SELECT *
FROM STUDENT
WHERE NAME=’SABASTIN’;
SELECT *
FROM STUDENT
WHERE NAME <>’SABASTIN’;
SELECT *
FROM STUDENT
WHERE AGE > 20;
SELECT *
FROM STUDENT
WHERE AGE < 30;
SELECT *
FROM STUDENT
WHERE AGE >= 20;
SELECT *
FROM STUDENT
WHERE AGE <= 30;
SELECT *
FROM STUDENT
WHERE ID BETWEEN 001 AND 005;
SELECT *
FROM STUDENT
WHERE NAME IN (‘Sabastin’, ‘Shirl’);
............................................................................................................................................................................................................................
The ALTER TABLE statement is also used to add and drop various constraints on an existing
table.
Syntax
ALTER TABLE table_name
ADD column_name datatype;
Exercise : 8
Syntax
ALTER TABLE table_name
DROP COLUMN column_name;
Exercise : 8
Note :
To comment use –
............................................................................................................................................................................................................................