Ravindra DBMS
Ravindra DBMS
SESSION 2022-23
DATE OF SUBMISSION: 10-10-2023
1. Design a Database and create required table. For e.g. Bank, College
Database.
5. Apply the constrains like Primary key, Foreign key, NOT NULL to the
tables.
DBMS : DBMS or Database Management System is a software application used to access, create,
and manage databases by modifying them.
Functions of DBMS :
Types of DBMS :
Installing MySQL on Windows: Your downloaded MySQL is neatly packaged with an installer.
Download the installer package, unzip it anywhere and run setup.exe. By default, this process will
install everything under C:\mysql.
• Step-3 : if you already have created data bases then use them else create new database.
Creation of Database :
Syntax : CREATE DATABASE databasename
DESC Command : desc or describe command shows the structure of table which include name of
the column, data-type of column and the nullability which means, that column can contain null values
or not.
Que.2. SQL statement for implementing ALTER,
UPDATE and DELETE.
Database Language : Database languages are used to read, update and store data in a
database. Read, Update, Manipulate, and Store data in a database using Database
Languages.there are four types of DBMS language :
DDL (Data Definition language) - DDL is used when we need to create database and tables,
alter them , drop them and rename the table .
TRUNCATE student;
DML (Data Manipulation Language) - DML language used to manipulate the database like
inserting data, updating table, retrieving record from a table .
DCL (Data Control Language) : Grant privilege to a user using the GRANT
statement.revoke the privilege using the REVOKE statement.
START TRANSACTION;
INSERT INTO student (name, lastname) VALUES ('Dia', 'Shivi');
COMMIT;
Example :
UPDATION Query :
DELETION Query :
DATABASE CONSTRAINTS
• Primary Key
• Foreign Key
• CHECK Constraint
• UNIQUE Constraint
• NOT NULL Constraint
PRIMARY KEY : A primary key is a column or a group of columns used to identify a row
uniquely in a table. A primary key constraint is the combination of a not-null
constraint and a UNIQUE constraint.
In case you want to specify the name of the primary key constraint, you use CONSTRAINT
clause as follows:
UNIQUE Constraint : This constraint ensures that a column or a group of columns in each
row have a distinct value. A column(s) can have a null value but the values cannot be
duplicated.
NOT NULL Constraint : This constraint ensures all rows in the table contain a definite
value for the column which is specified as not null. Which means a null value is not
allowed.
Following are the most used constraints that can be applied to a table.
• NOT NULL
• UNIQUE
• PRIMARY KEY
• FOREIGN KEY
• CHECK
• DEFAULT
NOT NULL : NOT NULL constraint restricts a column from having a NULL value.
Once NOT NULL constraint is applied to a column, you cannot pass a null value to that
column. It enforces a column to contain a proper value.
UNIQUE : UNIQUE constraint ensures that a field or column will only have unique values.
A UNIQUE constraint field will not have duplicate data. This constraint can be applied at
column level or table level.
FOREIGN KEY : FOREIGN KEY is used to relate two tables. FOREIGN KEY constraint
is also used to restrict actions that would destroy links between tables. Foreign Key is a
field in a table which uniquely identifies each row of a another table
4.Prevents the deletion of a record that contains a value referred to by a foreign key in
another table
5.Prevents the addition of a record to a table that contains a foreign key unless there is a
primary key in the linked table
Que.7. Write query for implementing the following
functions: MAX( ), MIN( ), AVG( ) and COUNT( ).
Aggregate Functions: Aggregate functions perform a calculation on a set of rows and return
a single row. You can use aggregate functions as expressions only in the following clauses
i.e., SELECT clause, HAVING clause. There are five aggregate functions:
AVG(): The AVG() function allows you to calculate the average value of a numeric column.
syntax : AVG(column)
COUNT(expression)
MAX() : It returns the maximum value from the specified table field.
syntax : MAX(expression)
SUM(): SUM function which returns the sum of all the values in the specified column. SUM
works on numeric fields only. Null values are excluded from the result returned.
syntax : SUM(expression)
These functions are called aggregate functions because they operate on the aggregate of
tuples. The result of an aggregate function is a single value.
Que.8. Perform the queries for triggers.
TRIGGERS
TRIGGERS : A SQL trigger is a database object just like a stored procedure, or we can say
it is a special kind of stored procedure which fires when an event occurs in a database. We
can execute a SQL query that will "do something" in a database when an event is fired.
Types of Triggers
1. DDL Trigger
2. DML Trigger
TRIGGERS PROCEDURES
Triggers can't be called inside a procedure. But, you can call a procedure inside a
trigger.
id first_name last_name
1 john doe
2 lily blush
WHERE ID = 2;
id first_name last_name
1 john doe
2 lily brown
Advantages of Triggers :
• Trigger generates some derived column values automatically
• Enforces referential integrity
• Event logging and storing information on table access
• Auditing
• Synchronous replication of tables
• Imposing security authorizations
• Preventing invalid transactions
• INNER JOIN
• LEFT JOIN
• RIGHT JOIN
• FULL JOIN
sample tables :Suppose we have two tables called basket a and basket b that stores fruits:
SELECT a.id id_a, a.fruit fruit_a, b.id id_b, b.fruit fruit_b FROM basket_a a LEFT JOIN basket_b b ON
a.fruit = b.fruit;
FULL JOIN: returns rows when there is a match in one of the tables.
SELECT a.id id_a, a.fruit fruit_a, b.id id_b, b.fruit fruit_b FROM basket_a a FULL JOIN basket_b b ON
a.fruit = b.fruit WHERE a.id IS NULL OR b.id IS NULL;
Output :
CREATE VIEW stu_View AS SELECT NAME, ADDRESS FROM college WHERE S_ID
syntax :
INSERT INTO view_name(column1, column2 , column3,..) VALUES(value1, value2,
value3..);
Que.11. Write the query for creating the users and their
roles.
How To Create a New User and their roles-
Create a New User :
asterisks in this command refer to the database and table that they can access .Once you
have finalized the permissions that you want to set up for your new users, always be sure to
reload all the privileges.Your changes will now be in effect.
• ALL PRIVILEGES - as we saw previously, this would allow a MySQL user full
access to a designated database (or if no database is selected, global access across
the system)
• SELECT - allows them to use the SELECT command to read through databases
To provide a specific user with a permission, you can use this framework:
If you need to revoke a permission, the structure is almost identical to granting it:
Just as you can delete databases with DROP, you can use DROP to delete a user altogether:
For example, Suppose we design a school database. In this database, the student
will be an entity with attributes like address, name, id, age, etc. The address can be
another entity with attributes like city, street name, pin code, etc and there will be
a relationship between them.
Component of ER Diagram
1. Entity:
An entity may be any object, class, person or place. In the ER diagram, an entity
can be represented as rectangles.
An entity that depends on another entity called a weak entity. The weak entity
doesn't contain any key attribute of its own. The weak entity is represented by a
double rectangle.
2. Attribute
The attribute is used to describe the property of an entity. Eclipse is used to
represent an attribute.
For example, id, age, contact number, name, etc. can be attributes of a student.
a. Key Attribute
c. Multivalued Attribute
An attribute can have more than one value. These attributes are known as a
multivalued attribute. The double oval is used to represent multivalued attribute.
For example, a student can have more than one phone number.
d. Derived Attribute
For example, A person's age changes over time and can be derived from another
attribute like Date of birth.
AD
3. Relationship
A relationship is used to describe the relation between entities. Diamond or
rhombus is used to represent the relationship.
Types of relationship are as follows:
a. One-to-One Relationship
When only one instance of an entity is associated with the relationship, then it is
known as one to one relationship.
For example, A female can marry to one male, and a male can marry to one female.
AD
b. One-to-many relationship
When only one instance of the entity on the left, and more than one instance of an
entity on the right associates with the relationship then this is known as a one-to-
many relationship.
For example, Scientist can invent many inventions, but the invention is done by
the only specific scientist.
c. Many-to-one relationship
When more than one instance of the entity on the left, and only one instance of an
entity on the right associates with the relationship then it is known as a many-to-
one relationship.
For example, Student enrolls for only one course, but a course can have many
students.
d. Many-to-many relationship
When more than one instance of the entity on the left, and more than one instance
of an entity on the right associates with the relationship then it is known as a many-
to-many relationship.
For example, Employee can assign by many projects and project can have many
employees.