Professional Documents
Culture Documents
Create Drop
Create Drop
Database language
Structured Query Language(SQL) as we all know is the database language by the use of
which we can perform certain operations on the existing database and also we can use this
language to create a database. SQL uses certain commands like Create, Drop, Insert etc. to
carry out the required tasks.
These SQL commands are mainly categorized into four categories as:
1. DDL – Data Definition Language
2. DML – Data Manipulation Language
3. DCL – Data Control Language
4. TCL – Transaction Control Language
DML(Data Manipulation Language) : The SQL commands that deals with the
manipulation of data present in the database belong to DML or Data Manipulation Language
and this includes most of the SQL statements.
Examples of DML:
INSERT – is used to insert data into a table.
UPDATE – is used to update existing data within a table.
DELETE – is used to delete records from a database table.
DCL(Data Control Language) : DCL includes commands such as GRANT and REVOKE
which mainly deals with the rights, permissions and other controls of the database system.
Examples of DCL commands:
GRANT-gives user’s access privileges to database.
REVOKE-withdraw user’s access privileges given by using the GRANT command
Creating Table
You can verify if your table has been created successfully by looking at the
message displayed by the SQL server, otherwise you can use the DESC command
as follows −
SQL> DESC CUSTOMERS;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| ID | int(11) | NO | PRI | | |
| NAME | varchar(20) | NO | | | |
| AGE | int(11) | NO | | | |
| ADDRESS | char(25) | YES | | NULL | |
| SALARY | decimal(18,2) | YES | | NULL | |
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Dropping table
DROP Table students;
constraints
CREATE TABLE people(LastName varchar(255) primary key NOT NULL,
FirstName varchar(255 ) NOT NULL, City VARCHAR(55));
CREATE TABLE students (
ID int primary key,
LastName varchar(255) NULL,
FirstName varchar(255) NOT NULL,
Address varchar(255) ,
City varchar(255) default 'Delhi',
emailid varchar(255) NOT NULL UNIQUE,
Age int CHECK (Age>=18)
);
Foreign key
CUSTOMERS table
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
ORDERS table
CREATE TABLE ORDERS (
ID INT NOT NULL,
DATE DATETIME,
CUSTOMER_ID INT references CUSTOMERS(ID),
AMOUNT double,
PRIMARY KEY (ID)
);
If the ORDERS table has already been created and the foreign key has not yet
been set, the use the syntax for specifying a foreign key by altering a table.
ALTER TABLE ORDERS
ADD FOREIGN KEY (Customer_ID) REFERENCES CUSTOMERS (ID);
2. To implementation on DML, TCL and DRL (a) Insert, (b) Select, (c) Update, (d) Delete,
(e) commit, (f) rollback, (g) save point, (i) Like'%', (j)Relational Operator.
Insert
Select statement
select * from Customers;
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Updating table
Thus, two rows from the table would be deleted and the SELECT statement would
produce the following result.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Rollback
Thus, the delete operation would not impact the table and the SELECT statement
would produce the following result.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Savepoint
START TRANSACTION;
SAVEPOINT initial;
UPDATE cusomers
SET firstName = 'Harry'
where D = 2216;
ROLLBACK TO initial
Like operator
Relational operator
SELECT * FROM students
WHERE age > 20;
4.To implement Nested Queries & Join Queries - To implementation of Nested Queries - (a)
Inner join, (b) Left join, (c) Right join (d) Full join
Nested Queries
SELECT *
FROM CUSTOMERS
WHERE ID IN (SELECT ID
FROM CUSTOMERS
WHERE SALARY > 4500) ;
Inner Join
1. SELECT officers.officer_name, officers.address, students.course_name
2. FROM officers
3. INNER JOIN students
4. ON officers.officer_id = students.student_id;
Output:
Left join
1. SELECT officers.officer_name, officers.address, students.course_name
2. FROM officers
3. LEFT JOIN students
4. ON officers.officer_id = students.student_id;
Output:
Right Join
SELECT officers.officer_name, officers.address, students.course_name, students.stu
dent_name
FROM officers
RIGHT JOIN students
ON officers.officer_id = students.student_id;
Output:
Full Join
SELECT officers.officer_name, officers.address, students.course_name, students.stu
dent_name
FROM officers
FULL JOIN students
ON officers.officer_id = students.student_id;
5. To implement Views - (a) View, (b) joint view, (c) force view, (d) View with check option
Now, you can query CUSTOMERS_VIEW in a similar way as you query an actual
table. Following is an example for the same.
SQL > SELECT * FROM CUSTOMERS_VIEW;
INSERT INTO
vps(employeeNumber,firstname,lastname,jobtitle,extension,email
,officeCode,reportsTo)
VALUES(1704,'John','Smith','IT
Staff','x9112','johnsmith@classicmodelcars.com',1,1703);
Error Code: 1369. CHECK OPTION failed 'classicmodels.vps'
VALUES(1227,'John','Smith','New Delhi','Delhi','johnsmith@classicmodelcars.com',23);
6.Describe control structure