Assignment 2 (2)
Assignment 2 (2)
Assignment 2 (2)
a. Design and Develop SQLDDL statements which demonstrate the use of SQL objects suchas
Table, View, Index, Sequence, Synonym, different constraints etc.
b. Write at least 10 SQL queries on the suitable database application using SQL DMLstatements.
Aim: Design and Develop SQL DDL statements which demonstrate the use of SQL objects
suchas Table, View, Index, Sequence, Synonym. Design and Develop SQL DML statements
Objective: To learn the various DDL commands, view concepts, types of Indexes,
sequence, Synonym and its implementation.To learn the various DML commands
Theory:
DDL:
Data Definition Language helps you to define the database structure or schema. Let’s learn about
DDL commands with syntax.
Database Tables
A database most often contains one or more tables. Each table is identified by a name
(e.g. "Customers" or "Orders"). Tables contain records (rows) with data.
database. Syntax
CREATE TABLE table_name (
column1
datatype,column2
datatype,column3
datatype,
....
);
Example
CREATE TABLE Persons
( PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
View:
A view contains rows and columns, just like a real table. The fields in a view are fields from one
or more real tables in the database.
You can add SQL functions, WHERE, and JOIN statements to a view and present the data as if
the data were coming from one single table.
If you have the Northwind database you can see that it has several views installed by default.
The view "Current Product List" lists all active products (products that are not discontinued)
from the "Products" table. The view is created with the following SQL:
FROM table_name
WHERE condition;
Now we want to add the "Category" column to the "Current Product List" view. We will update
the view with the following SQL:
view_name; Index:
The SQL statement below creates an index named "idx_lastname" on the "LastName" column in
the "Persons" table:
The SQL statement below creates an index named "idx_lastname" on the "LastName" column in
the "Persons" table:
If you want to create an index on a combination of columns, you can list the column names
within the parentheses, separated by commas:
MySQL:
Sequence:
A sequence is a set of integers 1, 2, 3, ... that are generated in order on demand. Sequences are
frequently used in databases because many applications require each row in a table to contain a
unique value and sequences provide an easy way to generate them. This chapter describes how
to use sequences in MySQL.
Using AUTO_INCREMENT column:
The simplest way in MySQL to use Sequences is to define a column as AUTO_INCREMENT
and leave rest of the things to MySQL to take care.
Example:
Try out the following example. This will create table and after that it will insert few rows in this
table where it is not required to give record ID because it's auto incremented by MySQL.
mysql> CREATE TABLE insect
-> (
-> id INT UNSIGNED NOT NULL AUTO_INCREMENT,
-> PRIMARY KEY (id),
-> name VARCHAR(30) NOT NULL, # type of insect
-> date DATE NOT NULL, # date collected
-> origin VARCHAR(30) NOT NULL # where collected
);
Query OK, 0 rows affected (0.02 sec)
DML:
Data Manipulation Language (DML) allows you to modify the database instance by inserting,
modifying, and deleting its data. It is responsible for performing all types of data modification in
a database.
DML Commands
The SQL INSERT INTO Statement
2. 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.
Here, the INSERT INTO syntax would be as follows:
DELETE Syntax
DELETE FROM table_name WHERE condition;