SQL New Slide
SQL New Slide
Language
Structured Query Language
• Structured Query Language (SQL) is a language
designed for managing data in relational database
management system (RDBMS). SQL provides an easy
and efficient way to interact with relational databases.
Components of SQL
Command Description
-
SELECT Retrieves certain records from one or more
tables.
-
INSERT Creates a record.
-
UPDATE Modifies records.
-
DELETE Deletes records.
Data Control Language(DCL)
-
Command Description
Data type defines the type of value that may be entered in the
column of a table. MySQL data types are classified into three.
They are
-• Numeric data type:
The most commonly used numeric data types in MySQL are INT
or INTEGER and DEC or DECIMAL.
-• String (text) data type:
String is a group of characters. The most commonly used string
data types in MySQL are CHARACTER or CHAR and
VARCHAR.
-
• Date and Time data type.:
MySQL has data types for storing dates and times. The data type
used to store date type value is DATE and to store time value is
TIME.
Numeric datatype
- -
-
-
O
-
-
O
- E
O 6
-
>
O
O
-
- -
-
O
O O
String Datatypes
-
-
CHAR VARCHAR
Its full name is CHARACTER Its full name is VARIABLE
-
- CHARACTER
-
-
-
It can hold a maximum It can hold a maximum of 65,535
-
of 255 characters. characters.
- -
E
mysql>create table student(name
CHAR(20));
--
mysql>create table student1(name
VARCHAR(20));
-
-
DATE and TIME Datatype
9/10/3
DATE TIME
The DATE data type is used to store
-
The TIME data type is used to store
-
dates. time values.
-
-
-
-
MySQL
SQL commands
-
• Creating tables:
-
specifying the name of the table and giving the column definitions
consisting of name of the column, data type and size, and constraints if
any, etc.
The syntax of CREATE TABLE command is:
CREATE TABLE <table_name>
--
(<column_name>
-
<data_type>
-
[<constarints>],
<column_name> <data_type> [<constarints>],
--
………………………………………………………………………..
-
-
-
.............................. .............................. );
-
Rules for naming tables and
columns
1. The name may contain letters (A - Z, a - z), digits (0 -
9), under score ( _ ) and dollar ($) symbol.
course, f-Income
• Eg: Create table:
CREATE TABLE student (adm_no INT,
-- ---
-
name VARCHAR(20),
-
gender CHAR,
-
dob DATE,
- -
course VARCHAR(15),
--
f_income INT);
--
Constraints
Constraints are the rules enforced on data that are entered into the
column of a table. Constraints could - be column level or table
-
-
level.
no
Column Constraints: wome
AUTO_INCREMENT: - -
Allows a unique number to be generated automatically when a
new record is inserted into a table.
PRIMARY KEY:
Uniquely identified each rows/records in a database table.
-
UNIQUE:
-
dob DATE,
-
course VARCHAR(15),
-
-
f_income INT);
-
Table Constraints:
Table constraints are similar to column constraints. But
table constraints are used not only on individual columns,
but also on a group of columns.
CREATE TABLE stock
(icode CHAR(2) PRIMARY KEY AUTO_INCREMENT,
iname VARCHAR(30) NOT NULL,
dt_purchase DATE,
rate DECIMAL(10,2),
qty INT,
UNIQUE (icode, iname));
S
Viewing the structure of a table
-
DESC <table_name> ;
- -
Eg:DESC student;
-
- -----
--
- - -
-
-
Inserting data into tables
1) The DML command INSERT INTO is used to insert tuples into tables.
VALUES(<value1>,<value2>,<value3>,………<value n>);
-
- -
For example, let us insert a new record into the table student with data 1001, 'Alok',
'M', 1998/10/2, 'Science', 24000 into the columns adm_no, name, gender, dob,
course and f_income, respectively.
Fe
----
E
INSERT INTO student(adm_no,name,gender,dob,course,f_income) VALUES
--------
cont…
2) MySQL allows inserting several rows into a table with a
single INSERT command by specifying multiple value lists.
studenty
-
-
z
-
-
-
2
2
-
- cont...
• SELECT * FROM student;
-
-
&
Eliminating duplicate values in columns using DISTINCT.
O
-
-
-
FROM <table_name>
--
WHERE <condition>;
-
•
=
SELECT * FROM student WHERE gender='F';
①
-
A
SELECT name, course, f_income FROM student WHERE course='Science' AND
f_income<25000; -
-
-
-
-
- -
cont..
Special operators for setting conditions.
-
⚫ BETWEEN...AND – Condition based on Range of
-
-
Values
-
-
⚫
-
IN – Condition based on List of Values
-
-
⚫ LIKE – Condition based on Pattern Matching
- -
=
-
>
OUTPUT
Select * from student where
IN course = "Command
&
AND course : "Humanities
-
-
, -
or
E
-
- -
OUTPUT
LIKE
Condition based on Pattern Matching
-
OUTPUT
-
-
- -
- =>
-
2
IS
-
f_income IS NULL;
-
OUTPUT
E -
-
-
-
ORDER BY clause -
-
SELECT * FROM student ORDER BY name;
-
-
-
-
-
Descending order Sorting
-
O
SELECT * FROM student ORDER BY name DESC;
-
-
Aggregate Functions
-
-
-
- -
-
- -
--
O 0
-F
SELECT COUNT(*), COUNT (f_income) FROM student
WHERE course='Science';
O
②
GROUP BY clause
- - -
O
HAVING clause
GROUP BY course
-
⚫ It is a DML Command
UPDATE <table_name>
-
= <value>,...]
[WHERE <condition>];
-
Example
WHERE name='Kaushi';
-
-
SELECT * FROM student WHERE name='Kaushi';
ALTER TABLE Command
-
⚫ Syntax:
ALTER TABLE <table_name>
-
ADD- -
<column_name> <data_type> [<size>] [<constraint>]
I
- -
Example:
ALTER TABLE student
- - -
- -
Syntax
⚫ ALTER TABLE <table_name> DROP
-
<column_name>;
-
-
Example
⚫ ALTER TABLE student DROP gr_mks;
-
(the column “gr_mks” will be removed)
-
Changing the definition of a column
MODIFY clause is used with ALTER TABLE command.
- -
Syntax:
ALTER TABLE <table_name>
-
Example:
ALTER TABLE student
&
- - -
command.
⚫ We can rename a table even though it contains tuples.
Syntax:
ALTER TABLE <table_name>
-
RENAME TO <new_table_name>;
-
Example a
ALTER TABLE student RENAME TO student2015;
---
-
DELETE FROM Command
Syntax
⚫ DELETE FROM <table_name> [WHERE <condition>];
-
-
Example
⚫ DELETE FROM student2015 WHERE adm_no=1027;
- -
⚫ Syntax
DROP TABLE <table_name>;
-
⚫ Example
DROP TABLE student2015; -
&
⚫ One query inside another Query
⚫ Inner query is known as Sub query
⚫ Outer Query
⚫ SQL first evaluates the inner query(sub query) within the WHERE
⚫ clause and the result of inner query is then substituted in the
⚫ Example
⚫ A view is a virtual table that does not really exist in the database
⚫ It is derived from one or more tables.
-
⚫ The table(s) from which the tuples are collected to create a view is
called base table(s).
⚫ Tuples are not physically stored anywhere.
⚫ The definition of the view is stored in the database.
⚫ We can view the desired information that is stored in a base table.
⚫ The contents of a view are taken from other tables using a query
Condition.
⚫ View is created using the DDL command CREATE VIEW .
⚫ Views Can perform all DML commands similar to a table.
⚫ We can use the same table as different table(virtual)without using
extra space.
⚫ Views can be Securely shared.
⚫ Reduces the complexity of conditions with WHERE clause while
accessing records.
Creating VIEW
Syntax:
CREATE VIEW <view_name>
-
--
-
Example ---
CREATE VIEW student1998 AS SELECT * FROM student2015
- -
WHERE dob < '1999-1-1';
-
Deleting VIEW
Syntax
DROP VIEW <view_name>;
Example:
DROP VIEW student1998;
-
10What is meant by column constraints? Write any
4 column constraints in SQL.
Constraints are the rules enforced on data that
are entered into the column of a table.
The column constraints are
1. NOT NULL 2
2. AUTO_INCREMENT L
3. UNIQUE -
4. PRIMARY_KEY -
5. DEFAULT -
Null values in in tables are specified as “null”.
State whether true or false.
E
false. “null” is a string
What is the use of SELECT command in SQL? Write
down its syntax of usage.
Syntax –
SELECT -
<column_name>[,<column_name>,<column_name>,
--
…] FROM <table_name>;
-
-
a. WHERE - used to select specific rows.
- -
Syntax
DELETE FROM <table_name> [WHERE <condition>];
Eg - -
Syntax
DROP TABLE <table_name>;
-
Eg
-
DROP TABLE student;
Write SQL queries based on the table STUDENT given below.
-
-
z
(a) Add a new, field percentage
-
to the table.
(b) Update percentage of all students. (percentage: Total/3)
Commen
(c) Find the average of column total from student in cornmerce -
-
batch. -
TABLE Student
a) ALTER
Percentage DE (3, 4)
;
ADD
Student
b) UPDATE
Total/s
set percentage =
;
2) select aus(total) from student
'Commenc
where batch :
;
Student
d) Delete
&
From
;
where batch : "Humantics