0% found this document useful (0 votes)
111 views15 pages

Class 12 Ip Mysql Assignment No 1

The document is an assignment for Class XII Informatics Practices, detailing SQL commands and their classifications, including Data Definition Language (DDL) and Data Manipulation Language (DML). It provides various SQL query examples for tasks such as inserting, updating, and deleting records in tables, as well as retrieving specific data. Additionally, it covers concepts like cardinality and degree of tables, along with practical SQL commands for different scenarios.

Uploaded by

Shiven X
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views15 pages

Class 12 Ip Mysql Assignment No 1

The document is an assignment for Class XII Informatics Practices, detailing SQL commands and their classifications, including Data Definition Language (DDL) and Data Manipulation Language (DML). It provides various SQL query examples for tasks such as inserting, updating, and deleting records in tables, as well as retrieving specific data. Additionally, it covers concepts like cardinality and degree of tables, along with practical SQL commands for different scenarios.

Uploaded by

Shiven X
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

PIONEER CONVENT SR. SEC.

SCHOOL
CLASS-XII
INFORMATICS PRACTICES
DOA 16th April Assignment- 1 DOS 19th April

Question 1
How are SQL commands classified ?
Answer
SQL commands can be divided into the following categories :

1. Data Definition Language (DDL) Commands


2. Data Manipulation Language (DML) Commands
3. Transaction Control Language (TCL) Commands
4. Session Control Commands
5. System Control Commands

Question 2
Differentiate between DDL and DML commands.
Answer
Data Manipulation
Data Definition Language (DDL)
Language (DML)
DML is a language that
DDL provides a set of definitions to specify the enables users to access or
storage structure and access methods used by manipulate data as organized
the database system. by the appropriate data
model.
DDL commands are used to perform tasks
such as creating, altering, and dropping DML commands are used to
schema objects. They are also used to grant retrieve, insert, delete, modify
and revoke privileges and roles, as well as for data stored in the database.
maintenance commands related to tables.
Examples of DML commands
Examples of DDL commands are CREATE,
are INSERT, UPDATE,
ALTER, DROP, GRANT, ANALYZE etc.
DELETE, SELECT etc.
Question 3
Kabir has created the following table named exam :
RegNo Name Subject Marks
1 Sanya Computer Science 198
2 Sanchay IP 100
3 Vinesh CS 90
4 Sneha IP 99
5 Akshita IP 100
Help him in writing SQL queries to perform the following tasks :
(i) Insert a new record in the table having following values : [6, 'Khushi',
'CS', 85]
(ii) To change the value "IP" to "Informatics Practices" in Subject column.
(iii) To remove the records of those students whose marks are less than
30.
(iv) To add a new column Grade of suitable datatype.
(v) To display records of "Informatics Practices" subject.
Answer
(i)
INSERT INTO exam(RegNo, Name, Subject, Marks)
VALUES(6, 'Khushi', 'CS', 85);
(ii)
UPDATE exam
SET Subject = 'Informatics Practices'
WHERE Subject = 'IP';
(iii)
DELETE FROM EXAM
WHERE MARKS < 30 ;
(iv)
ALTER TABLE EXAM
ADD COLUMN (Grade VARCHAR(1));
(v)
SELECT * FROM EXAM
WHERE Subject = "Informatics Practices";

Question 4(a)
What is the use of UPDATE statement in SQL ? How is it different from
ALTER statement ?
Answer
The UPDATE statement is used to modify existing records in a table. It
specifies the rows to be changed using the WHERE clause, and sets the new
data using the SET keyword. In contrast, the ALTER statement is used to
modify the structure of a table, such as adding, modifying, or deleting
columns, and adding or dropping constraints.

Question 4(b)
Mr. Shankar created a table VEHICLE with 3 rows and 4 columns. He
added 1 more row to it and deleted one column. What is the Cardinality
and Degree of the Table VEHICLE ?
Answer
For the table VEHICLE, the cardinality is 4 and degree is 3.
Explanation — Initially, the table VEHICLE created by Mr. Shankar had 3
rows and 4 columns, with a cardinality of 3 and a degree of 4. After
modifying the table by adding 1 row and deleting 1 column, the new
cardinality of the table VEHICLE is 4 and the degree is 3.

Question 4(c)
Consider the following table named "GYM" with details about fitness
items being sold in the store. Write command of SQL for (i) to (iv).
Table : GYM
ICODE INAME PRICE BRANDNAME
G101 Power Fit Exerciser 20000 Power Gymea
G102 Aquafit Hand Grip 1800 Reliable
G103 Cycle Bike 14000 Ecobike
G104 Protoner Extreme Gym 30000 Coscore
G105 Message Belt 5000 Message Expert
G106 Cross Trainer 13000 GTC Fitness
(i) To display the names of all the items whose name starts with "A".
(ii) To display ICODEs and INAMEs of all items, whose Brandname is
Reliable or Coscore.
(iii) To change the Brandname to "Fit Trend India" of the item, whose
ICODE as "G101".
(iv) Add a new row for new item in GYM with the details :
"G107", "Vibro exerciser", 21000, "GTCFitness"
Answer
(i)
SELECT INAME
FROM GYM
WHERE INAME LIKE 'A%';

Output
+-------------------+
| INAME |
+-------------------+
| Aquafit Hand Grip |
+-------------------+
(ii)
SELECT ICODE, INAME
FROM GYM
WHERE BRANDNAME = "Reliable" OR BRANDNAME = "Coscore";

Output
+-------+----------------------+
| ICODE | INAME |
+-------+----------------------+
| G102 | Aquafit Hand Grip |
| G104 | Protoner Extreme Gym |
+-------+----------------------+
(iii)
UPDATE GYM
SET BRANDNAME = 'Fit Trend India'
WHERE ICODE = 'G101';
(iv)
INSERT INTO GYM
VALUES("G107", "Vibro exerciser", 21000, "GTCFitness");

Question 5(a)
Mr. James created a table CLIENT with 2 rows and 4 columns. He added
2 more rows to it and deleted one column. What is the Cardinality and
Degree of the Table CLIENT ?
Answer
For the table CLIENT, the cardinality is 4 and degree is 3.
Explanation — Initially, the table CLIENT created by Mr. James had 2
rows and 4 columns, with a cardinality of 2 and a degree of 4. After
modifying the table by adding 2 rows and deleting 1 column, the new
cardinality of the table CLIENT is 4 and the degree is 3.

Question 5(b)
Consider the following table FITNESS with details about fitness products
being sold in the store. Write command of SQL for (i) to (iv).
Table : FITNESS
PCODE PNAME PRICE Manufacturer
P1 Treadmill 21000 Coscore
P2 Bike 20000 Aone
P3 Cross Trainer 14000 Reliable
P4 Multi Gym 34000 Coscore
P5 Massage Chair 5500 Regrosene
P6 Belly Vibrator Belt 6500 Ambawya
(i) To display the names of all the products with price more than 20000.
(ii) To display the names of all products by the manufacturer "Aone".
(iii) To change the price data of all the products by applying 25% discount
reduction.
(iv) To add a new row for product with the details :
"P7", "Vibro Exerciser", 28000, "Aone".
Answer
(i)
SELECT PNAME FROM FITNESS WHERE PRICE > 20000;

Output
+-----------+
| PNAME |
+-----------+
| Treadmill |
| Multi Gym |
+-----------+
(ii)
SELECT PNAME
FROM FITNESS
WHERE MANUFACTURER = "Aone";

Output
+-------+
| PNAME |
+-------+
| Bike |
+-------+
(iii)
UPDATE FITNESS
SET PRICE = PRICE * 0.75;
(iv)
INSERT INTO FITNESS
VALUES("P7", "Vibro Exerciser", 28000, "Aone");

Question 6
Write SQL commands for the following on the basis of given table CLUB.
Table : CLUB
COACH_I COACHNAM AG DATOFAP PA SE
SPORTS
D E E P Y X
100
1 KUKREJA 35 KARATE 27/03/1996 M
0
120
2 RAVINA 34 KARATE 20/01/1998 F
0
200
3 KARAN 34 SQUASH 19/02/1998 M
0
BASKETBAL 150
4 TARUN 33 01/01/1998 M
L 0
5 ZUBIN 36 SWIMMING 12/01/1998 750 M
6 KETAKI 36 SWIMMING 24/02/1998 800 F
220
7 ANKITA 39 SQUASH 20/02/1998 F
0
110
8 ZAREEN 37 KARATE 22/02/1998 F
0
9 KUSH 41 SWIMMING 13/01/1998 900 M
BASKETBAL 170
10 SHAILYA 37 19/02/1998 M
L 0
(a) To show all information about the swimming coaches in the club.
(b) To list names of all coaches with their date of appointment
(DATOFAPP) in descending order.
(c) To display a report, showing coachname, pay, age and bonus (15% of
pay) for all the coaches.
Answer
(a)
SELECT *
FROM CLUB
WHERE SPORTS = 'SWIMMING' ;

Output
+----------+-----------+-----+----------+-----+-----+------------
+
| COACH_ID | COACHNAME | AGE | SPORTS | PAY | SEX | DATOFAPP
|
+----------+-----------+-----+----------+-----+-----+------------
+
| 5 | ZUBIN | 36 | SWIMMING | 750 | M | 1998-01-12
|
| 6 | KETAKI | 36 | SWIMMING | 800 | F | 1998-02-24
|
| 9 | KUSH | 41 | SWIMMING | 900 | M | 1998-01-13
|
+----------+-----------+-----+----------+-----+-----+------------
+
(b)
SELECT COACHNAME, DATOFAPP
FROM CLUB
ORDER BY DATOFAPP DESC ;

Output
+-----------+------------+
| COACHNAME | DATOFAPP |
+-----------+------------+
| KETAKI | 1998-02-24 |
| ZAREEN | 1998-02-22 |
| ANKITA | 1998-02-20 |
| KARAN | 1998-02-19 |
| SHAILYA | 1998-02-19 |
| RAVINA | 1998-01-20 |
| KUSH | 1998-01-13 |
| ZUBIN | 1998-01-12 |
| TARUN | 1998-01-01 |
| KUKREJA | 1996-03-27 |
+-----------+------------+
(c)
SELECT COACHNAME, PAY, AGE, (PAY * 0.15) AS BONUS
FROM CLUB ;

Output
+-----------+------+-----+--------+
| COACHNAME | PAY | AGE | BONUS |
+-----------+------+-----+--------+
| KUKREJA | 1000 | 35 | 150.00 |
| RAVINA | 1200 | 34 | 180.00 |
| KARAN | 2000 | 34 | 300.00 |
| TARUN | 1500 | 33 | 225.00 |
| ZUBIN | 750 | 36 | 112.50 |
| KETAKI | 800 | 36 | 120.00 |
| ANKITA | 2200 | 39 | 330.00 |
| ZAREEN | 1100 | 37 | 165.00 |
| KUSH | 900 | 41 | 135.00 |
| SHAILYA | 1700 | 37 | 255.00 |
+-----------+------+-----+--------+

Question 7
Write SQL commands for the following on the basis of given table
STUDENT1.
Table : STUDENT1
No. Name Stipend Stream AvgMark Grade Class
1 Karan 400.00 Medical 78.5 B 12B
2 Divakar 450.00 Commerce 89.2 A 11C
3 Divya 300.00 Commerce 68.6 C 12C
4 Arun 350.00 Humanities 73.1 B 12C
5 Sabina 500.00 Nonmedical 90.6 A 11A
6 John 400.00 Medical 75.4 B 12B
7 Robert 250.00 Humanities 64.4 C 11A
8 Rubina 450.00 Nonmedical 88.5 A 12A
9 Vikas 500.00 Nonmedical 92.0 A 12A
10 Mohan 300.00 Commerce 67.5 C 12C
(a) Select all the Nonmedical stream students from STUDENT1.
(b) List the names of those students who are in class 12 sorted by
Stipend.
(c) List all students sorted by AvgMark in descending order.
Answer
(a)
SELECT *
FROM STUDENT1
WHERE Stream = 'Nonmedical' ;

Output
+-----+--------+---------+------------+---------+-------+-------+
| No. | Name | Stipend | Stream | AvgMark | Grade | Class |
+-----+--------+---------+------------+---------+-------+-------+
| 5 | Sabina | 500 | Nonmedical | 90.6 | A | 11A |
| 8 | Rubina | 450 | Nonmedical | 88.5 | A | 12A |
| 9 | Vikas | 500 | Nonmedical | 92.0 | A | 12A |
+-----+--------+---------+------------+---------+-------+-------+
(b)
SELECT Name
FROM STUDENT1
WHERE Class LIKE '12%'
ORDER BY Stipend ;

Output
+--------+
| Name |
+--------+
| Divya |
| Mohan |
| Arun |
| Karan |
| John |
| Rubina |
| Vikas |
+--------+
(c)
SELECT *
FROM STUDENT1
ORDER BY AvgMark DESC ;

Output
+-----+---------+---------+------------+---------+-------+-------
+
| No. | Name | Stipend | Stream | AvgMark | Grade | Class
|
+-----+---------+---------+------------+---------+-------+-------
+
| 9 | Vikas | 500 | Nonmedical | 92.0 | A | 12A
|
| 5 | Sabina | 500 | Nonmedical | 90.6 | A | 11A
|
| 2 | Divakar | 450 | Commerce | 89.2 | A | 11C
|
| 8 | Rubina | 450 | Nonmedical | 88.5 | A | 12A
|
| 1 | Karan | 400 | Medical | 78.5 | B | 12B
|
| 6 | John | 400 | Medical | 75.4 | B | 12B
|
| 4 | Arun | 350 | Humanities | 73.1 | B | 12C
|
| 3 | Divya | 300 | Commerce | 68.6 | C | 12C
|
| 10 | Mohan | 300 | Commerce | 67.5 | C | 12C
|
| 7 | Robert | 250 | Humanities | 64.4 | C | 11A
|
+-----+---------+---------+------------+---------+-------+-------
+

Question 8
What is foreign key ? How do you define a foreign key in your table ?
Answer
A non-key attribute, whose values are derived from the primary key of
some other table, is known as foreign key in its current table. Defining a
foreign key in a table involves specifying the relationship between the
tables and setting up rules for data integrity. When two tables are related
by a common column or set of columns, the related column(s) in the
parent table (or primary table) should be either declared as a primary key
or unique key. Meanwhile, the related column(s) in the child table (or
related table) should have a foreign key constraint referencing the primary
or unique key in the parent table.

Question 9
How is FOREIGN KEY commands different from PRIMARY KEY
command ?
Answer
The PRIMARY KEY is a set of one or more attributes that can uniquely
identify tuples within the relation. A primary key column cannot contain
NULL values, and it must have unique values for each row. Only one
primary key constraint can exist per table. Conversely, the FOREIGN
KEY command establishes a relationship between two tables by linking a
column or set of columns in one table to the primary key or a unique key
in another table. It enforces referential integrity, ensuring that values in
the foreign key column(s) of the referencing table match values in the
referenced table's primary key or unique key column(s). A foreign key can
allow NULL values, indicating that the relationship is optional. Multiple
foreign key constraints can exist in a table, each referencing a different
parent table.

Question 10
How is FOREIGN KEY commands related to the PRIMARY KEY ?

Answer

FOREIGN KEY commands establish relationships between tables by


linking columns in one table to the PRIMARY KEY or a unique key in
another table. This linkage ensures referential integrity, meaning that
values in the FOREIGN KEY column(s) of the referencing table must
match values in the PRIMARY KEY or unique key column(s) of the
referenced table. Therefore, FOREIGN KEY commands are directly
related to PRIMARY KEY commands as they rely on the unique
identification provided by PRIMARY KEY constraints in other tables.

Question 11
What are table constraints ? What are column constraints ? How are
these two different ?
Answer
Table constraints are rules or conditions applied to an entire table in a
database. They are defined when creating or altering a table's schema.
Column constraints are rules or conditions applied to individual columns
within a database table. They are specified at the column level when
creating or altering a table's schema.
The difference between the two is that column constraints apply only to
individual columns, whereas table constraints apply to groups of one or
more columns.

Question 12
Insert all those records of table Accounts into table Pending where
amt_outstanding is more than 10000.
Answer
INSERT INTO Pending
SELECT * FROM Accounts
WHERE amt_outstanding > 10000;

Question 13
Increase salary of employee records by 10% (table employee).
Answer
Table employee
ID First_Name Last_Name User_ID Salary
1 Dim Joseph Jdim 5000
2 Jaganath Mishra jnmishra 4000
3 Siddharth Mishra smishra 8000
4 Shankar Giri sgiri 7000
5 Gautam Buddha bgautam 2000
UPDATE employee
SET Salary = (Salary * 0.1) + Salary ;

Output
To view all the details (all columns and rows) of the "employee" table the
below query is executed :
SELECT * FROM employee ;
+----+------------+-----------+----------+--------+
| ID | First_Name | Last_Name | User_ID | Salary |
+----+------------+-----------+----------+--------+
| 1 | Dim | Joseph | Jdim | 5500 |
| 2 | Jaganath | Mishra | jnmishra | 4400 |
| 3 | Siddharth | Mishra | smishra | 8800 |
| 4 | Shankar | Giri | sgiri | 7700 |
| 5 | Gautam | Buddha | bgautam | 2200 |
+----+------------+-----------+----------+--------+

Question 14
Add a constraint (NN-Grade) in table Empl (given before assignment) that
declares column Grade not null.
Answer
ALTER TABLE Empl
ADD CONSTRAINT NN_Grade
(Grade NOT NULL) ;

Question 15
Drop the table Empl.
Answer
DROP TABLE IF EXISTS Empl;

Question 16(i)
Differentiate between DROP TABLE, DROP DATABASE.
Answer
DROP TABLE DROP DATABASE
This command is used to delete a This command is used to delete an
specific table from the database entire database including all its tables,
along with all its data, indexes, views, stored procedures, triggers, and
triggers, and constraints. other objects.
The syntax is : DROP TABLE The syntax is : DROP DATABASE
table_name;. database_name;.

Question 16(ii)
Differentiate between DROP TABLE, DROP clause of ALTER TABLE.
Answer
DROP TABLE DROP clause of ALTER TABLE
This command is used to delete a
This command is used to remove a
specific table from the database along
specific component of a table, such
with all its data, indexes, triggers, and
as columns, constraints, or indexes.
constraints.
The syntax is : ALTER TABLE
The syntax is : DROP TABLE
table_name DROP COLUMN
table_name;
column_name;

Question 17
Mr. Mittal is using a table with following columns :
Name, Class, Stream_Id, Stream_name
He needs to display names of students who have not been assigned any
stream or have been assigned stream_name that ends with "computers".
He wrote the following command, which did not give the desired result.
SELECT Name, Class FROM Students
WHERE Stream_name = NULL OR Stream_name = "%computers" ;
Help Mr. Mittal to run the query by removing the error and write correct
query.
Answer
The error in Mr. Mittal's original query lies in using "= NULL" instead of "IS
NULL" to check for NULL values and using = '%computers' instead of
"LIKE '%computers'" for pattern matching.
The correct query is:
SELECT Name
FROM Students
WHERE Stream_name IS NULL OR Stream_name LIKE '%computers';

Question 18
The Doc_name Column of a table Hospital is given below :
Doc_name
Avinash
Hariharan
Vinayak
Deepak
Sanjeev
Based on the information, find the output of the following queries :
(i) SELECT doc_name FROM HOSPITAL WHERE Doc_name like "%v";
(ii) SELECT doc_name FROM HOSPITAL WHERE doc_name like "%e
%";
Answer
(i) SELECT doc_name FROM HOSPITAL WHERE Doc_name like "%v";

Output
+----------+
| doc_name |
+----------+
| Sanjeev |
+----------+

Explanation
The query SELECT doc_name FROM HOSPITAL WHERE Doc_name like "%v"; selects
the doc_name from the HOSPITAL table where the Doc_name column ends with
the letter "v" using the LIKE operator with the "%" wildcard. This pattern
matches any string where "v" is the last character, and any characters
can precede it.
(ii) SELECT doc_name FROM HOSPITAL WHERE doc_name like "%e%";

Output
+----------+
| doc_name |
+----------+
| Deepak |
| Sanjeev |
+----------+

Explanation
The query SELECT doc_name FROM HOSPITAL WHERE doc_name like "%e%"; selects
the doc_name from the HOSPITAL table where the doc_name column contains the
letter "e". This is achieved using the LIKE operator with the "%" wildcard
before and after the letter "e", which matches any sequence of characters
that have "e" in them.

Question 19
Sarthak, a student of class XII, created a table "Class". Grade is one of
the columns of this table. To find the details of students whose Grades
have not been entered, he wrote the following MySql query, which did not
give the desired result:
SELECT * FROM Class WHERE Grade = "Null";
Help Sarthak to run the query by removing the errors from the query and
write the correct query.
Answer
The error in Sarthak's code is that he should use the IS NULL comparison
instead of = "Null" because the correct syntax to check for NULL values in
SQL is to use the IS NULL operator.
The correct query is:
SELECT * FROM Class WHERE Grade IS NULL;

Question 20
What is the purpose of ALTER TABLE command ? Can you add new
columns with constraints such as NOT NULL ? Give example to justify
your answer.
Answer
The ALTER TABLE command is used to change definitions of existing tables.
It is used to add columns, integrity constraints and redefine a column
(datatype, size, default value) in a table.
Yes, we can add new columns with constraints such as NOT NULL, which
ensures that a column must always contain a value (i.e., cannot be empty
or null).
For example, to add a new column tel_number with the NOT NULL
constraint in the Empl table, the statement is:
ALTER TABLE Empl
ADD COLUMN(tel_number integer NOT NULL);
Question 21
What is the purpose of DROP TABLE command in MySql ? How is it
different from DELETE command ?
Answer
The DROP TABLE command in MySql is used to permanently delete an entire
table from the database, including its structure, data, indexes, triggers,
and constraints. The DELETE command, on the other hand, is used to
remove specific rows or all rows from a table, leaving the table structure
intact.

You might also like