Delivery 2 Database Fundamentals v2
Delivery 2 Database Fundamentals v2
Authors:
Tutor:
Introduction......................................................................................................................................4
Activity approach.............................................................................................................................5
Problem............................................................................................................................................5
Entity-Relationship Diagram...........................................................................................................6
Relational model..............................................................................................................................7
Scheme.............................................................................................................................................8
Data dictionary...............................................................................................................................10
Restrictions....................................................................................................................................11
Physical Model..............................................................................................................................12
Activity 2 Approach.......................................................................................................................18
o Check functions....................................................................................................................30
Literature........................................................................................................................................37
Table2 Restrictions........................................................................................................................11
Introduction
As part of the activities of the subject of database fundamentals, the activity is the design
diagramming entity-relationship models, schema and relational model as initial steps in the
For the construction of this activity, the application developed in 1977 by Peter Chen is used as a
basis, (García-Molina)which has as essential modeling elements the entities, the relationships
and the attributes, which are the starting point for the development of this work and at the same
time to be able to carry out the relational model of the proposed exercise in order to apply the
Design the Entity-Relationship diagram, relational schema and model, data dictionary,
and constraints.
Problem
You are hired to build a database for the management of a mechanical workshop. The
database will include information about customers, cars, employees working in the workshop
ER Diagram Illustration1
Relational model
EMPLOYEE(employee_id:integer,ID:string,status_id:integer)
FOREIGN KEY: ID card REFERENCE ID card IN PERSON
STATUS(state_id:integer, state_description:string)
CUSTOMER(customer_id:integer,ID:string,address:string,landline:string,mobile_phone:string)
FOREIGN KEY: ID card REFERENCE ID card IN PERSON
Restrictions
The following restrictions have been established that arise from the execution of the process.
Table2 Restrictions
The physical model of the activity, designed in My SQL, and the SQL scripts with which it was
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`PERSON`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`PERSONA` (
`ID` INT NOT NULL,
`names` VARCHAR(45) NULL,
`last_name_1` VARCHAR(45) NULL,
`last_name_2` VARCHAR(45) NULL,
PRIMARY KEY (`ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`CLIENT`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`CLIENT` (
`client_id` INT NOT NULL,
`address` VARCHAR(120) NULL,
`landline` VARCHAR(45) NULL,
`mobile_phone` VARCHAR(45) NULL,
`PERSONA_cedula` INT NOT NULL,
PRIMARY KEY (`client_id`, `PERSON_ID`),
INDEX `fk_CLIENTE_PERSONA1_idx` (`PERSONA_cedula` ASC) VISIBLE,
CONSTRAINT `fk_CLIENT_PERSONA1`
FOREIGN KEY (`PERSONA_cedula`)
REFERENCES `mydb`.`PERSONA` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`VEHICLE`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`VEHICLE` (
`plate` VARCHAR(6) NOT NULL,
`model` VARCHAR(45) NULL,
`color` VARCHAR(45) NULL,
`brand` VARCHAR(45) NULL,
`CLIENTE_client_id` INT NOT NULL,
PRIMARY KEY (`board`, `CUSTOMER_customer_id`),
INDEX `fk_VEHICULO_CLIENTE1_idx` (`CUSTOMER_id_customer` ASC) VISIBLE,
CONSTRAINT `fk_VEHICLE_CUSTOMER1`
FOREIGN KEY (`CLIENTE_client_id`)
REFERENCES `mydb`.`CLIENTE` (`client_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`EMPLOYEE_STATUS`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`EMPLOYEE_STATUS` (
`id_state` INT NOT NULL,
`status_description` VARCHAR(45) NULL,
PRIMARY KEY (`id_estado`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`EMPLOYEE`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`EMPLOYEE` (
`employee_id` INT NOT NULL,
`PERSONA_cedula` INT NOT NULL,
`EMPLOYEE_STATUS_status_id` INT NOT NULL,
PRIMARY KEY (`employee_id`, `PERSON_ID`, `EMPLOYEE_STATUS_status_id`),
INDEX `fk_EMPLOYEE_PERSON1_idx` (`PERSON_ID` ASC) VISIBLE,
INDEX `fk_EMPLOYEE_STATUS_EMPLOYEE1_idx` (`EMPLOYEE_STATUS_status_id` ASC) VISIBLE,
CONSTRAINT `fk_EMPLOYEE_PERSON1`
FOREIGN KEY (`PERSONA_cedula`)
REFERENCES `mydb`.`PERSONA` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_EMPLOYEE_STATUS_EMPLOYEE1`
FOREIGN KEY (`EMPLOYEE_STATE_status_id`)
REFERENCES `mydb`.`EMPLOYEE_STATUS` (`status_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`SPARE`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`SPARE` (
`spare_id` INT NOT NULL,
`reference` VARCHAR(45) NULL,
`spare_description` VARCHAR(200) NULL,
`spare_brand` VARCHAR(45) NULL,
`price` FLOAT NULL,
PRIMARY KEY (`spare_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`SERVICE`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`SERVICE` (
`service_id` INT NOT NULL,
`service_description` VARCHAR(200) NULL,
`number_of_hours` INT NULL,
`vehicle_entry_datetime` DATETIME NULL,
`EMPLOYEE_employee_id` INT NOT NULL,
`VEHICLE_LICENSE_PLATE` VARCHAR(6) NOT NULL,
PRIMARY KEY (`service_id`, `EMPLOYEE_employee_id`, `VEHICLE_plate`),
INDEX `fk_SERVICIO_EMPLEADO1_idx` (`EMPLOYEE_employee_id` ASC) VISIBLE,
INDEX `fk_SERVICIO_VEHICULO1_idx` (`VEHICULO_placa` ASC) VISIBLE,
CONSTRAINT `fk_EMPLOYEE_SERVICE1`
FOREIGN KEY (`EMPLOYEE_employee_id`)
REFERENCES `mydb`.`EMPLOYEE` (`employee_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_VEHICLE_SERVICE1`
FOREIGN KEY (`VEHICLE_LICENSE_PLATE`)
REFERENCES `mydb`.`VEHICLE` (`license plate`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`INVOICE`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`INVOICE` (
`invoice_id` INT NOT NULL,
`fact_date` DATETIME NULL,
`spare_value` FLOAT NULL,
`service_hour_value` FLOAT NULL,
`subtotal_fact` FLOAT NULL,
`tax` FLOAT NULL,
`total_fact` FLOAT NULL,
`SERVICE_service_id` INT NOT NULL,
PRIMARY KEY (`invoice_id`, `SERVICE_service_id`),
INDEX `fk_FACTURA_SERVICIO1_idx` (`SERVICIO_id_servicio` ASC) VISIBLE,
CONSTRAINT `fk_INVOICE_SERVICE1`
FOREIGN KEY (`SERVICE_service_id`)
REFERENCES `mydb`.`SERVICE` (`service_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`table1`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`table1` (
)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`SERVICE_has_SPARE`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`SERVICIO_has_REPUESTO` (
`SERVICE_service_id` INT NOT NULL,
`SPARE_ID_spare` INT NOT NULL,
PRIMARY KEY (`SERVICE_service_id`, `SPARE_PART_spare_id`),
INDEX `fk_SERVICIO_has_REPUESTO_REPUESTO1_idx` (`REPUESTO_id_repuesto` ASC) VISIBLE,
INDEX `fk_SERVICIO_has_REPUESTO_SERVICIO1_idx` (`SERVICIO_id_servicio` ASC) VISIBLE,
CONSTRAINT `fk_SERVICE_has_SPARE_SERVICE1`
FOREIGN KEY (`SERVICE_service_id`)
REFERENCES `mydb`.`SERVICE` (`service_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_SERVICE_has_SPARE_PART_SPARE1`
FOREIGN KEY (`SPARE_id_repuesto`)
REFERENCES `mydb`.`SPARE` (`spare_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
Activity 2 Approach
Establish and write functions, procedures, cursors and triggers relevant to the proposed
The activity was developed in Oracle, with the user as owner of the schema, tables,
public synonyms, sequences, procedures, functions, among these is the use of cursors, and also
the implementation of triggers in the management of the key fields of each table.
The SQL codes used in the development of this activity are listed below.
/*CREATION OF TABLES*/
CREATE TABLE STATE(
STATUS_ID NUMBER(3,0) PRIMARY KEY,
DESCRIPTION_EST VARCHAR2(60));
BEGIN
IF (l_existe_frefrepuesto = 0 AND pi_reference IS NOT NULL AND pi_brand IS NOT NULL)
THEN
INSERT INTO SPARE PART
VALUES(NULL,
pi_reference,
pi_description,
pi_marca,
pi_price);
DBMS_OUTPUT.PUT_LINE(TO_CHAR(SQL%ROWCOUNT));
IF SQL%ROWCOUNT=1 THEN
COMMIT;
DBMS_OUTPUT.PUT_LINE('Spare part created successfully');
END IF;
ELSE
DBMS_OUTPUT.PUT_LINE('The spare part already exists: '||l_existe_refrepuesto||', reference: '||
pi_referencia||' and brand: '||pi_marca);
END IF;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20000,'Error: Spare part with reference'||pi_referencia);
END CREATE_SPARE;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20000,'Error: Updating spare'||pi_idrepuesto||' to service:'||
pi_idservicio);
END ASIGREP_SERVICE;
DBMS_OUTPUT.PUT_LINE(TO_CHAR(SQL%ROWCOUNT));
IF SQL%ROWCOUNT=1 THEN
COMMIT;
DBMS_OUTPUT.PUT_LINE('Employee assigned to service successfully');
END IF;
ELSIF l_employee_status = 1 THEN
DBMS_OUTPUT.PUT_LINE('Employee is not available: '||pi_idempleado);
ELSIF p_operacion = 'R' THEN --Remove employee
UPDATE SERVICE
SET employee_id = 0
WHERE id_servicio = pi_idservicio
AND employee_id = pi_employed_id;
IF SQL%ROWCOUNT=1 THEN
COMMIT;
DBMS_OUTPUT.PUT_LINE('Retired employee: '||pi_idempleado ||' from service: '||pi_idservicio);
END IF;
ELSE
DBMS_OUTPUT.PUT_LINE('The client does not exist: '||pi_idempleado||' or the service does not
exist'||pi_idservicio);
END IF;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20000,'Error: Updating employee'||pi_idempleado||' to service:'||
pi_idservicio);
END ASIGEMP_SERVICE;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20000,'Error: Service'||pi_idcliente||' Vehicle:'||pi_placa);
END CREATION_SERVICE;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20000,'Error: Vehicle'||pi_plate);
END CREATION_VEHICLE;
BEGIN
IF LENGTH(l_exists_person) > 0
THEN
DBMS_OUTPUT.PUT_LINE('The person exists: '||l_existe_persona);
INSERT INTO EMPLOYEE(ID,state_id)
VALUES(pi_cedula,pi_estado);
ELSE
DBMS_OUTPUT.PUT_LINE('The person does not exist: '||pi_cedula);
DBMS_OUTPUT.PUT_LINE('Data: '||pi_cedula||' It is being created.');
INSERT INTO PERSON
VALUES(pi_cedula,
pi_names,
pi_lastname1,
pi_lastname2);
IF SQL%ROWCOUNT=1 THEN
COMMIT;
INSERT INTO EMPLOYEE(ID,state_id)
VALUES(pi_cedula,pi_estado);
IF SQL%ROWCOUNT=1 THEN
COMMIT;
DBMS_OUTPUT.PUT_LINE('Person and employee created:'|| pi_cedula);
ELSE
DBMS_OUTPUT.PUT_LINE('An error occurred creating the employee');
END IF;
ELSE
DBMS_OUTPUT.PUT_LINE('An error occurred creating the person');
END IF;
END IF;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20000,'Error: Employee Creation'||pi_cedula);
END CREATE_EMPLOYEE;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20000,'Error: Person'||pi_cedula);
END CREATION_PERSON;
BEGIN
IF LENGTH(l_exists_person) > 0
THEN
DBMS_OUTPUT.PUT_LINE('The person exists: '||l_existe_persona);
INSERT INTO CUSTOMER(ID,address,mobile_phone,landline_phone)
VALUES(pi_cedula,pi_direccion,pi_telefono_movil,pi_telefono_fired);
ELSE
DBMS_OUTPUT.PUT_LINE('The person does not exist: '||pi_cedula);
DBMS_OUTPUT.PUT_LINE('Data: '||pi_cedula||' It is being created.');
INSERT INTO PERSON
VALUES(pi_cedula,
pi_names,
pi_lastname1,
pi_lastname2);
IF SQL%ROWCOUNT=1 THEN
COMMIT;
INSERT INTO CUSTOMER(ID,address,mobile_phone,landline_phone)
VALUES(pi_cedula,pi_direccion,pi_telefono_movil,pi_telefono_fired);
IF SQL%ROWCOUNT=1 THEN
COMMIT;
DBMS_OUTPUT.PUT_LINE('Person and client created:'|| pi_cedula);
ELSE
DBMS_OUTPUT.PUT_LINE('An error occurred creating the client');
END IF;
ELSE
DBMS_OUTPUT.PUT_LINE('An error occurred creating the person');
END IF;
END IF;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20000,'Error: Creating Client'||pi_cedula);
END CREATE_CLIENT;
o Check functions
ACID is a group of 4 properties that ensure that database transactions are performed
reliably. Each of these properties and how they can be secured are described below.
o Atomicity: It is the property that guarantees and verifies whether the transaction was carried
out or not and this property indicates that if an operation is not completed then it is cancelled.
For example, when inserting a record and the server goes down, a record was recorded
started and which ended, or by maintaining a copy of the data before changes occur.
Databases generally implement atomicity by using some logging system to track changes.
The system synchronizes logs as needed once changes occur successfully. The crash
o Consistency: It is the property that guarantees that transactions that can be completed
without problems will be executed. This concept has to do with the integrity of the database.
Prevents data from being changed and becoming meaningless or unreferenced. For example,
in our mechanical workshop activity, a customer cannot be deleted if they have used some
service of the workshop. If you want to delete the client, you will first have to delete all
time, they will be executed one after the other and if they are executed in parallel, each one
Most databases offer a number of transaction isolation levels, which control the degree of
locking that occurs when data is selected. For many applications, most transactions can be
constructed avoiding the higher isolation levels and thus reducing the load on the system due
to locks.
o Durability: This is the property that ensures that a transaction has been carried out and the
changes made by the transaction are permanent, even in the event of any problem such as
that can be reprocessed to recreate the state of the system before a failure. A transaction is
considered confirmed only after it has been entered into the log.
o Make backups: Companies usually make backups of their databases every hour, day or
week, depending on the size of the database and the available space. When something bad
happens, you can import data from the old database for any tables that were damaged or lost.
The data may be a little out of date, but having outdated data is better than having no data.
databases on different sides. If for some reason a particular copy of the database is not
available, then the query can be sent to another copy of the database that is available.
o Granting Privileges: Users and privileges need to be set up appropriately from the
beginning of database creation. As a general rule, there should only be a few users with full
access to the database (such as backend engineers), to reduce the risk of data loss and
Literature
Database Fundamentals. Fourth edition; regarding the fourth edition in Spanish, by McGRAW-
http://www.mcgraw-hill.es/olc/silberschatz
Garcia-Molina 1983 H. García-Molina, Using Semantic Knowledge for Transaction Processing
pages 186-213.
https://www.solvetic.com/tutoriales/article/1955-mysql-realizar-transacciones-acid-e-integridad-
referencial/
bases-de-datos