0% found this document useful (0 votes)
86 views4 pages

Lab 2 - CLC

The document discusses data manipulation in a database system. It covers modifying table structures using ALTER TABLE statements to add, drop and modify columns. It also discusses creating constraints using CREATE TABLE and ALTER TABLE statements to restrict the type of data that can be inserted. The INSERT, UPDATE and DELETE statements are covered to insert new records, modify existing records and delete records from tables. Examples of queries to list movie titles, actor/actress names, films by gender are provided.

Uploaded by

Quân Trần
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views4 pages

Lab 2 - CLC

The document discusses data manipulation in a database system. It covers modifying table structures using ALTER TABLE statements to add, drop and modify columns. It also discusses creating constraints using CREATE TABLE and ALTER TABLE statements to restrict the type of data that can be inserted. The INSERT, UPDATE and DELETE statements are covered to insert new records, modify existing records and delete records from tables. Examples of queries to list movie titles, actor/actress names, films by gender are provided.

Uploaded by

Quân Trần
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

TON DUC THANG UNIVERSITY Database System

FACULTY OF INFORMATION TECHNOLOGY Semester II

LAB2: DATA MANIPULATION LANGUAGE

I. MODIFY TABLE STRUCTURE

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;

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;

ALTER TABLE - ALTER/MODIFY COLUMN

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

ALTER TABLE table_name


ALTER COLUMN column_name datatype;

II. 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.

1
Huynh Ngoc Tu
TON DUC THANG UNIVERSITY Database System
FACULTY OF INFORMATION TECHNOLOGY Semester II

Syntax
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);

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 - Uniquely identifies a row/record in another table
• CHECK - Ensures that all values in a column satisfies a specific condition
• DEFAULT - Sets a default value for a column when no value is specified
• INDEX - Used to create and retrieve data from the database very quickly

III. DATA OPERATION


1. The SQL INSERT INTO Statement

The INSERT INTO statement is used to insert new records in a table.

Syntax
It is possible to write the INSERT INTO statement in two ways.

The first way specifies both the column names and the values to be inserted:

2
Huynh Ngoc Tu
TON DUC THANG UNIVERSITY Database System
FACULTY OF INFORMATION TECHNOLOGY Semester II

INSERT INTO table_name (column1, column2, column3, ...)


VALUES (value1, value2, value3, ...);

If you are adding values for all the columns of the table, you do not need to
specify the column names in the SQL query. However, make sure the order of the
values is in the same order as the columns in the table. The INSERT INTO syntax
would be as follows:

INSERT INTO table_name


VALUES (value1, value2, value3, ...);

2. The SQL UPDATE Statement

The UPDATE statement is used to modify the existing records in a table.

Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition; The DELETE statement is used to delete existing records in a
table.

3. SQL DELETE Statement


The DELETE statement is used to delete existing records in a table.

Syntax
DELETE FROM table_name
WHERE condition;

3
Huynh Ngoc Tu
TON DUC THANG UNIVERSITY Database System
FACULTY OF INFORMATION TECHNOLOGY Semester II

PRACTICE EXERCISE

Please create the MovieDB database and input its data according to the above
tables.

Exercise 2: Express the following queries in SQL:

1. List the titles of all films


2. List the names of all female actresses.
3. Which actors/actresses starred in ‘Bourne Identity, The’? List their names.
4. List the titles of all films with female roles.
5. List the titles of all films with no female roles.

4
Huynh Ngoc Tu

You might also like