Database Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Experiment name: Study of Designing Entity-Relationship Model.

Objectives: The main objectives of this experiment are,


• To know about same basic operation and implementation by mysql code.
• Add and remove columns after creating a table.
• Create foreign key and use it another table
• To know about RDBMS and establishing relationships between two tables using a
foreign key
Theory:
MySQL is an open-source relational database management system known for its speed,
reliability, and user-friendly interface. It follows the relational database model, organizing data
into tables with rows and columns, and supports SQL for data manipulation. MySQL excels in
performance, handling large datasets efficiently through techniques like indexing and caching.
It is highly scalable, capable of serving both small-scale projects and large enterprise
applications. With robust security features, including user authentication and encryption,
MySQL ensures data integrity and protection. Additionally, it supports high availability through
features like replication and clustering, and offers various storage engines and data types for
flexibility. Overall, MySQL's extensive features, active community support, and cross-platform
compatibility make it a popular choice for a wide range of applications

RDMS: A Relational Database Management System (RDBMS) is a software that manages


databases based on the relational model, organizing data into tables with rows and columns.
The primary key, a unique identifier for each record, ensures entity integrity by preventing
duplicate values. Meanwhile, foreign keys establish relationships between tables, allowing
records in one table to reference records in another. This enforces referential integrity,
ensuring that data remains consistent across related tables. For example, in a customer and
orders scenario, the customer's unique ID (primary key) is referenced in the orders table
(foreign key), linking each order to a specific customer. This fundamental interplay of primary
and foreign keys forms the backbone of structured, organized data management in relational
databases.

Lab task 1:The working of reverse, replace, trim, position and ascii function.
Code:
select reverse(stu_name) as reverse from student;
select replace("I am a bad boy","bad","good");
select ltrim(" jakir ");
select rtrim(" jakir ");
select position("is" in "my name is jakir") as name;
select ascii("%");
select ascii("#");
output:

6
Discussion:
Reverse: in MySQL, the REVERSE function is used to reverse the order of characters in a
string. Here reverse student name from student table
ASCII:The ASCII function in MySQL returns the ASCII value (integer representation) of the
leftmost character of a given string
Ltream: The LTRIM function in MySQL is used to remove leading spaces (or other specified
characters) from the left side of a string. This is often used for cleaning up data where
leading spaces may cause issues.
Rtream: The RTRIM function in MySQL is used to remove trailing spaces (or other specified
characters) from the right side of a string. This is useful for cleaning up data where trailing
spaces may cause problems
Position: The POSITION function (or LOCATE function, they are synonymous in MySQL) is
used to find the position of a substring within a string. It returns the starting position of the
first occurrence of a substring in the string

Lab task 2: Add another Column to an after creating a table


Code:
ALTER TABLE student ADD email varchar(255);
Outpur:

Discussion: here the email address column is added after creating this table.

Lab task3: : Remove column from a table


Code:
ALTER TABLE student DROP column email;
Output:

7
Lab task 3: : Create two table and merge them
Code:
Create table table2( t_id int
primary key, t_name
varchar(25), t_contact
varchar(25), t_age int
);
INSERT INTO table2(t_id,t_name,t_contact,t_age)
VALUES
(111, 'shihab' ,1234567,20),
(112, 'mamun',7356783,42 ),
(882, 'Tom' ,458756788,41),
(424, 'Martin',1279876529,30);

select* from table2;


Create table table3( id int
primary key, table_id int,
foreign key(table_id) references table2(t_id));
INSERT INTO table3(id,table_id)
VALUES (001,111),
(002,882),
(003,112);
select* from table3;
select t_id,id,t_age from table3 join table2 on table3.table_id;

output:

Fig: table 1 Fig: table 2

Fig: adding table 1 and table 2

8
Discussion: here table 1 t_id use as a foreign key for table 2 table id. After joining tow table
the new table column is t_id, id, t_age.

Discussion and conclusion:


In a relational database, primary and foreign keys are vital for establishing connections
between tables. A primary key is a unique identifier within a table, ensuring each record has
a distinct identity. On the other hand, a foreign key is a field in one table that refers to the
primary key in another, facilitating relationships between tables. By linking these keys, data
integrity is maintained, and meaningful associations are established. When joining two tables,
the foreign key in one table is matched with the primary key in another, allowing for the
retrieval of related information. This process is pivotal in database management, enabling the
creation of comprehensive datasets from multiple interrelated tables.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy