Simple Quesries in SQL &
Table Creation and Data Manipulation
Based on CBSE Curriculum
Class -11
Chapter-17 & 18
‘Scanned with CascannerIntroduction
* SQL was developed in 1970 in IBM lab. It is also known as
SEQUEL which was a non-procedural language.
+ It always specifies that WHAT is required?
+ We can perform following tasks in SQL-
|. Creation and modification of Database structure.
Il. Change in security settings for system.
Ill. Giving permission to User to work with database or table
IV. Quering a Database .
V._ Insertion/modification/deletion of data...... etcElements of MySQL
Main elements of MySQL are-
— Literals
— Datatypes
— Nulls
— Comments
Literals : generally known as fixed data value. It can be
character, numeric or text literal.
Data Types : these are of following types-
— (i) Numeric :INT, TINYINT, SMALLINT, MEDIUMINT, FLOAT , DOUBLE
etc
— (ii) Date and Time : DATE, DATETIME, TIME, YEAR etc
— (iii) String type : CHAR, VARCHAR, BLOB or TEXT TINYTEXT, ENUM
etc
‘Scanned with CascannerElements of MySQL
NULL Values : if a column in a row has no value, then
column is said to be null. NULL can appear in a column of
any data type provided they are not restricted by NOT Null
or Primary Key.
Comments : it is a text which does not execute. Comments
are used only for documentation purpose. Three types of
comments are-
/* comment */
-- comment
# commentSQL COMMAND SYNTAX
+ SQL provides a set of predefined commands.
* SQL syntax has following elements-
* Keyword:
— Statements:
— Clauses:
— Arguments:
* SQL commands are not case sensitive.
Creation of Database
* eee command is used to create a Database
For ex-
mysql> create database school;Using Database
* Following command is used to use a Database
For ex -
mysql> USE school;
A message will come saying- “database changed”
Commands end with ; or \g.
SY) CCE CaS Sat Smet 9 Ped
ee ee eT Cy * to clear the buffer.
ATABASE SCHOOL
STEet tat ee]
Ree ecur Ur
eet
Scanned with CamSeanerTable Creation
* To create a table in Database, following command is used-
mysql> CREATE TABLE
(,
, . . .);
For ex-
mysq|>create table student (Roll INT(4) Primary Key, Name CHAR(20),
(Age INT(2), City CHAR(10) ) ;
A message will come saying- “Query OK”
Primary key restrict a column to have unique values only.
‘Scanned with CascannerViewing Table structure
To see structure of a table in Database, following command is used-
sql> DESC
For ex -
mysql>DESC Student;
It displays whole structure of the table-
mysql> DESC Student;
Le i
Sore _
gd
int¢4>
char¢(2@)>
aT ep)
Pate RiP)
Scanned with CamSeanerModification in Table structure
* To modify structure of a table in Database, following
command is used-
ysql>ALTER TABLE
ADD/MODIFY
Alter Table Student Add (class INT(2));
Amessage comes saying “Query OK” .
ae a |e rs
a
ART Mee hee CC Gee b
Comes d ed (8.42 se
@ Duplicat Ups ate
Rett ccs
ec ech
(ero Te Ren Rc
‘Scanned with CascannerDropping a Table
* To drop a table in Database, following command is used-
mysql> DROP Table
;
For ex -
mysq|>drop table
A message will come saying- “Query OK” now if you want to see the structure of the
table you cant see because it has already been deleted.
Tee yey Cee at aay
Query OK, 6 rows affected (8.03 sec>
Invsq1> desc student;
IERROR 1146 (42802): Table ’school.student’ doesn’t exist
imusg 1 >
‘Scanned with CascannerData Integrity by Constraints
The checks or conditions applied on one or more columns of
a table are known as CONSTRAINTS .
These are set to maintain integrity in a table hence also
known as integrity constraints.
When a constraint is applied on a table, all the data should
follow this constraint.
Constraints are to be set at the time of table creation so that it
should be followed at the time of data insertion. Syntax is -Integrity Constraints
Constraints maintains the integrity of a database. Some of
the constraints are-
Unique Constraint : This constraint ensure that all the data of
the column should be unique. It allows null values.
Primary key Constraint : This constraints is used to uniquely
identify data. It does not accept null values.
Default Constraint : This constraint is used to set a default
values in case no value is provided by user for a column.
Check Constraint : This constraint is used to set a limit for a
column. for ex- no data should be inserted less than 20 in
age column.
Foreign key Constraint: it is a non -key attribute of one table
derived from primary key from other table.Creation of a Table
mysq|> create table student
(Roll INT(4) PRIMARY KEY,
NAME CHAR(20) NOT NULL,*
Age INT(2) CHECK (age>5); | Toten taser ss cura
Class CHAR(3) Default ‘’, <— | IRRITEC Stirs teiu NMI ace
City CHAR(10)); Sener aes an
level constraints.
EERO eee ASCs
-> CROLL INT<4> PRIMA 7
MeL kea Pm 0 |) A Pn
Sa 1S ae LIke Dame) 5
ane ‘ Se PELL > ;
ORC MCL) el: a Poth
i es oo .
ROLL
Pi
\
erica] Re CL) py cemeoo Cle}
‘Scanned with CascannerTable level constraints Setting
mysql> create table student
(Enroll INT(4),
These constraints are known as Table level constraints.
‘Scanned with CamscannerInsertion of a record in Table
Syntax to insert a record in a Table is-
{mysql> INSERT INTO (
VALUES (,,,,.
COST DT eReL
mysql> INSERT INTO STUDENT VALUES
—> €1063.’Sunita’ .?7.’I1’ .’ Barabanki’ >5
(CUS) ee ad affected C4: ick} EttaInsertion of a record in Table
Bi Ree Ue ee RMU eC e RE eR mee) ay
UCN a Re a Reba em Olea Lace RTL Nd
Ce
inysql> INSERT INTO STUDENT CROLL. NAME, AGE> VALUES
$4 5
by
Sar No constraint
Eur ss ‘
reeeten Ne amclay
Peery Urls Bates
NULL
LIA A
SUSAR ea)
CSET Reroute UALS}
rows in set (8.00 sec)
‘Scanned with CascannerUpdating a record in Table
Syntax to update a record in a Table is-
“mysql> UPDATE SET =
WHERE
Un] oreuCul ee
heer eure
Sunita Pe eeeUCuy ss
Meera
reed
rows in set (8.08 sec)
mysql> update student set age-6 where rol.
Query OK, 1 row affected cM Li et Ted
Rows matched: 1 Changed: 1 Warning: 6
eer Pam CULES A eo =" Lucknow?
Sea ya wea ee met) ely ee ee
Query OK, 2 rows affected (@.@3 sec)
Rows matche: 2 Changed: 2 Warnings:
Scanned with CamSeanerDeletion of a record from a Table
* Syntax to delete a record from a Table is-
voll in (1661,1604)>;
Query OK, 2 rows affected
PS ee secs lies cee
Kanpur
Pb cere ea)
Query OK, 3 rows affec
sql> select * from student;
ea BC
Scanned with CamSeanerAccessing a Table
Syntax to access Data from a table is-
eee
i eeu
alicia LLY
columns and without
a Core ema ec
pee all records.
on tae et)
a r iE
3 ie i
i fer
I Lucknow
e6 Kanpur
CM eee
Inysq1> Select ™ from Student Where city=’B
cc ; a} i Peau
ee ee will display where city
Ursa E eri ss SCL
n set (8.00
‘Scanned with CascannerAccessing a Table
Syntax to access Data from a table is-
Ta Oca Re Pe eS SRRE Ie?
RT AU oer ceele ley
Cie eee RMA earls
Crue
c Roll, Cit
33 Here columns have be
Pe ee mmmct at) ass eee tt]
CM Const
cara iat
CC turns!
cacy mee rte a
Cae cu)
UMRAO
Scanned with CamSeanerDistinct keyword
t DISTINCT City from Student; [| Here DISTINCT keyword is used
to remove duolicacy from city
eee RR Re Ee
EMM gcolc Relea RUT
cel
Viewing Tables in a Database
Scanned with CamSeanerCee
> name like
D3
etry
a)
Pi
Bar
8
banki
lo Cu eA OL MLD
shown.
Scere als ae eraser ae
Bee eisai
CE
Pattern Matching
Wimmera
Peat eae
areal cs
Crecente oad
Peconic eae
‘Scanned with CascannerSyntax for creation of a table from another table is -
mysql>CREATE TABLE
AS (SELECT FROM
WHERE );
ysql> create table bbkscl as