Cs Record

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 30

Relational Database Management System(P) 121317044050

1.AN INTRODUCTION TO SQL [Structured Query Language]


SQL is Structured Query Language, which is a computer language for storing, manipulating and retrieving
data stored in a relational database. SQL is the standard language for Relational Database System. All the
Relational Database Management Systems (RDMS) like MySQL, MS Access, Oracle, Sybase, Informix,
Postgres and SQL Server use SQL as their standard database language.

Why SQL?
SQL is widely popular because it offers the following advantages:
 Allows users to access data in the relational database management systems.
 Allows users to describe the data.
 Allows users to define the data in a database and manipulate that data
 Allows to embed within other languages using SQL modules, libraries& pre-compilers.
 Allows users to create and drop databases and tables
 Allows users to create view, stored procedure, functions in a database
 Allows users to set permissions on tables, procedures and views.

SQL Process
When you are executing an SQL command for any RDBMS, the system determines the best way to carry out
your request and SQL engine figures out how to interpret the task. There are various components included in
this process. These components are –
 Query Dispatcher
 Optimization Engines
 Classic Query Engine
 SQL Query Engine, etc.

BSC(MSCS) SEM IV Page 1 RAYI SAHITHI


Relational Database Management System(P) 121317044050

MySQL

MySQL tutorial provides basic and advanced concepts of MySQL. Our MySQL
tutorial is designed for beginners and professionals.
MySQL is a relational database management system. It is open-source and free.
Our MySQL tutorial includes all topics of MySQL database such as insert record,
update record, delete record, select record, create table, drop table etc. There are also
given MySQL interview questions to help you better understand the MySQL database.

What is MySQL

MySQL is a fast, easy to use relational database. It is currently the most popular open-
source database. It is very commonly used in conjunction with PHP scripts to create
powerful and dynamic server-side applications.
MySQL is used for many small and big businesses. It is developed, marketed and
supported by MySQL AB, a Swedish company. It is written in C and C++.

Reasons of popularity

 MySQL is becoming so popular because of these following reasons:


 MySQL is an open-source database so you don't have to pay a single penny to
use it.
 MySQL is a very powerful program so it can handle a large set of functionality
of the most expensive and powerful database packages.
 MySQL is customizable because it is an open source database and the open-
source GPL license facilitates programmers to modify the SQL software
according to their own specific environment.
 MySQL is quicker than other databases so it can work well even with the large
data set.
 MySQL supports many operating systems with many languages like PHP,
PERL, C, C++, JAVA, etc.
 MySQL uses a standard form of the well-known SQL data language.
 MySQL is very friendly with PHP, the most popular language for web
development.
 MySQL supports large databases, up to 50 million rows or more in a table. The
default file size limit for a table is 4GB, but you can increase this (if your
operating system can handle it) to a theoretical limit of 8 million terabytes (TB).

BSC(MSCS) SEM IV Page 2 RAYI SAHITHI


Relational Database Management System(P) 121317044050

SQL Commands
The standard SQL commands to interact with relational databases are CREATE, SELECT, INSERT, UPDATE,
DELETE and DROP. These commands can be classified into the following groups based on their nature

DDL - Data Definition Language

Command Description

CREATE Creates a new table, a view of a table, or other object in the


database.

ALTER Modifies an existing database object, such as a table.

Deletes an entire table, a view of a table or other objects in the


DROP database
.

DML - Data Manipulation Language

Command Description

SELECT Retrieves certain records from one or more tables.

INSERT Creates a record.

UPDATE Modifies records.

DELETE Deletes records.

DCL - Data Control Language

Command Description

GRANT Gives a privilege to user.

REVOKE Takes back privileges granted from user.

BSC(MSCS) SEM IV Page 3 RAYI SAHITHI


Relational Database Management System(P) 121317044050

SQL DATATYPES

The SQL data type defines a kind of value that a column can contain.
In a database table, every column is required to have a name and a data type.

DATA TYPE SYNTAX EXPLANATION


Integer INTEGER The integer data type is used to specify an integer
value.
Character CHAR(X) Here ‘x’ is the character’s number to store.
Character VARCHAR(X) Here ‘x’ is the character’s value to store.
varying
Decimal DECIMAL(P,S) It specifies a decimal value. Here ‘p’ is the precision
value and ‘s’ is the scale value.
Date DATE It stores year, month and date values.

SQL OPERATORS
SQL statements generally contain some reserved words or characters that are used to perform
operations such as comparison and arithmetical operations etc. These reserved words or characters
are known as operators.

Generally there are three types of operators in SQL:


 SQL Arithmetic Operators
 SQL Comparison Operators
 SQL Logical Operators

SQL ARITHMETIC OPERATORS

OPERATORS DESCRIPTIONS

+ It is used to add containing values of both operands.

- It subtracts right hand operand from left hand operand.

* It multiply both operand value.

/ It divides left hand operand by right hand operand.

% It divides left hand operand by right hand operand and returns


remainder.

SQL COMPARISION OPERATORS


BSC(MSCS) SEM IV Page 4 RAYI SAHITHI
Relational Database Management System(P) 121317044050

OPERATOR DESCRIPTION
= Examine both operand value that are equal or not, If yes the condition is true.
!= This is used to check the value of both operands equal or not, If not the
condition becomes true.
> Examines the left operand value is greater than the right operand value, If yes
the condition becomes true.
< Examines the left operand value is less than the right operand value, If yes the
condition is true.
>= Examines the left operand value is greater than or equal to the value of right
operand value, If yes the condition is true.
<= Examines the left operand value is less than or equal to the value of right
operand value, If yes the condition is true.
!< Examines the left operand value is not less than the right operand value.
!> Examines the left operand value is not greater than the right operand value.

SQL LOGICAL OPERATORS

OPERATOR DESCRIPTION
ALL This operator is used to compare a value to all values in another value
set.
AND This operator allows the existence of multiple conditions in an SQL
statement.
ANY This operator is used to compare the value that are with the set of values.
BETWEEN This operator is used to search for the set of values that are within a set
of values.
IN This operator is used to compare a value that are within a set of values.
NOT This operator reverses the meaning of any logical operator.
OR This operator is used to combine multiple conditions in an SQL
statement.
EXIST This operator is used to search for the presence of a row in s specified
table.
LIKE This operator is used to compare a value to similar values using wild
card operator.
String operators: LIKE, % , _
(Percentage and underscore are wild characters)
(% indicates multiple characters, _ indicates a single char)

BSC(MSCS) SEM IV Page 5 RAYI SAHITHI


Relational Database Management System(P) 121317044050

SQL CLAUSES

OPERATOR DESCRIPTION
WHERE It can be used to limit the number of rows affected by a SQL DML
statement or returned by a query.
AND It is used in SQL query to create two or more conditions to be met.
OR It is used in a SQL query to create a SQL statement where records
are returned when any one of the condition met.
WITH It is used to provide a sub-query block which can be referenced in
several places within the main SQL query.
AS It is used to assign temporarily a new name to a table column.

SQL CONSTRAINTS
Constraints are the rules that we can apply on the type of data in a table. That is, we can specify the
limit on the type of data that can be stored in a particular column in a table using constraints.
The available constraints in SQL are:
 NOT NULL: This constraint tells that we cannot store a null value in a column. That is, if a
column is specified as NOT NULL then we will not be able to store null in this particular
column any more.
 UNIQUE: This constraint when specified with a column, tells that all the values in the
column must be unique. That is, the values in any row of a column must not be repeated.
 PRIMARY KEY: A primary key is a field which can uniquely identify each row in a table.
And this constraint is used to specify a field in a table as primary key.
 FOREIGN KEY: A Foreign key is a field which can uniquely identify each row in another
table. And this constraint is used to specify a field as Foreign key.
 CHECK: This constraint helps to validate the values of a column to meet a particular
condition. That is, it helps to ensure that the value stored in a column meets a specific
condition.
 DEFAULT: This constraint specifies a default value for the column when no value is
specified by the user.

SQL AGGREGATE FUNCTIONS


In database management an aggregate function is a function where the values of multiple rows are
grouped together as input on certain criteria to form a single value of more significant meaning.
OPERATOR DESCRIPTION
COUNT Counts rows in a specified table or view.
SUM Calculates the sum of values.
AVERAGE Calculates the average of a set of values.
MINIMUM Gets the minimum value in a set of values.
MAXIMUM Gets the maximum value in a set of values.

BSC(MSCS) SEM IV Page 6 RAYI SAHITHI


Relational Database Management System(P) 121317044050

SQL FUNCTIONS :
My SQL has many In built-in functions.This reference contains string, numeric, date and some
advanced functions in SQL.

OPERATOR DESCRIPTION

ABSTRACT Returns the absolute value of an number

LOWER Converts a string to lower-case

UPPER Converts a string to upper-case

REVERSE Reverse a string and returns the result

REPLACE Replaces all occurrences of a substring within a string with a new


string

POWER Returns the value of a number raised to the power of another number

ROUND Rounds a number to a specified number of decimal places

CEIL Returns the smallest integer value that is >= to a number

YEAR Returns the year part for a given data

MONTH Returns the month part for a given data

DATE Extracts the date part from a date type expression.

BSC(MSCS) SEM IV Page 7 RAYI SAHITHI


Relational Database Management System(P) 121317044050

DATABASE TABLES
A database most often contains one or more tables. Each table is identified by a names . Tables
contain records (rows) with data.

SYNTAX FOR CREATING A DATABASE


Create Database Database_Name

EX: Create Database KA121317052022

SYNTAX FOR CREATING A TABLE


Create Table table_name (column_name1 datatype(size), column_name2 datatype(size),

... column_nameN datatype(size))

EX: Create Table Bscbooks(Sno int,Bookname char(30),Author char(20),ISBN


varchar(15),Copiestaken numeric(5),Noofcopies numeric(5))

REFERENTIAL INTEGRITY CONSTRAINT


Referential integrity constraint refers to relationship between the tables. The relationship between
tables is established by using foreign keys. This constraint states that a foreign key must have a
matching primary key in another table or must be null.

FOREIGN KEY: foreign key is used to relate two tables. Foreign key constraint is also used to
restrict actions that would destroy links between tables.

BSC(MSCS) SEM IV Page 8 RAYI SAHITHI


Relational Database Management System(P) 121317044050

2.CREATING TABLES AND INSERTING RECORDS

use sahithi50;
create table employee(eno int,ename varchar(50),gender
varchar(15),salary int,doj date,com int);
insert into employee values(1,'sahith','male',500000,'2017-8-19',10000);
insert into employee values(2,'mahitha','female',50000,'2017-7-19',5000);
insert into employee values(3,'rajit','male',50000,'2015-5-9',1000);
insert into employee values(4,'sumanth','male',51000,'2017-8-9',15000);
insert into employee values(5,'rani','female',20000,'2017-8-9',1000);
select * from employee;

OUTPUT:

ENO ENAME Gender Salary Doj Com


1 Sahith Male 500000 2017-8- 10000
19
2 Mahitha Female 50000 2017-7- 5000
19
3 Rajit Male 50000 2015-5-9 1000
4 Sumanth Male 51000 2017-8-9 15000
5 rani female 20000 2017-8-9 1000

BSC(MSCS) SEM IV Page 9 RAYI SAHITHI


Relational Database Management System(P) 121317044050

3.CREATING TABLES WITH PRIMARY KEY AND INSERTING


RECORDS

Use sahithi50;
create table products(prod_id int primary key,pname
varchar(50),ptype varchar(50),pprice int);
insert into products values(1,'handbag','accessories',50000);
insert into products values(2,'phone','electronics',100000);
insert into products values(3,'books','stationery',50000);
insert into products values(4,'shoes','footwear',25000);
insert into products values(5,'clothes','fashion wear',100000);
select * from products;

OUTPUT:

PROD_ID PNAME PTYPE PPRICE


1 Handbag Accessories 50000
2 Phone Electronics 100000
3 Books Stationery 50000
4 Shoes Footwear 25000
5 clothes Fashion wear 100000

BSC(MSCS) SEM IV Page 10 RAYI SAHITHI


Relational Database Management System(P) 121317044050

4.CREATING TABLES WITH SIMPLE SELECT STATEMENT


Use sahithi50;

create table employee(eno int,ename varchar(50),gender varchar(15),salary


int,doj date,com int);

insert into employee values(1,'sahith','male',500000,'2017-8-19',10000);

insert into employee values(2,'mahitha','female',50000,'2017-7-19',5000);

insert into employee values(3,'rajit','male',50000,'2015-5-9',1000);

insert into employee values(4,'sumanth','male',51000,'2017-8-9',15000);

insert into employee values(5,'rani','female',20000,'2017-8-9',1000);

select * from employee;

OUTPUT:

ENO ENAME Gender Salary Doj Com


1 Sahith Male 500000 2017-8-19 10000
2 Mahitha Female 50000 2017-7-19 5000
3 Rajit Male 50000 2015-5-9 1000
4 Sumanth Male 51000 2017-8-9 15000
5 rani female 20000 2017-8-9 1000

BSC(MSCS) SEM IV Page 11 RAYI SAHITHI


Relational Database Management System(P) 121317044050

5. CREATING TABLES WITH PRIMARY KEY,FOREIGN KEY AND


OTHER CONSTRAINTS SUCH AS UNIQUE ,NOT NULL,DEFAULT
ETC AND INSERTING RECORDS
Use sahithi50;

create table course(cno int primary key,cname varchar(50)default 'good


course',ccost int check(ccost>1000),cdetail varchar(50) );

create table student(rollno varchar(50) primary key,sname varchar(50),fname


varchar(50) not null,address varchar(50),gender varchar(25)check ('m''f'),doj
date,age int,stream varchar(50) check ('bsc''bcom''ba''bms'),contactno
varchar(50) unique ,cno int references course2(cno));

insert into course values(8,'business',4000,'commerce');

insert into course values(9,'pschology',2000,'learn about youself');

insert into course values(10,'english',1500,'prepare yourself for competitive


world');

insert into course(cno,ccost)values(11,2500);

insert into course(cno,ccost,cdetail)values(12,2300,'enhance your skills');

select * from course;

insert into student values(21,'sahi','kavith','hyd','f','2000-2-


7',18,'bsc','13476997' ,8);

insert into student values(22,'hidi','salman','mumbai', 'f','1999-2-


7',19,'ba','49586956' ,9);

insert into student values(23,'krishna','krishnan','delhi','m','1998-2-


17',19,'bcom','653437' ,10);

insert into student(rollno,sname,fname,gender)


values(24,‘chaturya’,’srinivas’,’f’);

insert into student values(25,'ruhi','rohan','surat','f','1997-6-


14',20,'bms','5446765' ,11);
BSC(MSCS) SEM IV Page 12 RAYI SAHITHI
Relational Database Management System(P) 121317044050

select * from student;

OUTPUT:
TABLE NAME: COURSE

CNO CNAME CCOST CDETAIL


8 Business 4000 Commerce
9 Psychology 2000 learn about
youself
10 English 1500 prepare yourself
for competitive
world
11 Good course 2500 -
12 Good course 2300 enhance your
skills

TABLE NAME:STUDENT

ROLL SNAME FNAME ADDRE GEND DOJ AG STREA CONN CNO


NO SS ER E M O
21 Sahi Kavith Hyd Female 2000- 18 BSC 13476 8
2-7 997
22 Hidi Salman Mumbai Female 1999- 19 BA 49586 9
2-7 956
23 Krishna Krishna Delhi Male 1998- 19 BCOM 65343 10
n 2-17 7
24 Chaturya srinivas - Female - - - - 11
25 Ruhi Rohan surat female 1997- 20 BMS 54467 12
6-14 65

BSC(MSCS) SEM IV Page 13 RAYI SAHITHI


Relational Database Management System(P) 121317044050

6.SELECT WITH WHERE CLAUSE

Use sahithi50;

create table emp(eno int primary key,ename varchar(50),gender


varchar(15),salary int,doj date,com int,dno int);

insert into emp values(1,'amit','male',500000,'2017-8-19','10000',1);

insert into emp values(2,'bhavya','female',500000,'2017-7-19','15000',2);

insert into emp values(3,'neil','male',5000000,'2017-8-9','10000',3);

select * from emp;

insert into emp values(4,'kunal','male',60000,'2017-8-19','10000',1);

insert into emp values(5,’santhosh','male',20000,'2017-8-19','10000',1);

create table dept(deptno int primary key,deptname varchar(50),location


varchar(20),noofemp int ,dateofst date);

select * from dept;

insert into dept values(11,'stats','ugblock',3,'2000-2-7');

insert into dept values(12,'maths','pgblock',6,'1999-3-4');

insert into dept values(13,'cs','pgblock',10,'1998-3-4');

insert into dept values(14,'language','pgblock',5,'1956-3-4');

BSC(MSCS) SEM IV Page 14 RAYI SAHITHI


Relational Database Management System(P) 121317044050

output:
table name: emp
Empno ename Salary doj Com Dno
1 Amit 500000 2017-08- 10000 1
19
2 Bhavya 500000 2017-07- 15000 2
19
3 Neil 5000000 2017-08- 10000 3
09
4 Kunal 60000 2017-08- 10000 1
19
5 santhosh 20000 2017-08- 10000 1
19

Table name :dept

Deptno Deptname location Noofemp Dateofst


11 Stats ugblock 3 2000-02-07
12 Maths Pgblock 6 1999-03-04
13 Cs Pgblock 10 1998-03-04
14 Language pgblock 5 1956-03-04

select ename from emp where salary=50000;

output:

Empno ename Salary Doj Com Dno


1 Amit 50000 2017-08- 10000 1
19
2 Bhavya 50000 2017-07- 15000 2
19

select ename from emp where gender='female’;

Empno ename Salary doj Com Dno


2 Bhavya 500000 2017-07- 15000 2
19

BSC(MSCS) SEM IV Page 15 RAYI SAHITHI


Relational Database Management System(P) 121317044050

7.CREATE TABLES AND PERFORMING OPERATIONS:

Use sahithi50;

create table emp(eno int primary key,ename varchar(50),gender


varchar(15),salary int,doj date,com int,dno int);

insert into emp values(1,'amit','male',500000,'2017-8-19','10000',1);

insert into emp values(2,'bhavya','female',500000,'2017-7-19','15000',2);

insert into emp values(3,'neil','male',5000000,'2017-8-9','10000',3);

select * from emp;

insert into emp values(4,'kunal','male',60000,'2017-8-19','10000',1);

insert into emp values(5,’santhosh','male',20000,'2017-8-19','10000',1);

create table dept(deptno int primary key,deptname varchar(50),location


varchar(20),noofemp int ,dateofst date);

select * from dept;

insert into dept values(11,'stats','ugblock',3,'2000-2-7');

insert into dept values(12,'maths','pgblock',6,'1999-3-4');

insert into dept values(13,'cs','pgblock',10,'1998-3-4');

insert into dept values(14,'language','pgblock',5,'1956-3-4');

select ename from emp where salary <=20000


Ename
Santhosh
Select deptname from dept where deptno=14;
Deptname
Language

BSC(MSCS) SEM IV Page 16 RAYI SAHITHI


Relational Database Management System(P) 121317044050

8.CREATING TABLES AND PERFORMING AGGREAGATE FUNCTIONS

Use sahithi50;

create table emp(eno int primary key,ename varchar(50),gender


varchar(15),salary int,doj date,com int,dno int);

insert into emp values(1,'amit','male',500000,'2017-8-19','10000',1);

insert into emp values(2,'bhavya','female',500000,'2017-7-19','15000',2);

insert into emp values(3,'neil','male',5000000,'2017-8-9','10000',3);

select * from emp;

insert into emp values(4,'kunal','male',60000,'2017-8-19','10000',1);

insert into emp values(5,’santhosh','male',20000,'2017-8-19','10000',1);

create table dept(deptno int primary key,deptname varchar(50),location


varchar(20),noofemp int ,dateofst date);

select * from dept;

insert into dept values(11,'stats','ugblock',3,'2000-2-7');

insert into dept values(12,'maths','pgblock',6,'1999-3-4');

insert into dept values(13,'cs','pgblock',10,'1998-3-4');

insert into dept values(14,'language','pgblock',5,'1956-3-4');

select count(*) from emp;

BSC(MSCS) SEM IV Page 17 RAYI SAHITHI


Relational Database Management System(P) 121317044050

output:

select count(*) from emp;

Count(*)
5

Select sum(salary) from emp;

Sum(salary)
6080000

Select avg(salary) from emp;

Avg(salary)
1216000.0000

Select min(salary) from emp;

Min(salary)
20000

BSC(MSCS) SEM IV Page 18 RAYI SAHITHI


Relational Database Management System(P) 121317044050

9.CREATING TABLES AND PERFORMING MATHEMATICAL


FUNCTIONS
Use sahithi50;

Select abs(-45);

Select acos(45);

Select asin(45);

Select atan(45);

Select avg(34);

Select ceiling(89);

Select cos(45);

Select cot(45);

Select degrees(67);

Select exp(99);

Select floor(99);

Select log10(200);

Select radians(45);

Select rand(95);

Select round(87.9);

Select sign(78);

Select sqrt(64);

Select tan(45);

BSC(MSCS) SEM IV Page 19 RAYI SAHITHI


Relational Database Management System(P) 121317044050

10.SELECT USING AND,OR,IN,BETWEEN,LIKE,NOT CLAUSES


Use sahithi50;

create table emp(eno int primary key,ename varchar(50),gender


varchar(15),salary int,doj date,com int);

insert into employee values(1,'sahith','male',50000,'2017-8-19',10000);

insert into employee values(2,'mahitha','female',50000,'2017-7-19',5000);

insert into employee values(3,'rajit','male',15000,'2015-5-9',1000);

insert into employee values(4,'sumanth','male',51000,'2017-8-9',15000);

insert into employee values(5,'rani','female',20000,'2017-8-9',1000);

select ename,salary from emp where salary between 20000 and 30000;

OUTPUT:

ENAME SALARY
Rani 20000

select ename , salary from emp where gender='female' and salary between
20000 and 30000;

OUTPUT:

ENAME SALARY
Rani 20000

Select ename,salary from emp where salary in(40000,50000);

Output:

ENAME SALARY
Sahith 50000
Mahitha 50000
BSC(MSCS) SEM IV Page 20 RAYI SAHITHI
Relational Database Management System(P) 121317044050

select ename from emp where salary>50000 and doj='2017-8-9’;

ENAME
Sumanth

select ename from emp where salary<25000;

ENAME
Rajith
Rani

Select ename from emp where ename like 'm%';

ENAME
Mahitha

select ename from emp where ename like 'r%' and '%t';

ENAME
Rajit

select ename from emp where ename not like '%i';

ENAME
Sahith
Mahitha
Rajit
Sumanth

select ename from emp where gender=’female’ or salary=59000;

ENAME
Mahitha
Rani

BSC(MSCS) SEM IV Page 21 RAYI SAHITHI


Relational Database Management System(P) 121317044050

11.ORDER BY,DISTINCT CLAUSES(ASCENDING AND


DESCENDING ORDER)
Use sahithi50;

create table emp(eno int primary key,ename varchar(50),gender


varchar(15),salary int,doj date,com int);

insert into employee values(1,'sahith','male',50000,'2017-8-19',10000);

insert into employee values(2,'mahitha','female',50000,'2017-7-19',5000);

insert into employee values(3,'rajit','male',15000,'2015-5-9',1000);

insert into employee values(4,'sumanth','male',51000,'2017-8-9',15000);

insert into employee values(5,'rani','female',20000,'2017-8-9',1000);

OUTPUT:

select ename,salary from emp where ename like'r%' order by doj;

ENAME SALARY
Rajiith 15000
Rani 20000

select eno,ename from emp where gender=’female’ order by eno;

ENO ENAME
2 Mahitha
5 Rani

BSC(MSCS) SEM IV Page 22 RAYI SAHITHI


Relational Database Management System(P) 121317044050

select distinct eno from emp;

ENO
1
2
3
4
5

select distinct ename from emp;

ENAME
Sahith
Mahitha
Rajit
Sumanth
Rani

BSC(MSCS) SEM IV Page 23 RAYI SAHITHI


Relational Database Management System(P) 121317044050

12.CREATE TABLE USING GROUP BY CLAUSE


Use sahithi50;

create table emp(eno int primary key,ename varchar(50),gender


varchar(15),salary int,doj date,com int,dno int);

insert into emp values(1,'amit','male',500000,'2017-8-19','10000',1);

insert into emp values(2,'bhavya','female',500000,'2017-7-19','15000',2);

insert into emp values(3,'neil','male',5000000,'2017-8-9','10000',3);

insert into emp values(5,’santhosh','male',20000,'2017-8-19','10000',1);

insert into emp values(4,'kunal','male',60000,'2017-8-19','10000',1);

select * from emp group by ename;

OUTPUT:

Empno Ename Salary doj Com dno


1 Amit 500000 2017-08- 10000 1
19
2 Bhavya 500000 2017-07- 15000 2
19
3 Kunal 60000 2017-08- 10000 1
19
4 Neil 5000000 2017-08- 10000 3
09
5 Santhosh 20000 2017-08- 10000 1
19

BSC(MSCS) SEM IV Page 24 RAYI SAHITHI


Relational Database Management System(P) 121317044050

13.ALTERING THE TABLE STRUCTURE USING ALTER


COMMAND
Use sahithi50;

create table hotel(hotelno int primary key,hotelname varchar(50),city


varchar(50));

insert into hotel values(131,'sitara','hyderabad');

insert into hotel values(132,'taj','bangalore');

insert into hotel values(133,'itc kakatiya','mumbai');

insert into hotel values(134,'haveli','kolkata');

insert into hotel values(135,'paradise','hyderabad');

alter table hotel add telno varchar(50);

select * from hotel;

OUTPUT:

HOTELNO HOTELNAME CITY TELNO


131 Sitara hyderabad -
132 Taj bangalore -
133 itc Kakatiya mumbai -
134 Haveli kolkata -
135 Paradise hyderabad -

BSC(MSCS) SEM IV Page 25 RAYI SAHITHI


Relational Database Management System(P) 121317044050

14.UPDATING AND DELETING TABLE ENTRIES

Use sahithi50;

create table hotel(hotelno int primary key,hotelname varchar(50),city


varchar(50));

insert into hotel values(131,'sitara','hyderabad');

insert into hotel values(132,'taj','bangalore');

insert into hotel values(133,'itc kakatiya','mumbai');

insert into hotel values(134,'haveli','kolkata');

insert into hotel values(135,'paradise','hyderabad');

alter table hotel add telno varchar(50);

update hotel set telno='9949694718' where hotelno=131;

OUTPUT:

HOTELNO HOTELNAME CITY TELNO


131 Sitara hyderabad 9949694718
132 Taj bangalore -
133 itc Kakatiya mumbai -
134 Haveli kolkata -
135 Paradise hyderabad -

delete from hotel where city='mumbai';

HOTELNO HOTELNAME CITY TELNO


131 Sitara hyderabad 9949694718
132 Taj bangalore -
134 Haveli kolkata -
135 Paradise hyderabad -

BSC(MSCS) SEM IV Page 26 RAYI SAHITHI


Relational Database Management System(P) 121317044050

15.JOINS
Use sahithi50;

create table student2(rollno int primary key,student_name


varchar(50),student_location varchar(50),student_course varchar(50));

insert into student2 values(1,'Maria','Secunderabad','Physical Sciences');

insert into student2 values(2,'Suzan','Banglore','Social Sciences');

insert into student2 values(3,'Robert','Chennai','Life Sciences');

insert into student2 values(4,'William','Pune','Commerce');

insert into student2 values(5,'Ellena','Hyderabad','Finance');

insert into student2 values(6,'Edward','Bombay','Political Sciences');

select * from student2;

create table course2(cno int primary key,course_name


varchar(50),course_cost long,rollno int references student(rollno));

insert into course2 values(1,'B.Sc M.S.Cs',600000,1);

insert into course2 values(2,'B.A PPI',800000,2);

insert into course2 values(3,'B.Sc BZC',700000,3);

insert into course2 values(4,'B.Com H',590000,4);

insert into course2 values(5,'B.Com IPP',650000,5);

insert into course2 values(6, 'B.SC POL',400000,6);

select * from student2 s join course2 c on s.rollno=c.cno;

BSC(MSCS) SEM IV Page 27 RAYI SAHITHI


Relational Database Management System(P) 121317044050

OUTPUT:

Rollno stname Stloc stcourse cno cname ccost rollno


1 Maria secunderaba Physical 1 BSC 600000 1
d science MSCS
2 Suzan Bangalore social 2 BA 800000 2
science PPI
3 Robert Chennai Life 3 BSC 700000 3
science BZC
4 William Pune Commerce 4 BCOM 590000 4
H
5 Ellena Hyderabad Finance 5 BCOM 650000 5
IPP
6 edward bombay Political 6 BSC 400000 6
science POL

BSC(MSCS) SEM IV Page 28 RAYI SAHITHI


Relational Database Management System(P) 121317044050

EXAMPLE:

Use sahithi50;

create table emp1(eno int primary key,ename varchar(50),gender


varchar(50),job varchar(50),salary long,doj date);

insert into emp values(1,'sirisha','Female','Analyst','900000','1987/10/02');

select * from emp1; select * from emp1 where salary='15000';

select * from emp1 where salary!='50000';

select * from emp1 where gender='Female';

select * from emp1 where salary between 20000 and 85000;

select * from emp1 where gender='Female' and salary between 20000 and
30000;

select ename from emp1 where salary in(30000,40000);

select ename from emp1 where salary>30000 and doj<'1998/1/15';

select ename from emp1 where ename like 'a%';

select ename from emp1 where ename like '%m%';

select ename from emp1 where ename not like'%h';

select ename from emp1 where ename like '_a%';

select ename,eno from emp1 where eno in(101,104,105);

select ename from emp1 where ename like'_____';

select eno,salary from emp1 where job='Analyst';

select ename from emp1 where job!='clerk';

select eno from emp1; select ename,job from emp1 where job not
in('clerk','manager');
BSC(MSCS) SEM IV Page 29 RAYI SAHITHI
Relational Database Management System(P) 121317044050

select ename,salary from emp1 where doj like'1983%';

alter table emp1 add(location varchar(50);

update emp1 set location=’secunderabad’ where eno=1;

select * from emp1;

BSC(MSCS) SEM IV Page 30 RAYI SAHITHI

You might also like