0% found this document useful (0 votes)
8 views7 pages

Relational Database Notes Solved

The document discusses relational databases, specifically focusing on foreign keys, referential integrity, and various SQL commands. It explains the roles of primary keys, candidate keys, and alternate keys, along with differences between data types, SQL commands, and clauses. Additionally, it covers concepts like joins, attributes, domains, and the use of cursors in SQL, providing examples and explanations for each topic.

Uploaded by

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

Relational Database Notes Solved

The document discusses relational databases, specifically focusing on foreign keys, referential integrity, and various SQL commands. It explains the roles of primary keys, candidate keys, and alternate keys, along with differences between data types, SQL commands, and clauses. Additionally, it covers concepts like joins, attributes, domains, and the use of cursors in SQL, providing examples and explanations for each topic.

Uploaded by

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

RELATIONAL DATABASE & MYSQL AND MYSQL CONNECTVITY

FOREIGN KEY

 Used to create relationship between two tables.


 It is a non-key attribute whose value is derived fromthe Primary key or unique key of another table.
 It implement referential Integrity between two table
 Primary Key column of a table from where values will be derived is known as Primary Table or Master
Table or Parent Table and Foreign key column table will be Foreign Table or Detail Table or Child table

IN THE ABOVE RELATION EMPLOYEE : Empno and Ename are two columns have unique values so
they are in the candidate for the PRIMARY KEY position so they are called candidate key. Among both Empno
becomes PRIMARY KEY so other CANDIDATE KEY Ename will be an ALTERNATE KEY
Table : EMPLOYEE
EMPNO ENAME GENDER DEPTNO SALARY COMM

1 ANKITA F 10 20000 1200

2 SUJEET M 20 24000 1200

3 VIJAYA F 10 28000 2000

4 NITIN M 30 18000 3000

5 VIKRAM M 30 22000 1700

IN THE ABOVE RELATION EMPLOYEE


Empno and Ename are two columns have unique values so they are in the candidate for the PRIMARY KEY
position so they are called candidate key. Among both Empno becomes PRIMARY KEY so other CANDIDATE
KEY Ename will be an ALTERNATE KEY

From the Above table definition we can observe that the DEPTNO column of EMPLOYEE
table is deriving its value from DEPTNO of table DEPARTMENT. So we can say that the
DEPTNO of EMPLOYEE table is a foreign key whose value is dependent upon the Primary
key column DEPTNO of table DEPARTMENT.

REFERENTIAL INTEGRITY
 Used to ensure relationship between records in related tables are valid and user don’t accidentally
delete or change the related data.
 Referential integrity can be applied when:
o The master table’s column is a Primary Key or has a unique index
o The related fields have the same data type
o Both tables must belong to same database.
REFERENTIAL INTEGRITY

 When referential integrity is enforced usingForeign Key you must observe the following rules:
 You cannot enter a value in Child Table which is not available in Master Table’s Primary key column.
However you can enter NULL values in foreign key
 You cannot delete a record from Master Table if matching record exists in related table

AMIT KUMAR KUSHWAHA (9891145409) Page 1


 You cannot modify or change the Primary Key value in Master table if its matching record is present
in related table.
1. What is the difference between DDL and DML command?

: S.NO DDL DML


1 Data definition language Data manipulation language
2 Used to create ,modify and delete Used to update values of the given
physical structure of table table
3 Example : create table, Alter table and Example ; update ,select , delete Insert
drop command etc
2. DIFFERENCE BETWEEN CHAR AND VARCHAR DATA TYPE

S.NO. Char Datatype Varchar Datatype


1. It specifies a fixed length character It specifies a variable length character string.
String.
2. When a column is given datatype as When a column is given datatype as VARCHAR(n),
CHAR(n), then MySQL ensures that all then the maximum size a value in this column can
values stored in that column have this have is n bytes. Each value that is stored in this
length i.e. n bytes. If a value is shorter column store exactly as you specify it i.e. no blanks
than this length n then blanks are are added if the length is shorter than maximum
added, but the size of value remains length n.
n bytes.
3. Name the command used to
a. Arrange data in particular or sorting of data : Answer : order by
b. Used to change physical structure of a table : Answer : Alter table
c. Used to delete physical structure of table: Answer :drop table
d. used to view tables from database: Answer :show tables
e. used to access current database : Answer :use
f. used to create database : Answer : create database
g. used to delete database : Answer : drop database
h. used to see physical structure of a table : Answer : desc or describe
i. used to see unique values from column : Answer :distinct
4. Difference between count and count (*)?

S.NO COUNT() COUNT(*)


1 Return the count of all row in the Return the count of only those row in
table irrespective of null values the column having non-null values
2 Applied on whole table Applied on particular column
3 Select count(salary) from table Select count(*) from table
employee ; employee ;
Output : 4 Output : 5
As it will count only those record As it will count all record even
having non-null values having null values
5. Difference between where and having clause

S.NO WHERE HAVING


1 Where clause is used to put Having clause is multiple row

AMIT KUMAR KUSHWAHA (9891145409) Page 2


condition on single row function used to put condition with
group by clause
2 Cannot be used with aggregate Can be used with aggregate
function function
6. Difference between Delete and Drop table command in SQL.

S.NO Delete Drop


1 DML command DDL command
2 Used to delete rows from a table Used to delete a whole table
matching the condition but if we do physically from the database
not use where clause it will delete
all rows from table but table
remain in memory as empty table
3 Delete from employee where Drop table employee
gender = ‘G’; Output : it will physical structure
Output : it will show only record of table and now table will no
of employee number 2,4,5 longer be available
For example: in table employee

EMPNO ENAME GENDER DEPTNO SALARY COMM

1 ANKITA F 10 20000 1200

2 SUJEET M 20 24000 1200

3 VIJAYA F 10 28000 2000

4 NITIN M 30 18000 3000

5 VIKRAM M 30 NULL 1700

7. What is the use of IS NULL statement ?


Answer: to check if the column has null values or not
8. Differentiate between Degree and Cardinality? give example
Answer : Degree: refer to number of column or attributes in a table
Cardinality: refers to number of row or tuple in a table
For example: in table employee Degree is 6 and cardinality is 5
9. Differentiate between Primary key and candidate key
Primary key : is a column of a group of column that can uniquely identify the records. there can be only one primary
key in a table.
Candidate key can be any column or group of column that can qualify as unique key in the table. There can be
multiple candidate keys in the table
One of the Candidate key is a primary key
For example: in table employee Empno and Ename are the candidate key as they contain unique values . but among
both only Empno is suitable for becoming primary key.
10. Differentiate between candidate key and Alternate key

AMIT KUMAR KUSHWAHA (9891145409) Page 3


Candidate key: Candidate key can be any column or group of column that can qualify as unique key in the table.
There can be multiple candidate keys in the table
Alternate key: those candidate keys which cannot be selected a primary key are said to be alternate key
For example: In table employee Empno and Ename are the candidate key as they contain unique values. But among
both only Empno is suitable for becoming primary key. So Ename will be an alternate key
11. Give one point of difference between Equi join and Natural Join.

S.NO EQUI JOIN NATURAL JOIN

1 the join in which column from are The join in which only one of the
compared for equality identical columns existing in both
tables is present
2 Duplicate column are shown No duplication of column are there

12. Differentiate between Attributes and Domain in the context of RDBMS?


Attributes: the vertical data set of relation is known as attributes. It represents one entity. For example Empid and
Ename are attributes for relation employee
Domain: refers to the pool of values from which a field of a table drives its values .Example Empid drives its values
from set on integers representing employee id of employee of company
13. What is the difference between % and _ wildcard characters in SQL Like Clause? Illustrate with example
Answer : The % character can be used to replace 1 to any number of characters in string whereas the _ character
can be used to replaced exactly one character . for example In table SCORES
NAME SCORE
RITESH 45
BRIJESH 44
RAKESH 78
BYOMKESH 67
FOR EXAMPLE : FOR EXAMPLE :
SELECT NAME FROM SELECT NAME
SCORES WHERE NAME FROM SCORES
LIKE %E% WHERE NAME
OUTPUT LIKE _ _ _E%
RITESH RITESH
BRIJESH RAKESH
RAKESH
BYOMKESH
14. What is record set ?
Answer: the row or records return after the execution of queries by the cursor
15. What is cursor?
Answer : the result set returned by the queries after execution are going to be stored in a place called cursor from
where record by record processing of row takes place.
16. What is the difference between fetchone() and fetchmany ()?
Answer : fetchone () will read the record from cursor one by one for further processing while fetchmany () will read
all the records from cursor at once then with the help of loop we can process records
17. A result set is extracted from the database using cursor object ( that has been already created ) by giving the
following statement
Mydata=cursor.fetchone ()

AMIT KUMAR KUSHWAHA (9891145409) Page 4


a) How many records will be returned ny fetchone() method
Answer : one record
b) What will be the datatype of mydata object after the given command is executed ?
Answer :tuple
18. Consider the following table AUTOMOBILE and answer I) to III)

Table : Automobile
Code Cname Make Color capacity charges
501 A-star Suzuki Orange 3 14
503 Indigo Tata Silver 3 12
502 Innova Toyoto White 7 15
509 SX4 Suzuki Silver 4 14
510 C-class Mercedes Red 4 35

i) To show details of those cars having capacity more than 3 in descending order of charges.
Answer : select * from automobile where capacity >3 order by desc;
ii) Delete the column capacity from table Automobile
Answer : alter table automobile drop column capacity;
iii) Increase the charges by 5% of those whose cars whose name end with ‘e’.
Answer :update automobile set charges +=charges *.05 where Cname like ‘%e’;
iv) Write the degree and cardinality of the table
Answer : degree : 6 , cardinality : 5
v) Which field should be made primary key and justify
Answer : code can be a primary key as no two cars can have a same code.
vi) Identify candidate key (s) from the above table
Answer : code ,cname can be a candictate key as they contain unique values
vii) Write a command to add column city of type varchar 20.
Answer : Alter table Automobile add city varchar(20);
viii) Write a command to add primary key code to the table
Answer : Alter table add primary key (code);
ix) Write a command to change the name of the column Cname to Carname.
Answer: Alter table Automobile change Cname Carname varchar (20);
x) Write a command to change the data type of column make from char to varchar 20
Answer : Alter table Automobile modify make varchar (20);
xi) Write a command to show unique values from make
Answer:Select distinct(make ) from automobile
xii) Write a command to remove primary key from table automobile
Answer : Alter table automobile drop primary key;
xiii) If two columns are added and 2 rows are deleted from the tableresult, what will be the new
degree and cardinality of the above table?
Answer : Degree : 8 and cardinality = 3
xiv) Write a command to delete a record whose capacity is more then 4
Answer : Delete from Automobile where capacity >4;

AMIT KUMAR KUSHWAHA (9891145409) Page 5


19. Consider the following tables – Bank_Account and Branch:
Table: Bank_Account table : Branch
ACode Name Type Acode City
A01 Amrita Savings A01 Delhi
A02 Parthodas Current A02 Mumbai
A03 Miraben Current A01 Nagpur

What will be the output of the following statement?


SELECT * FROM Bank_Account NATURAL JOIN Branch;
Answer : THE OUTPUT IS :

Acode Name Type City


A01 AMRITA SAVING DELHI
A01 AMRITA SAVINGS NAGPUR
A02 PARTHODAS CURRENT MUMBAI
20. Explain the use of ‘Foreign Key’ in a Relational Database Management System. Give example to support
your answer.
Ans: 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. It is used to set referential integrity
between two tables so that no one can change delete or insert data accidentally in any table For example: In
the tables Bank_Account and Branch given below, ACODE is primary key in Bank_Account table but
foreign key in Branch table.
21. Explain group by clause with example
Answer : The GROUP BY clause combines all those records(row) that have identical values in a particular
field(column) or a group of fields(columns).Grouping result into one summary record per group.
GROUPING can be done by a column name, or with aggregate functions in which case the aggregate
produces a value for each group.
Table : EMPL
EMPNO ENAME JOB SAL DEPTNO
8369 SMITH CLERK 2985 10
8499 ANYA SALESMAN 9870 20
8566 AMIR SALESMAN 8760 30
8698 BINA MANAGER 5643 20
e.g. Calculate the number of employees in each grade.

SELECT JOB, COUNT(*) FROM EMPL GROUP BY JOB ;


Output
JOB COUNT(*)
CLERK 1
SALESMAN 2
MANAGER 1

22. Explain Cartesian product or cross join or unrestricted join ?


Answer : when two table are joined without any condition using where clause. The resultant table will
contain all possible concatenation of row from both the table.
For example :Consider the following tables – Bank_Account and Branch:

Ifsc City
AMIT KUMAR KUSHWAHA (9891145409) Page 6
Table: Bank_Account A5012 Delhi table : Branch
A5103 Mumbai
ACode Name Type
A01 Amrita Savings
A02 Parthodas Current
A03 Miraben Current
The resultant output will be

Acode Name Type Ifsc City


A01 AMRITA SAVING A5012 DELHI
A01 AMRITA SAVINGS A5103 NAGPUR
A02 PARTHODAS CURRENT A5012 DELHI
A02 PARTHODAS CURRENT A5103 NAGPUR
A03 Miraben Current A5012 DELHI
A03 Miraben Current A5103 NAGPUR
23. Distinguish between a Primary Key and a Unique key in a table.
S.NO. PRIMARY KEY UNIQUE KEY
1. Column having Primary key can’t contain Column having Unique Key can contain
NULL value NULL value
2. There can be only one primary key in Table. Many columns can be defined as Unique key
24. Distinguish between ALTER Command and UPDATE command of SQL.

S.NO. ALTER COMMAND UPDATE COMMAND


1. It is a DDL Command It is a DML command
2. It is used to change the definition or It is used to modify the data values present
physical structure of
existing table, i.e. adding column, in the rows of the table.
deleting column, etc.
3. Syntax for adding column in a table: Syntax for using UPDATE command:
ALTER TABLE <tablename> UPDATE <Tablename>
ADD <Column name><Datatype> ; SET <Columnname>=value
WHERE <Condition> ;
25. Write short note on ALTER TABLE COMMAND
Answer: The ALTER TABLE command is used to change definitions of existing tables.(adding columns,
deleting columns etc.). The ALTER TABLE command is used for :
1. adding columns to a table
2. Modifying column-definitions of a table.
3. Deleting columns of a table.
4. Adding constraints to table.
5. Enabling/Disabling constraints.

AMIT KUMAR KUSHWAHA (9891145409) Page 7

You might also like