0% found this document useful (0 votes)
39 views10 pages

DBMS Recap

The document provides an overview of key concepts in DBMS including cardinality, degree, primary key, foreign key, NOT NULL, DISTINCT, GROUP BY, aggregate functions, HAVING clause, data definition language commands, data manipulation language commands, data control language commands, transaction control language commands, INSERT, UPDATE, DELETE, SELECT, ALTER, DROP, JOIN types, and ambiguous errors.

Uploaded by

thiviyan242007
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)
39 views10 pages

DBMS Recap

The document provides an overview of key concepts in DBMS including cardinality, degree, primary key, foreign key, NOT NULL, DISTINCT, GROUP BY, aggregate functions, HAVING clause, data definition language commands, data manipulation language commands, data control language commands, transaction control language commands, INSERT, UPDATE, DELETE, SELECT, ALTER, DROP, JOIN types, and ambiguous errors.

Uploaded by

thiviyan242007
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/ 10

DBMS Recap (20 Marks)

1. Cardinality and degree:


Cardinality – no. of rows
Degree – no. of columns
2. Primary Key – Unique and not null
3. Foreign Key – Refers another tables Primary Key
4. NOT NULL – ensures that the value should be empty
5. DISTINCT – avoids duplication / repetition [write the answers as per the row order]
Select distinct (city) from employee; [row-wise answers]
Select count(distinct(city)) from employee; [single numeric data – integer]
6. Group by: [each / wise – 99%] [always in ascending order based on the group by
column]
There should at-least one aggregate function proceeded by a group by column name
Max(), Min(), Sum(), Avg() and Count().
Group – for condition u have to use only Having clause and not where clause
mysql> select supid,min(price),max(price),sum(price),avg(price),count(*) from
product
-> group by
-> supid;
+-------+------------+------------+------------+------------+----------+
| supid | min(price) | max(price) | sum(price) | avg(price) | count(*) |
+-------+------------+------------+------------+------------+----------+
| 111 | 25 | 29 | 133 | 26.6000 | 5|
| 222 | 5| 20 | 35 | 11.6667 | 3|
| 333 | 10 | 50 | 70 | 23.3333 | 3|
+-------+------------+------------+------------+------------+----------+
7. Show databases; [List of databases available]
8. Show tables; [List of tables available]
9. Use databasename
10. Desc tablename [structure of the table]
11. Command Classification:
DDL: CREATE, ALTER, DROP
DML: INSERT, UPDATE, DELETE, SELECT
DCL: GRANT AND REVOKE
TCL: COMMIT, ROLLBACK AND SAVEPOINT

mysql> desc product;


+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| pno | int(3) | YES | | NULL | |
| pname | varchar(25) | YES | | NULL | |
| price | int(2) | YES | | NULL | |
| supid | int(3) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
12. Insert command: [quotes – char, varchar,date]
Type 01: (when all the column values are present)
Into into Product values
(117,’abc’,10,111);

Type 02: (when certain columns are given)


Assume – (pno,price,supid – is given)
Insert into Product
(pno,price,supid)
Values
(118,25,222);
13. Update: [increase /decrease/ change a column value]
Increase the salary by 10%
Add 5 marks to a subject
Change the name of a person
Change the group of a person
Type 01: [for all employees / students / products – without where clause]
Update Product
Set
Price = Price + Price * 10/100;

Type 02:[with where clause – for certain students / employees / products]


Update Product
Set
Price = Price + Price * 10/100
Where
Supid = 222;

14. Delete: [delete the values] – Deletes a row / specific rows / all the rows but not a
specific column – [structure is not getting affected]
Type 01:[removing the all the row values]
Delete from Product;

Type 02:[removes only certain row/row(s)]


Delete from Product
Where
Pno > 111;
15. Usage of null and is not null [Select command - 1 mark]

[Correct statement – write command]


mysql> select * from productduplicate
-> where
-> pname is null;

[Statement given in the paper - wrong]


mysql> select * from productduplicate
-> where
-> pname = null;

16. Drop: [Structure with the data (if present) allows gets deleted] – [1 mark]
Drop table tablename;
Drop database databasename;

17. Alter [v.v. important]


Type 01: [Adding a new column]
mysql> alter table product
-> add
-> mobile bigint;

Type 02: [Dropping an existing column – remember datatype won’t occur]


mysql> alter table product
-> drop
-> mobile;

Type 03: [Increase or decrease the size of a column]


mysql> alter table product
-> modify
-> pname varchar(30);
18. Select Command: [important variations]
a. Year of joining = 2020
Month of joining = March
Select * from employee
Where
Year(doj) = ‘2020’; [quotes is must – check the table ensure that the date is in ‘yyyy-
mm-dd’]

Select * from employee


Where
month(doj) = ‘03’;
b. Changing the column name in display alone [alias name – this doesn’t change the
original column name – only meant for display purpose]

mysql> select rollno,name,gender,fees * 12 "Annual Fees"


-> from
-> strain;
+--------+---------+--------+-------------+
| rollno | name | gender | Annual Fees |
+--------+---------+--------+-------------+
| 100 | SUNDAR | M | 1200000 |
| 101 | SUNDARI | F | 1200000 |
| 108 | NULL | F | 3600000 |
| 109 | NULL | F | 3600000 |
| 119 | NULL | F | 3600000 |
| 911 | NULL | F | 3000000 |
+--------+---------+--------+-------------+
6 rows in set (0.00 sec)

mysql> desc strain;


+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| ROLLNO | int(3) | NO | PRI | NULL | |
| NAME | varchar(25) | YES | | NULL | |
| GENDER | char(1) | YES | | F | |
| FEES | int(6) | NO | | NULL | |
| MOBILE | bigint(20) | YES | UNI | NULL | |
+--------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)

Cartesian Product: [no common column – output – no.of rows in table 01 * no.of rows in
table 02]

mysql> select * from t1;


+------+------+
| name | age |
+------+------+
| Raja | 20 |
| Kuja | 25 |
+------+------+
2 rows in set (0.00 sec)

mysql> select * from t2;


+--------+
| gender |
+--------+ t2
| Male |
| Female |
+--------+ t1
2 rows in set (0.00 sec)

mysql> select * from t1,t2; //Cartesian Product


+------+------+--------+
| name | age | gender |
+------+------+--------+
| Raja | 20 | Male |
| Kuja | 25 | Male |
| Raja | 20 | Female |
| Kuja | 25 | Female |
+------+------+--------+
4 rows in set (0.00 sec)

Natural join: [no need to give and condition – common column appears only once that too
at the front]
mysql> select * from supplier;
+-------+----------+---------+
| supid | sname | city |
+-------+----------+---------+
| 111 | s and co | mumbai |
| 333 | ttt | chennai |
+-------+----------+---------+
2 rows in set (0.00 sec)

mysql> select * from product;


+------+---------+-------+-------+
| pno | pname | price | supid |
+------+---------+-------+-------+
| 102 | pencil | 20 | 333 |
| 104 | NULL | 100 | 333 |
| 105 | NIRMA | 58 | 111 |
| 105 | NEEM | 58 | 111 |
| 107 | Divider | 20 | 333 |
+------+---------+-------+-------+
5 rows in set (0.00 sec)

mysql> select * from supplier natural join product;


+-------+----------+---------+------+---------+-------+
| supid | sname | city | pno | pname | price |
+-------+----------+---------+------+---------+-------+
| 333 | ttt | chennai | 102 | pencil | 20 |
| 333 | ttt | chennai | 104 | NULL | 100 |
| 111 | s and co | mumbai | 105 | NIRMA | 58 |
| 111 | s and co | mumbai | 105 | NEEM | 58 |
| 333 | ttt | chennai | 107 | Divider | 20 |
+-------+----------+---------+------+---------+-------+
5 rows in set (0.00 sec)

Equi join:

mysql> select * from product p,supplier s


-> where
-> p.supid = s.supid;
+------+---------+-------+-------+-------+----------+---------+
| pno | pname | price | supid | supid | sname | city |
+------+---------+-------+-------+-------+----------+---------+
| 102 | pencil | 20 | 333 | 333 | ttt | chennai |
| 104 | NULL | 100 | 333 | 333 | ttt | chennai |
| 105 | NIRMA | 58 | 111 | 111 | s and co | mumbai |
| 105 | NEEM | 58 | 111 | 111 | s and co | mumbai |
| 107 | Divider | 20 | 333 | 333 | ttt | chennai |
+------+---------+-------+-------+-------+----------+---------+
5 rows in set (0.00 sec)

mysql> select * from supplier s,product p


-> where
-> s.supid = p.supid;
+-------+----------+---------+------+---------+-------+-------+
| supid | sname | city | pno | pname | price | supid |
+-------+----------+---------+------+---------+-------+-------+
| 333 | ttt | chennai | 102 | pencil | 20 | 333 |
| 333 | ttt | chennai | 104 | NULL | 100 | 333 |
| 111 | s and co | mumbai | 105 | NIRMA | 58 | 111 |
| 111 | s and co | mumbai | 105 | NEEM | 58 | 111 |
| 333 | ttt | chennai | 107 | Divider | 20 | 333 |
+-------+----------+---------+------+---------+-------+-------+
5 rows in set (0.00 sec)
Keys:

For Example:
Employee(Empid#,Ename,Salary,Aadhaar#,Passport#,VoterID#)

Primary Key – Empid [Candidate Keys – Alternate Keys]


Candidate keys – Empid, Aadhaar, Passport, VoterID [Primary Key + Alternate Keys]
Alternate Keys – Aadhaar, Passport, VoterID [Candidate Keys – Primary Key]

Ambiguous Error
mysql> create table Orders
-> (Oid int(1), Ono int(5), Cid int(1));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into Orders


-> values
-> (1,77890,1),
-> (2,23653,2);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> create table Cust


-> (Cid int(1), Lastname varchar(20),Fname varchar(20), age int(2));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into Cust


-> values
-> (1,'KIRAN','KUMAR',70),
-> (2,'VINOD','MEHRA',45);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> SELECT CID,ONO FROM CUST C,ORDERS O


-> WHERE
-> C.CID = O.CID;
ERROR 1052 (23000): Column 'CID' in field list is ambiguous
After Rectification:

mysql> SELECT C.CID,ONO FROM CUST C,ORDERS O


-> WHERE
-> C.CID = O.CID;
+------+-------+
| CID | ONO |
+------+-------+
| 1 | 77890 |
| 2 | 23653 |
+------+-------+
2 rows in set (0.00 sec)

Tablename.columnname vs Table alias.columname

mysql> select Lastname,Ono from cust,orders


-> where
-> cust.cid = orders.cid;
+----------+-------+
| Lastname | Ono |
+----------+-------+
| KIRAN | 77890 |
| VINOD | 23653 |
+----------+-------+
2 rows in set (0.00 sec)

mysql> select c.Lastname,o.Ono from cust c,orders o


-> where
-> c.cid = o.cid;
+----------+-------+
| Lastname | Ono |
+----------+-------+
| KIRAN | 77890 |
| VINOD | 23653 |
+----------+-------+
2 rows in set (0.00 sec)

Adding NULL with any number or string

mysql> select 100+NULL;


+----------+
| 100+NULL |
+----------+
| NULL |
+----------+
1 row in set (0.00 sec)
mysql> select "Arun"+null;
+-------------+
| "Arun"+null |
+-------------+
| NULL |
+-------------+
1 row in set, 1 warning (0.00 sec)

Theory
DDL DML
Data definition language Data Manipulation Language
It deals with the structure of the data It deals with the data
Commands: CREATE, ALTER AND Commands: INSERT, SELECT, UPDATE, DELETE
DROP
Eg: drop table employee; Eg: delete from employee;
Eg: Alter table employee Eg: Update employee
Add Set
Mobile bigint; Salary = Salary + Salary * 0.25;

ALTER UPDATE
It is a part of DDL command It belongs to DML command
It deals with the structure of the data It deals with the data
Eg: Alter table employee Eg: Update employee
Add Set
Mobile bigint; Salary = Salary + Salary * 0.25;

DROP DELETE
It is a part of DDL command It belongs to DML command
It deals with the structure of the It deals with the data
data
Eg: drop table employee; Eg: delete from employee;
Primary Key Unique Key
It is unique and not null It is unique and cane be null
It helps us to identify a row / tuple It doesn’t help to identify a row– it can also
be null
Employee(Empid#,Ename,Salary,Aadhaar,P Employee(Empid#,Ename,Salary,Aadhaar,P
assport,VoterID,Carno##) assport,VoterID,Carno##)
#Empid – Primary Key ##Carno – Unique Key

DDL commands – word table / database appears


DML commands – word table won’t appear at all

You might also like