0% found this document useful (0 votes)
2 views9 pages

NOTES - F - of - DATABASE - A5

The document provides a detailed guide on creating and managing databases and tables in MariaDB, specifically for attendance and election systems. It includes commands for creating databases, tables, inserting data, altering tables, and querying data. Additionally, it outlines the structure of several tables related to students, activities, elections, candidates, and voting processes.

Uploaded by

carlbenedictnj7
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)
2 views9 pages

NOTES - F - of - DATABASE - A5

The document provides a detailed guide on creating and managing databases and tables in MariaDB, specifically for attendance and election systems. It includes commands for creating databases, tables, inserting data, altering tables, and querying data. Additionally, it outlines the structure of several tables related to students, activities, elections, candidates, and voting processes.

Uploaded by

carlbenedictnj7
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/ 9

CREATING DATABASE & TABLES

# mysql -u root -p
MariaDB [(none)]> create database Db_Attendance;
MariaDB [(none)]> use Db_Attendance;
MariaDB [Db_Attendance]> create table tbl_students(
-> StudentID varchar(8) not null primary key,
-> FirstName varchar(30) not null,
-> MiddleName varchar(20),
-> LastName varchar(20) not null);
MariaDB [Db_Attendance]> describe tbl_students;
MariaDB [Db_Attendance]> create table tbl_activities(
-> ActivityID varchar(8) not null primary key,
-> ActivityTitle varchar(30) not null,
-> ActivityVenue varchar(30) not null);
MariaDB [Db_Attendance]> create table tbl_attendance(
-> ActivityID varchar(8) not null,
-> StudentID varchar(8) not null,
-> TimeIn timestamp);
MariaDB [Db_Attendance]> describe tbl_attendance;
MariaDB [Db_Attendance]> alter table tbl_attendance
-> add foreign key(StudentID)
-> references tbl_students(StudentID)
-> on update cascade
-> on delete restrict;
MariaDB [Db_Attendance]> alter table tbl_attendance
-> add foreign key(ActivityID)
-> references tbl_activities(ActivityID)
-> on update cascade
-> on delete restrict;
MariaDB [Db_Attendance]> show create table tbl_attendance

INSERTING DATA INTO THE TABLES


MariaDB [Db_Attendance]> INSERT INTO tbl_students values
-> ('20251000', 'LEBRON', 'ANDREW', 'JAMES');
MariaDB [Db_Attendance]> SELECT * FROM tbl_students;

MariaDB [Db_Attendance]> INSERT INTO tbl_activities values


-> ('1', 'Foundation Day', 'COVERED COURT');
MariaDB [Db_Attendance]> SELECT * FROM tbl_activities;

MariaDB [Db_Attendance]> INSERT INTO tbl_attendance values


-> (1, '20251000', now());
MariaDB [Db_Attendance]> SELECT * FROM tbl_attendance;
or
MariaDB [Db_Attendance]> SELECT StudentID,LastName FROM
tbl_attendance;

SORTING DATA FROM THE TABLES (A-Z\Z-A)

MariaDB [Db_Attendance]> select * tbl_student order by LastName asc;

DELETING DATA FROM THE TABLES


MariaDB [Db_Attendance]> delete from tbl_students where student_id
= “<StudentID>”;
TABLE CREATION
CREATE TABLE tbl_items (
id INT(8) AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
price VARCHAR(12) NOT NULL,
brand VARCHAR(30) NOT NULL);

CREATE TABLE tbl_customer (


customer_id INT(8) AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(20) NOT NULL,
contact VARCHAR(15) NOT NULL);

CREATE TABLE tbl_orders (


order_id INT(8) AUTO_INCREMENT PRIMARY KEY,
item_id INT(8) NOT NULL,
customer_id INT(8) NOT NULL,
order_date DATE NOT NULL,
quantity INT NOT NULL,
FOREIGN KEY (item_id) REFERENCES tbl_items(id),
FOREIGN KEY (customer_id) REFERENCES
tbl_customer(customer_id));

DATA INSERTION
INSERT INTO tbl_items (name, price, brand) VALUES
('LAPTOP', 25000.00, 'ACER'),
('PRINTER', 14000.00, 'EPSON');

INSERT INTO tbl_customer (first_name, last_name, contact) VALUES


('LEBRON', 'JAMES', '09123456789'),
('STEPHEN', 'CURRY', '09876543210');

INSERT INTO tbl_orders (order_id, item_id, customer_id, order_date)


VALUES
(1, 1, 1, '2024-02-20'), -- LEBRON buys 2 Laptops
(2, 2, 1, '2024-02-20'), -- LEBRON buys 2 Printers
(3, 1, 2, '2024-02-21'), -- CURRY buys 2 Laptops
(4, 2, 2, '2024-02-21'); -- CURRY buys 2 Printers

STRUCTURE OF ALL 6 TABLES


1️⃣ tbl_candidate (candidate_ID, candidate_name, partylist_ID,c_date_registered)
2️⃣ tbl_member (member_ID, member_name, position_ID, m_date_registered)
3️⃣ tbl_partylist (partylist_ID, partylist)
4️⃣ tbl_position (position_ID, position)
5️⃣ tbl_ballot (ballot_ID, member_ID, ballot_precinct)
6️⃣ tbl_vote_count (candidate_ID, partylist_ID, total_votes)

CREATING THE TABLES

MariaDB [(none)]> create database DB_Election;


MariaDB [(none)]> use DB_Election;
MariaDB [DB_Election]> create table tbl_Candidate(
-> candidate_ID int(8) auto_increment primary key,
-> partylist_ID int(8) not null,
-> candidate_name varchar(100) not null,
-> c_date_registered date not null);
MariaDB [DB_Election]> describe tbl_candidate;
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| candidate_ID | int(8) | NO | PRI | NULL | auto_increment |
| partylist_ID | int(8) | NO | | NULL | |
| candidate_name | varchar(100) | NO | | NULL | |
| c_date_registered | date | NO | | NULL | |
+-------------------+--------------+------+-----+---------+----------------+
MariaDB [DB_Election]> create table tbl_member(
-> member_ID int(8) auto_increment primary key,
-> position_ID int(8) not null,
-> member_name varchar(100) not null,
-> m_date_registered date not null);
MariaDB [DB_Election]> describe tbl_member;
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| member_ID | int(8) | NO | PRI | NULL | auto_increment |
| position_ID | int(8) | NO | | NULL | |
| member_name | varchar(100) | NO | | NULL | |
| m_date_registered | date | NO | | NULL | |
+-------------------+--------------+------+-----+---------+----------------+
MariaDB [DB_Election]> create table tbl_partylist(
-> partylist_ID int(8) auto_increment primary key,
-> partylist varchar(50) not null);
MariaDB [DB_Election]> describe tbl_partylist;
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| partylist_ID | int(8) | NO | PRI | NULL | auto_increment |
| partylist | varchar(50) | NO | | NULL | |
+--------------+-------------+------+-----+---------+----------------+
MariaDB [DB_Election]> create table tbl_position(
-> position_ID int(8) auto_increment primary key,
-> position varchar(30) not null);
MariaDB [DB_Election]> describe tbl_position;
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| position_ID | int(8) | NO | PRI | NULL | auto_increment |
| position | varchar(30) | NO | | NULL | |
+-------------+-------------+------+-----+---------+----------------+
MariaDB [DB_Election]> create table tbl_ballot(
-> ballot_ID int(8) auto_increment primary key,
-> member_ID int(8) not null,
-> ballot_precinct varchar(30) not null);
MariaDB [DB_Election]> describe tbl_ballot;
+-----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+----------------+
| ballot_ID | int(8) | NO | PRI | NULL | auto_increment |
| member_ID | int(8) | NO | | NULL | |
| ballot_precinct | varchar(30) | NO | | NULL | |
+-----------------+-------------+------+-----+---------+----------------+
MariaDB [DB_Election]> create table tbl_vote_count(
-> candidate_ID int(8) not null,
-> partylist_ID int(8) not null,
-> total_votes int default 0);
MariaDB [DB_Election]> describe tbl_vote_count;
+--------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+-------+
| candidate_ID | int(8) | NO | | NULL | |
| partylist_ID | int(8) | NO | | NULL | |
| total_votes | int(11) | YES | |0 | |
+--------------+---------+------+-----+---------+-------+

ALTERING THE TABLES

MariaDB [DB_Election]> alter table tbl_candidate


-> add foreign key(partylist_ID)
-> references tbl_partylist(partylist_ID)
-> on update cascade
-> on delete restrict;
Query OK, 0 rows affected (0.092 sec)
MariaDB [DB_Election]> describe tbl_candidate;
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| candidate_ID | int(8) | NO | PRI | NULL | auto_increment |
| partylist_ID | int(8) | NO | MUL | NULL | |
| candidate_name | varchar(100) | NO | | NULL | |
| c_date_registered | date | NO | | NULL | |
+-------------------+--------------+------+-----+---------+----------------+
MariaDB [DB_Election]> alter table tbl_member
-> add foreign key(position_ID)
-> references tbl_position(position_ID)
-> on update cascade
-> on delete restrict;
MariaDB [DB_Election]> describe tbl_member;
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| member_ID | int(8) | NO | PRI | NULL | auto_increment |
| position_ID | int(8) | NO | MUL | NULL | |
| member_name | varchar(100) | NO | | NULL | |
| m_date_registered | date | NO | | NULL | |
+-------------------+--------------+------+-----+---------+----------------+
MariaDB [DB_Election]> alter table tbl_ballot
-> add foreign key(member_ID)
-> references tbl_member(member_ID)
-> on update cascade
-> on delete restrict;
MariaDB [DB_Election]> describe tbl_ballot;
+-----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+----------------+
| ballot_ID | int(8) | NO | PRI | NULL | auto_increment |
| member_ID | int(8) | NO | MUL | NULL | |
| ballot_precinct | varchar(30) | NO | | NULL | |
+-----------------+-------------+------+-----+---------+----------------+
MariaDB [DB_Election]> alter table tbl_vote_count
-> add foreign key(candidate_ID)
-> references tbl_candidate(candidate_ID)
-> on update cascade
-> on delete restrict;
MariaDB [DB_Election]> alter table tbl_vote_count
-> add foreign key(partylist_ID)
-> references tbl_partylist(partylist_ID)
-> on update cascade
-> on delete restrict;
MariaDB [DB_Election]> describe tbl_vote_count;
+--------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+-------+
| candidate_ID | int(8) | NO | MUL | NULL | |
| partylist_ID | int(8) | NO | MUL | NULL | |
| total_votes | int(11) | YES | |0 | |
+--------------+---------+------+-----+---------+-------+

INSERTING DATA INTO THE TABLES

MariaDB [DB_Election]> insert into tbl_partylist values


-> (null,'Partylist A'),
-> (null,'Partylist B'),
-> (null,'Partylist C');
MariaDB [DB_Election]> select * from tbl_partylist;
+--------------+-------------+
| partylist_ID | partylist |
+--------------+-------------+
| 1 | Partylist A |
| 2 | Partylist B |
| 3 | Partylist C |
+--------------+-------------+
MariaDB [DB_Election]> insert into tbl_position values
-> (null,'President'),
-> (null,'Vice President'),
-> (null,'Secretary');
MariaDB [DB_Election]> select * from tbl_position;
+-------------+----------------+
| position_ID | position |
+-------------+----------------+
| 1 | President |
| 2 | Vice President |
| 3 | Secretary |
+-------------+----------------+
MariaDB [DB_Election]> insert into tbl_candidate values
-> (null, 1, 'James Harden', '2025-03-10'),
-> (null, 2, 'Luka Doncic', '2025-04-11'),
-> (null, 3, 'Jimmy Butler', '2025-05-14');
MariaDB [DB_Election]> select * from tbl_candidate;
+--------------+--------------+----------------+-------------------+
| candidate_ID | partylist_ID | candidate_name | c_date_registered |
+--------------+--------------+----------------+-------------------+
| 1| 1 | James Harden | 2025-03-10 |
| 2| 2 | Luka Doncic | 2025-04-11 |
| 3| 3 | Jimmy Butler | 2025-05-14 |
+--------------+--------------+----------------+-------------------+
MariaDB [DB_Election]> insert into tbl_member values
-> (null, 1, 'Lebron James', '2024-05-15'),
-> (null, 2, 'Stephen Curry', '2024-07-23'),
-> (null, 3, 'Kevin Durant', '2024-09-29');

MariaDB [DB_Election]> select * from tbl_member;


+-----------+-------------+---------------+-------------------+
| member_ID | position_ID | member_name | m_date_registered |
+-----------+-------------+---------------+-------------------+
| 4| 1 | Lebron James | 2024-05-15 |
| 5| 2 | Stephen Curry | 2024-07-23 |
| 6| 3 | Kevin Durant | 2024-09-29 |
+-----------+-------------+---------------+-------------------+
MariaDB [DB_Election]> insert into tbl_ballot values
-> (null, 4, 'Precinct 1'),
-> (null, 5, 'Precinct 2'),
-> (null, 6, 'Precinct 3');
MariaDB [DB_Election]> select * from tbl_ballot;
+-----------+-----------+-----------------+
| ballot_ID | member_ID | ballot_precinct |
+-----------+-----------+-----------------+
| 1| 4 | Precinct 1 |
| 2| 5 | Precinct 2 |
| 3| 6 | Precinct 3 |
+-----------+-----------+-----------------+
MariaDB [DB_Election]> insert into tbl_vote_count values
-> (1, 1, 300),
-> (2, 2, 250),
-> (3, 3, 400);
MariaDB [DB_Election]> select * from tbl_vote_count;
+--------------+--------------+-------------+
| candidate_ID | partylist_ID | total_votes |
+--------------+--------------+-------------+
| 1| 1| 300 |
| 2| 2| 250 |
| 3| 3| 400 |
+--------------+--------------+-------------+
MariaDB [db_election]> SELECT
-> b.ballot_ID,
-> b.ballot_precinct,
-> m.member_name,
-> m.m_date_registered,
-> c.candidate_name,
-> p.position,
-> p2.partylist
-> FROM
-> tbl_ballot b
-> INNER JOIN
-> tbl_member m ON b.member_ID = m.member_ID
-> INNER JOIN
-> tbl_position p ON m.position_ID = p.position_ID
-> INNER JOIN
-> tbl_candidate c ON m.position_ID = c.candidate_ID
-> INNER JOIN
-> tbl_partylist p2 ON c.partylist_ID = p2.partylist_ID;
+-----------+-----------------+---------------+-------------------+----------------+----------------+-------------+
| ballot_ID | ballot_precinct | member_name | m_date_registered | candidate_name | position | partylist |
+-----------+-----------------+---------------+-------------------+----------------+----------------+-------------+
| 1 | Precinct 1 | Lebron James | 2024-05-15 | James Harden | President | Partylist A |
| 2 | Precinct 2 | Stephen Curry | 2024-07-23 | Luka Doncic | Vice President | Partylist B |
| 3 | Precinct 3 | Kevin Durant | 2024-09-29 | Jimmy Butler | Secretary | Partylist C |
+-----------+-----------------+---------------+-------------------+----------------+----------------+-------------+

MYSQL JOINS
TASK 1: (*** ALL TUPLES)

select g1.grantee_ID, s.student_ID, s.lastname, s.firstname, s.middlename, g2.amount


from tbl_grant_masterlist g1
inner join tbl_enrollees e ON e.enr_ID = g1.enr_ID
inner join tbl_student s ON s.student_ID = e.student_ID
inner join tbl_grant g2 ON g2.grant_ID = g1.grant_type;

TASK 2: (DISPLAY ONLY TDP GRANTEES)

select g1.grantee_ID, s.student_ID, s.lastname, s.firstname, s.middlename, g2.amount


from tbl_grant_masterlist g1
inner join tbl_enrollees e ON e.enr_ID = g1.enr_ID
inner join tbl_student s ON s.student_ID = e.student_ID
inner join tbl_grant g2 ON g2.grant_ID = g1.grant_type
where g1.grant_type = '1';

TASK 3: (DISPLAY ONLY TES GRANTEES)

select g1.grantee_ID, s.student_ID, s.lastname, s.firstname, s.middlename, g2.amount


from tbl_grant_masterlist g1
inner join tbl_enrollees e ON e.enr_ID = g1.enr_ID
inner join tbl_student s ON s.student_ID = e.student_ID
inner join tbl_grant g2 ON g2.grant_ID = g1.grant_type
where g1.grant_type = '2';

You might also like