9) 11th LESSON 11structured Query Langauge
9) 11th LESSON 11structured Query Langauge
STRUCTURED QUERY
LANGUAGE
STD -11
INFORMATICS PRACTICES(13 T H EDITION SUMITA
ARORA)
I) CREATING A DATABASE-topic 11.4-DataBases in Mysql 11.4+11.7 Show databases like<‘database name’>
Create database <database name>
VI) CREATING A TABLE- Show databases like ‘world’;
Eg-> Create database 11b; Create table tablename Without ‘’ error
Or (colname dt,
Create database [if not exists] <database name> Colname dt, int/integer default size 11
Eg-> Create database if not exists 11b; Colname dt);
Mysql tables
are not sorted
ITEM TABLE by default
curdate()
or
current_date()
or
current_date
Note- If any value is null then the result is null.
You can make use of -*+/ or % for calculations.
6) USING COLUMN ALIAS-
I,e, giving different temporary names to the columns of the table is called ‘column alias’ for output purpose only.
Prog10-WAQ to display the dop and custno with the column name as Date of Purchase and Customer Number as
column names-
Note- When the new col name is more than 1 word, it has to be
included in quotes (double quotes or single) or else if the new
column name is only one word no need of quotes.
Giving column alias to an expression-
Prog-11-select 22/7 as “pi”;
Eg-
7) HANDLING NULLS-
An empty value is represented as NULL in a table.
Prog12-
10)WHERE CLAUSE-
Prog17-<itemname> has the price<price*100>% Till now we were putting conditions on which columns you
wanted to see now lets put condition on rows.
OR
If both 1 and 2 are present then
both the results will be displayed.
OR
Prog25- WAQ to display all the details from the item table where the itemno is less than 3 and the price is less than 20.
AND
OR
Prog26-WAQ to display the details for itemno1 and 2
If you put the condition with and then the query will not work since there is no record with itemno1 and 2.So even
if the query says ‘and’ you have to understand it is ‘or’. This problem comes when we are referring to the same
column again.
Prog27-WAQ to display itemname, itemno and price for all the items where the itemno is not 5
NOT
Or where itemno!=5;
Or where itemno<>5;
Rem the NOT logical operator comes before the column name
Prog28-WAQ to display all the details for items other than pen
where itemname<>’pen’;
Or
where itemname!=‘pen’;
Prog29-WAQ to display the details from the item table where the item no is 1 or 2 and the price is greater than 19.
Not
And
or
11) CONDITION BASED ON RANGE- BETWEEN
NOT BETWEEN
For conditions based on ranges we make use of between and not between operators-
Prog30-WAQ to display all details from the item table where the item numbers are between 1 and 3
Prog31-WAQ to display all the details for the items not having itemnos between 1 and 3
Prog32-WAQ to display details of items whose price ranges between 10 to 20
Or
Prog34-WAQ to display details of items which are not pen and pencil.
Prog35- WAQ to display details for items with numbers 1,3 or 5
Prog39-WAP to display details of items whose names do not begin with the letter ‘p’
Prog40-WAP to display all the item names starting with ‘wx%ab’
optional
Prog47- WAP to display all the items sorted by itemnames but only for items whose price is more than 20
Eg2
SORTING WITH COLUMN ALIAS
Another
Prog50- eg
or
Prog19-wap to display dept details of deptnos ranging from 20 to 40 Prog22-same as 21 but with the not
Prog21-Display the deptno of departments ACC, Sales, Adv, adm using IN clause.
Prog24-WAP to display dept details
from the dept table whose dnames
are made up of exactly 3 characters
Prog25-WAQ to display all dnames containing the letter ‘a’ .
Prog28-WAQ to display loc sorted in ASC order
Prog32-Create a copy of the table dept called deptnew but for deptnos 20 and 30 only
x----------------x
Prog 33- Create a view called v1 from dept having deptno 1 in it. End of practicals
IMPORTANT POINTS AND PRGRAMS FROM THE TOPIC COVERED SO FAR FROM THE TEXT BOOK(11.4+11.7)
Prog1-Write SQL commands to check if a database by the name newdb exists on Mysql.If not then create a new
database by this name.
Note-Before issuing a create table command, make sure that its parent database has been opened using
Use<database name> command Prog3-Display species of all pets
from table pet
Note-The order of the selection determines the order of display. Select all species from pet;
Note-distinct and all both together cannot be used in the same select query.
Note-To perform calculations may either write expression with select or use table dual(in from clause),which is a
one row.one column dummy table, provided by mysql.
Prog4--Display the name age and marks(aggregate) of students whose age is greater than or equal to 16 from the
table student
Select name,age,aggregate
From student
Where age>=16;
Prog5-WAQ to display all the details from pet table for species cat/dog having gender as male;
x---------------x
STRUCTURED QUERY LANGUAGE-(11.1+11.2)
I) INTRODUCTION TOPIC-11.1 Pg 355
II) SOME MYSQL SQL ELEMENTS- TOPIC -11.2 Pg 355
◦ i literals
◦ ii datatypes
◦ iii nulls
◦ iv comments
To make an
unsigned int in
mysql
Float(10,2)
Double(16,4)
Decimal(10,0)
Char varchar
1.Fixed length string 1.Variable length string
If u give the length of 10 If u give a length of 10
and enter only 5 and enter only 5
characters the remaining characters the remaining
length will be filled up by length is freed off in
spaces. memory
2.Default length is 1 2. It is mandatory to
specify a length else you
get an error
x------------x
End of topic
Topic 11.6-INSERTING DATA IN TABLES-
TOPIC-11.5(CREATING TABLES)
DATA INTEGRITY THROUGH CONSTRAINTS
Def- Constraint-A constraint is a condition or check applicable on a field or set of fields. The constraints applied to
maintain data integrity are also known as integrity constraints.
After enabling an integrity constraint all the data entering is checked if it satisfies the constraint or not. If it does
it is allowed to enter else not.
Two Basic types of constraints are-
(i)Column constraints(applicable to one column)
(ii)Table constraints(applicable to groups)
Try entering duplicate values in the stud table it will give an error
2) COMPOSITE PRIMARY KEY- (TABLE LEVEL) This constraint does not allow the combinations of the composite
key columns to repeat.
Prog-2 Make a table stud with rollno and name as a composite key
1. Default with PK
works only once
2. 1 col can have only
1 default value
3. If no default and a
not null constraint
then – entry to be
done becomes
mandatory.
Prog5-WAQ to make the stud table with rollno and name and assign a unique constraint to the name column.
Column level desc stud Table level
What will happen with the following query-
Create table stud This will behave separately
(rollno int, Create table stud
name char(20) unique not null, (rollno int,
marks int); Name char(20),
Don’t put a comma between 2 constraints in column level
Unique(rollno),
Unique(name));
Now the above name column will behave like a PK
Prog 6-WAQ to make the stud table and assign a unique constraint to rollno and name of the stud table.
Column level Table level
When u assign a unique constraint
to more than 1 column as a table
level constraint, then it starts to
behave like a composite key but
allowing nulls.
This method is preferred
(6) CHECK CONSTRAINT- BOTH
This constraint limits the values that can be inserted into a column of a table.(puts checks on cols)
Prog7- Make a stud table with rollno and name and the rollno should be greater than 10;
Column level
Error for rollno =10
Table level
Create table stud error
(rollno int,
Name char(10),
grno int check (grno>rollno));
Def-FK-A Fk is a non key which gets its value from the PK of another table. It enforces referential integrity.
Def Referential Integrity- It is a system of rules that a DBMS uses to ensure that relationship between records in
a table are valid and users don’t accidently delete or update any related data.
Before putting a FK
-Both the tables should be in the same database
-Both cols should have the same(similar) dt.
U cannot assign FK without it being the
-A FK of a child table must refer to the PK or unique of a Parent table
PK or unique key in the parent table
-Names may be different.
Links FK
Table level
create table order1
(ordno int, Column level
qty int,
i_no int,
primary key(ordno),
foreign key(i_no) references item1(itemno));
Error cause
itemno-3
does not
exist in
the
item table
*FK works only as a table level constraint
*If given as column level, it will not display an error message but will not check the referential integrity.
FOREIGN KEY
STUDENT TEACHER
Pk FK
Rollno name Tid rollno
1 aaa 11 1
2 naa 12 1
13 2
Prog10- Create both the tables above and make a FK at rollno of teacher table such that if u delete or modify a
PK value ,it should get modified or deleted in the FK .
Now update an entry in the rollno column of the student table, you will automatically see the change in the
teacher table. CASCADE is called a referencing action
On delete cascade Other referencing actions-
On update cascade RESTRICT-rejects the operation
NO ACTION-not allow deletion or updation of
the PK as long as there is a related record in the
FK. (same as not writing)
SET NULL-will set null in the FK
x-----------------x
End of topic
Prog16-
Pg 368
Prog17-
Prog18-
Pg 368
Show create table item;
Prog20-
TOPIC- 11.8 MORE DML COMMANDS- 1)insert
2)update
2)UPDATE COMMAND 3)delete
NULL
Prog2- Add a column commission to the emp table with default value as 0.
Prog3-Add a new col as doj with a default date as ‘1999-02-02’ Default value has come to 0
Default
value
Prog4-Add a col salary with a not null constraint
If u add a col with a not null constraint and also don’t give it a default value then it takes the value according to the dts
Eg- int 0
Char/varcharspace
Date0000-00-00
Time00:00:00
Prog5-WAP to add a col address with a not null constraint
Prog3-Add a not null constraint to enamewill work if the ename col has no null values
Prog4- Add a default constraint to the job col of the emp table as clerk;
Note-PK will be removed only if there is not FK attached to it. You do not need the
name of the PK to delete it since there is only 1 pk in a table.
drop primary key cascade;
Prog5-Even if not null and default key have no name then also we can remove them by redefining them.
Remove a not null constraint from ecode col .
Prog6-Drop the FK from the table shop which links its employee number to the empno of the emp table-
Alter table shop
Drop foreign key F1;
DROP TABLE COMMAND-
Prog7- Delete the entire table try with its data.
or
x---------x
End of topic
IMPORTANT POINTS IN THE TB RELATED TO THE TOPIC 11 .9
Prog1-WAQ to add 2 cols name1 and name2 and also give a check constraint to col name2 that
it should contain only ‘aaa’ and ‘bbb’ as names.(in one query)
OR
No need of bracket ( )
Prog4-Eg23pg391
Prog5-Eg24pg391
Don’t
use the
bracket
with
modify
Prog6-eg25-pg392
Alter commission drop default;
Prog7-eg26-pg392
Prog8-eg27-pg392
Prog9-eg28-pg392
Note-Mysql will OR
reject the FK if its alter table sales Now() is static and
database engine is Modify commission int default 1000; Sysdate() is dynamic
not INNODB.
So your query
involving a FK
constraint may be
rejected also.
Prog10-eg29pg393
Prog11-eg30pg393
Prog12-eg31pg393
Or
Alter table sales x------------x
Modify commission int; End of lesson
ASSIGNMENTS
MCQ PG 325
New-pg 395
T
New page 411
sports
sports
sports
,dop
or
Not in syllabus