Cs Record
Cs Record
Cs Record
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.
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
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
Command Description
Command Description
Command Description
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.
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.
OPERATORS DESCRIPTIONS
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.
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)
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 FUNCTIONS :
My SQL has many In built-in functions.This reference contains string, numeric, date and some
advanced functions in SQL.
OPERATOR DESCRIPTION
POWER Returns the value of a number raised to the power of another number
DATABASE TABLES
A database most often contains one or more tables. Each table is identified by a names . Tables
contain records (rows) with data.
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.
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:
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:
OUTPUT:
OUTPUT:
TABLE NAME: COURSE
TABLE NAME:STUDENT
Use sahithi50;
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
output:
Use sahithi50;
Use sahithi50;
output:
Count(*)
5
Sum(salary)
6080000
Avg(salary)
1216000.0000
Min(salary)
20000
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);
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
Output:
ENAME SALARY
Sahith 50000
Mahitha 50000
BSC(MSCS) SEM IV Page 20 RAYI SAHITHI
Relational Database Management System(P) 121317044050
ENAME
Sumanth
ENAME
Rajith
Rani
ENAME
Mahitha
select ename from emp where ename like 'r%' and '%t';
ENAME
Rajit
ENAME
Sahith
Mahitha
Rajit
Sumanth
ENAME
Mahitha
Rani
OUTPUT:
ENAME SALARY
Rajiith 15000
Rani 20000
ENO ENAME
2 Mahitha
5 Rani
ENO
1
2
3
4
5
ENAME
Sahith
Mahitha
Rajit
Sumanth
Rani
OUTPUT:
OUTPUT:
Use sahithi50;
OUTPUT:
15.JOINS
Use sahithi50;
OUTPUT:
EXAMPLE:
Use sahithi50;
select * from emp1 where gender='Female' and salary between 20000 and
30000;
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