MySql Notes For The Class XI and XII 2022-23
MySql Notes For The Class XI and XII 2022-23
Field : It is a collection of the data with same type. The region where the data is
kept called as field. In simple words it is also known as column. Technically it is
called as attribute.
an institution.
OR
Q What do you mean by Key in MySql? Define various types of keys available.
Ans Key: a field with any specialty is called as key. OR a column which is used to
identify one or more rows in a table.
Primary Key: A field which contains values without repetition. It has capacity to
hold / uniquely identify record.
Candidate Key: The fields which have willingness to be primary key, called as
candidate key.
Alternate Key: Except primary key From the pool of candidate keys, all the
remaining candidate keys are called as alternate key(s).
Foreign Key: The field which is non-primary key in the first table, but acting as a
primary key in another table, called as Foreign key. ( non-primary key of first
table)
DDL: It stands for data definition language. It has a set of commands which is
used to create / delete / modify [change or edit] the structure of the table.
eg:
Create Table, Drop Table, Alter Table etc.
DML: It stands for data manipulation language. It has a set of commands which
is used to insert / remove [erase or eliminate or delete] / modify / search / sort
[arrange or order] the records [i.e. actual data].
eg:
Select, Insert, Update, Delete etc
Note:
- It is a non case sensitive language.
- Every query ends with semi-colon.
- The name of the database / table name should not contain any special
Note:
> Table name should not contain any special character.
> The last field should not carry comma
int(w)
or - to hold integer values
integer(w)
decimal(w,d)
or - to hold real / decimal values
float(w,d)
Q Create a table Student with following fields and respective data type
rollno – int
name – char
gender – char
city – char
dob – date
class – int
section – char
per - decimal (5,2)
Ans create table student
(
rollno int(2),
name char(15),
gender char,
city char(15),
dob date,
class int(2),
section char,
per decimal(5,2)
);
Note: If the data type char is not mentioned with any width, then by default it
will hold 1 character.
Note:
> All the char / varchar / date type based field - data will be enclosed with
double quotes or single quotes.
values(26,"Suresh","m",'Dungarpur',"2001-08-1","12",null,77.14);
Q Display the names with their percentage of those students who have scored
less than 50%.
Ans Select name, per
from student
where per<50;
Q Display the names along with date of birth of those students whose dob is
before 1st April 2005.
Ans Select name, dob
from student
where dob<'2005-04-01';
Q Display names as well as dob of all those female students who belongs to
Udaipur.
Ans select name, dob
from student
where gender="f" and city="Udaipur";
Q Show names along with percentage and city of those students who either
belongs to other than Udaipur city or whose per is more than 50.
Ans select name, per, city
from student
where city<>"Udaipur" or per>50;
select *
from student
where per between 50 and 70 ;
OR
select *
from student
where not (per>=50 and per<=70);
OR
select *
from student
where per not between 50 and 70 ;
OR
select *
from student
where name in ("sunil","Rajesh","payal");
Q Display details of all the students except Sunil, Rajesh and Payal.
Ans select *
from student
where not (name = "Sunil " or name="Rajesh" or name="payal" );
OR
where name != "sunil " or name!="Rajesh" or name!="payal";
OR
where name not in ("sunil","Rajesh","payal");
Q Display Complete Information Of All The Students Except Roll Number 35, 65,
82.
Ans select *
from student
where not (rollno= 35 or rollno=65 or rollno=82 );
OR
where rollno != 35 or rollno!=65 or rollno!=82;
OR
where rollno not in (35,65,82);
Q Display names of cities without any duplication. or remove the duplicate
names of cities.
from student;
Note: the distinct keyword is used to remove duplicate values.
Q Display new roll numbers with their name along with class of all the students.
The new roll number can be finding out by addition of 100 to each roll
number.
Ans select rollno+100, name, class
from student ;
Or
select rollno+100 as "New Roll No." , name, class
from student;
Q Display the name of the student with their class and roll number whose name
starts with "A" and ends with "Kar".
Ans select name, class, rollno
from student
where name = "A%kar";
Note: the above query is wrong. in spite of = sign, like keyword must be
used(during pattern matching)
select name, class, rollno
from student
where name like "A%kar";
Q Display names of all the students with their city and percentage in descending
manner of percentage.
Ans select name, city, per
from student
order by per desc;
Q Modify the records of all the students by adding 100 to each roll number.
Ans update student
set rollno = rollno + 100 ;
Q shop (table)
itemname - biscuit, chocolates, maggie, ...
itemcost - xx, xxx, xxx
Modify the itemcost of all the biscuit type of products with addition of 5%.
Ans update shop
set itemcost = itemcost + itemcost * 5 / 100
where itemname = "biscuit";
2. max() : it will display / return maximum value from the group of values.
eg:
select max(per)
from student;
3. min() : it will display / return minimum value from the group of values.
eg:
select min(per)
from student;
5. count() : this function display / returns no. of records available in the table.
Sample1
------------
val1 val2
1 6
null 2
3 4
8 0
select count(*)
from student;
select count(rollno)
from student;
Note: it will count no. of records specified in the field excluding null.
example:
select publisher
from book;
Q.
ans.
Q what will happen when the following query will be got executed?
select name, avg(per)
from student;
select ucase(name)
from student;
2. lower() / lcase() : this function returns / displays the name in lower case
letters.
select lower(name)
from student;
----------------------------------------------------------------------------------
mysql> select 45*3 ;
+------+
| 45*3 |
+------+
| 135 |
+------+
1 row in set (0.00 sec)
| 135 |
+------+
1 row in set (0.00 sec)
eg:
select mid("informatics",3, 4);
o/p: form
Note: here 3 is position where to begin the operation, and 4 is no. of characters
to be extracted.
Note: in case 4(3rd argument) is not mentioned, then all the characters will be
extracted ffrom the beginning(3).
4. instr() : it will return the position of the serached string from the in / source
string.
eg:
select instr("this is a bus","is");
o/p : 3
select concat("dhawal","jain");
6. length() : it will return no. of characters (including spaces & special chars.)
from the existing string.
eg:
select length("yuvraj");
o/p : 6
7. ltrim() : it will remove all the leading / left-most spaces of the string
.
8. rtrim() : it will remove all the trailing / right-most spaces of the string
.
9. trim() : it will remove all the leading as well as trailing spaces of the string. It
will not remove the spaces in-between.
10. left() : it will extract number of characters from the left most side of the
string.
eg:
select left("ravi",2);
o/p : ra
11. right() : it will extract number of characters from the right most side of the
string.
eg:
select right("ravi",2);
o/p : vi
- primary key : it will make sure that repeated values should not be entered
it will be used only once (allowed for only one field) in a table
- not null : none of the data will be left blank for the respective field
it can be applied for more than 1 field
Q Change the data type decimal to int for the column bprice in the table book.
Ans alter table book
modify bprice int(4);
Q Put the constraint not null to the column name of student table.
select name,per
from student
order by per desc , name ;
sumit - 77
aman - 45
nilesh - 78
sumit - 82
raj - 77
payal - 36
sumit - 12
rajesh - 77
----------------------------------------
sumit - 82
nilesh - 78
raj - 77
rajesh - 77
sumit - 77
...
Q what is null?
Ans NULL means a value that is unavailable, unassigned, unknown or inapplicable.
NULL is not the same as zero or a space or any other character. In a table NULL
is searched for using IS NULL or IS NOT NULL keywords.
Q What is drop?
Ans This command removes records as well as structure of the table. This belongs
to DDL category.
Whereas delete command removes only records not structure. The command
delete belongs to DML category.
Rollback: it means transaction has not been finished completely and hence all
data changes made by the transaction in the database are undone. OR this
command undoes the changes made during the transaction execution.
Savepoint: it is a point in a transaction, uptil which all changes have been saved
permanently. Basically it creates history.
Q What is Join?
Ans A join is a query that combines rows from two or more tables. There are
basically two types
Cross-Join: it is also called as Cartesian product. It is a basic type of join that
simply matches each row from one table to every row from another table. OR it
is a joining of two tables without any condition. It forms a table which consist
multiplication of rows of and addition of columns from both the tables.
eg:
select *
from table1, table2;
MONTH(date) : Returns the numeric month from the date passed, in the range
0 to 12.
Eg: SELECT MONTH('2010-02-26');
Eg: SELECT MONTH(curdate());
YEAR(date) : Returns the year for date passed in the range 0 to 9999. Returns
values like 1998, 2010,1996 and so on.
Eg: SELECT YEAR('2010-02-26');
Eg: SELECT YEAR(DOB)from student;
DAYNAME(date) : it returns the name of the day for the date passed
Eg: SELECT YEAR('2009-07-21');
Result: Tuesday
DAYOFYEAR(date) : Return the day of the year for the given date in numeric
format in the range 1 to 366.
Eg: SELECT DAYOFYEAR('2009-07-21');
Result: 202
Eg: SELECT DAYOFYEAR('2009-01-01');
Result: 1
Q
Ans
Ans
Ans
Ans
* The names of clauses are: DISTINCT, ORDER BY, GROUP BY, WHERE,
* The names of operators are: BETWEEN, IN
* % AND _ are two wild card characters.
* NULLs in aggregate functions totally ignore null values present in a field.
* Min(), Max() and Count() work on any type of values-Numeric, Date and String
AVG() and SUM() work on only numeric values (Int and Decimal)
* When AutoCommit is ON [SET AUTOCOMMIT =1;] each statement during
transaction automatically committed.