Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
128 views

MySQL Theory Notes

Uploaded by

Faizan Uddin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views

MySQL Theory Notes

Uploaded by

Faizan Uddin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

MySQL Theory Notes

1. List some commonly used DBMS software packages.


(i) MySQL
(ii) Oracle
(iii) DB2
(iv) Postgre
(v) Sybase

2. Difference between char and varchar datatype.


CHAR VARCHAR
It is fixed length with any value from 1 to 255 It is variable length with any value from 1 to
characters. 65535 characters.
CHAR(10) reserve spaces for 10 characters. If VARCHAR(10) means a maximum of 10
data does not have 10 characters,(eg “City” has characters can be stored but the actual
4 characters) MySQL fills the remaining 6 allocated bytes will depend on the length of
characters with spaces padded on the right. entered data. (eg “City” has 4 charaters) MySQL
occupy only 4 characters.
Some space is wasted due to the fixed length. No wastage of space, due to flexible length.

3 Define Constraints? Is it mandatory to define constraints for all the attributes in a table?
Constraints are the types of restrictions, which are used to specify rules for the data in a table. Constraints are
used to limit the type of data that can go into a table.
It is not mandatory to define constraints for each attribute of a table.

4. List and explain the commonly used constraints.


Constraints Description
NOT NULL Ensures that a column cannot have NULL values where NULL means
missing/unknown/not applicable value
UNIQUE Ensures that all the values in a column are distinct/unique.
DEFALUT A default value specified for the column is no value is provided
PRIMARY KEY The column which can uniquely identify each row/record in a table.
FOREIGN KEY The column which refers to value of an attribute defined as primary
key in another relation.

5. Define Domain.
Domain is a set of values from which an attribute can take value in each row.
For example,
roll number field can have only integer values and so its domain is a set of integer values.

6. Differentiate DDL and DML commands.


DDL DML
DDL stands for Data Definition language and DML stands for Data Manipulation Language
comprises of commands which will change the and comprises of commands which are used to
structure of database object. insert, edit, view & delete the data stored in a
database object.

DDL Commands: DML Commands:


CREATE, ALTER and DROP INSERT, DELETE, UPDATE, SELECT.
7. Define Candidate keys, Primary Key and Alternate key.
Candidate Key:
A candidate key is a set of attributes in a relation that can uniquely identify each tuple(row). A relation can
have multiple candidate keys, but only one of them is chosen as the primary key.
Primary Key:
A candidate key which uniquely identifies the record is called as primary key.
Alternate Key:
An alternate key is a candidate key that is not selected as the primary key.

STUDENT
SID ADMNNO SNAME CITY DOB MobileN GENDER
o
101 12365 Ritu Nagpur 2003-10-15 47653433 F
102 23645 Naveen Goa 2004-12-24 48473625 M
103 38746 Rahul Chennai 2001-12-21 84736543 M
104 35463 Rahul Mumbai 2002-12-25 73874653 M
105 87463 Priya Chennai 2005-01-12 98764635 F

In this Student Table:


Candidate Keys are: SID, ADMNNO and MobileNo
Primary Key: SID (or) Primary Key: ADMNNO (or) Primary Key: MobileNo
Alternate Key: ADMNNO and MobileNo Alternate Key: SID and MobileNo Alternate Key: SID and ADMNNO

8. Define Composite primary key.


If no single attribute in a relation is able to uniquely distinguish the tuples, then more than one attribute is
taken together as primary key. Primary key consisting of more than one attribute is called Composite
Primary key.
Example Table: Attendance
Columns: AttendanceDate, RollNumber, AttendanceStatus.
In the relation ATTENDANCE, Roll Number cannot be used as primary key as roll number of same student will
appear in another row for a different date. Similarly, in relation Attendance, AttendanceDate cannot be used
as primary key because same date is repeated for each roll number.
However combination of these two attributes RollNumber and AttendanceDate together would always have
unique value in ATTENDANCE table as on any working day, of a student would be marked attendance only
once. Hence {RollNumber, AttendanceDate} will be the composite primary key of ATTENDANCE relation

9. Explain foreign key with an example.


A foreign key is an attribute whose value is derived from the primary key of another relation. The foreign key
is used to represent the relationship between two relations. [GuardianID is the foreign key]
Example:
Table: STUDENT
RollNo SName DOB GuardianID
43526 Anil 2006-04-02 G004
46376 Sharnesh 1999-20-12 G007
74635 Arjun 2009-10-10 G004
Table: Guardian
GuardianID GName GPhone GAddress
G004 Kevin 883746328 234,Dubai
G005 Mani 746352826 212,Sharjah
G007 Rohit 372645225 353,Dubai
GuardianID is a non-key attribute of the STUDENT table, which is the primary key of the Guardian table and is
used to relate both these table.
NOTE: Foreign key can take NULL value if it is not the part of primary key of the foreign table.

10 Explain the use of ‘Foreign Key’ in a Relational Database Management System.


. Give example to support your answer.
A foreign key is used to set or represent a relationship between two relations (or tables) in a database. Its
value is derived from the primary key attribute of another relation.
For example:
In the tables TRAINER and COURSE given below, TID is primary key in TRAINER table but foreign key in COURSE
table.
TRAINER COURSE
TID TNAME CITY HIREDATE SALARY
101 Ritu Nagpur 1998-10-15 56700 CID CNAME FEES STARTDATE TID
102 Naveen Goa 1994-12-24 80000 C201 Deepa 12000 2018-07-02 101
103 Rahul Chennai 2001-12-21 82000 C202 Sindhu 15000 2018-07-15 103
104 Kevin Mumbai 2002-12-25 68000 C203 Neeraj 10000 2018-10-01 102
105 Priya Pune 1996-01-12 95000 C204 Prakas 9000 2018-09-15 104
h

11 Differentiate between cardinality and degree of a table with the help of an example.
. Cardinality is defined as the number of tuples/rows in a table.
Degree is the number of attributes/columns in a table.
Table: Account
ACCNo CName
AC100 Sheela
AC101 Darsh
AC102 Kathy
Cardinality of the Account table is: 3
Degree of the Account table is: 2
12 Differentiate between COUNT() and COUNT(*) functions in SQL with an appropriate example.
. COUNT(*) returns the count of all the records/rows in the table,
whereas COUNT () counts the number of non-NULL values in a column that is given as an argument. Hence the
result may differ.
EMP
ENO ENAME JOB SALARY DEPTNO
8499 Akshya NULL 9870 20
8566 Arun SALESMAN 8760 30
8698 Benet MANAGER 5643 20
8912 Surya NULL 3000 10
8369 Smith CLERK 2985 10
e.g. SELECT COUNT(*) FROM EMP; e.g. SELECT COUNT(JOB) FROM EMPL;
Output Output
COUNT(*) COUNT(JOB)
5 3
13 Differentiate ALTER and UPDATE commands.
. ALTER UPDATE
It is a DDL command It is a DML command
It changes the structure of the table. It modifies the data of the table.
Example: Example:
ALTER TABLE student add column average float; UPDATE student set marks=marks+5
WHERE marks <= 50;

14 Differentiate between WHERE and HAVING clause.


. WHERE clause HAVING clause
WHERE clause is used to place conditions on HAVING clause is used to place condition on
individual rows. groups.
It implements in row operation. It implements in column operation.
WHERE clause may or may not contain HAVING clause must include aggregate function
aggregate functions. with the GROUP BY clause.
Example: Example:
Example: SELECT Dept_ID, count(*)
SELECT * FROM employee
FROM employee GROUP BY Dept_ID
WHERE Salary > 27000; HAVING count(*)>=2;

15 Differentiate GROUP BY and ORDER BY clause.


. GROUP BY ORDER BY
The GROUP BY clause groups the rows together Order By clause is used to display the result of a
that contain the same values in a specified SQL query in ascending or descending with
column. We can use the aggregate functions to respect to specified attribute values. By default,
work with grouped values. the records will be arranged in ascending order.
Example: Example:
Select Job, count(*) Select *
From employee From employee
Group By job; Order By ename;

16 Which command is used to view the structure of an existing table?


. Describe command is used to view the structure of the table with its constraints.
Example:
desc student; or describe student;
The above example displays the structure of the student table.

17 Which is the pattern-matching operator in SQL and what are the special wildcard character used to match the
. pattern?
LIKE operator is used for substring pattern-matching in SQL.
Percent (%) and underscore( _ ) are the two special wildcard characters used for the pattern match.
The percent (% ) symbol is used to represent zero or more characters.
The underscore ( _ ) symbol is used to represent a single character.
18 What are aggregate functions? List and explain the aggregate functions.
. Aggregate functions also called as group functions, that work on groups of rows rather than on single rows.
These functions are also called as multiple row functions.
By default, Aggregate functions ignores NULL values for calculation.
Aggregate functions:
i. Sum( ) - this function returns the sum of values in given column.
ii. Avg( ) – returns the average of given data.
iii. Min( ) – returns the minimum value from a given data.
iv. Max( ) – returns the maximum value from a given data.
v. Count( ) – returns the number of rows in a given column.

19 Differentiate BETWEEN and IN operators with example.


.
BETWEEN IN
The BETWEEN operator defines the range The IN operator selects values that match
of values inclusive of boundary values. any value in the given list of values.
Example: Example:
Select * Select *
From employee From employee
Where Salary BETWEEN 5000 and 10000; Where eid IN(102, 104, 110);
20 Differentiate Equi-join and Natural Join.
. Equi-join Natural Join
The join in which columns from two tables The join in which only one of the identical
are compared for equality columns existing in both tables is present
Duplicate columns are shown No duplication of columns

You might also like