Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello in this database i constantly get error for creating triggers, like the one i created in the last part of the code . can

Hello in this database i constantly get error for creating triggers, like the one i created in the last part of the code . can you please see that why i get an error when i set a trigger for the database tables? and please write a correct trigger example for me so i could use that.

CREATE DATABASE online_voting;

USE online_voting;

CREATE TABLE Voters ( VoterID INTEGER PRIMARY KEY, Fname VARCHAR(255), Name VARCHAR(255), Address VARCHAR(255), DOB DATE, Phone VARCHAR(255) );

CREATE TABLE Candidates ( CandidateID INTEGER PRIMARY KEY, Fname VARCHAR(255), Name VARCHAR(255), Party VARCHAR(255), Sex CHAR(1), DOB DATE );

CREATE TABLE Areas ( AreaCode INTEGER PRIMARY KEY, Property VARCHAR(255), CriminalRecord VARCHAR(255), Population INTEGER );

CREATE TABLE Wards ( WardNo INTEGER PRIMARY KEY, AreaCode INTEGER, FOREIGN KEY (AreaCode) REFERENCES Areas(AreaCode) );

CREATE TABLE Votes ( Cid INTEGER, Vid INTEGER, PRIMARY KEY (Cid, Vid), FOREIGN KEY (Cid) REFERENCES Candidates(CandidateID), FOREIGN KEY (Vid) REFERENCES Voters(VoterID) );

INSERT INTO Voters (VoterID, Fname, Name, Address, DOB, Phone) VALUES (1, 'John', 'Doe', '123 Main St', '1970-01-01', '555-555-1212');

INSERT INTO Candidates (CandidateID, Fname, Name, Party, Sex, DOB) VALUES (1, 'Jane', 'Smith', 'Democrat', 'F', '1980-05-05');

INSERT INTO Areas (AreaCode, Property, CriminalRecord, Population) VALUES (1, 'Urban', 'Low', 1000);

INSERT INTO Wards (WardNo, AreaCode) VALUES (1, 1);

INSERT INTO Votes (Cid, Vid) VALUES (1, 1);

INSERT INTO Voters (VoterID, Fname, Name, Address, DOB, Phone) VALUES (2, 'Alice', 'Johnson', '456 Maple Ave', '1985-07-07', '555-555-1212'), (3, 'Bob', 'Williams', '789 Oak St', '1995-12-31', '555-555-1212');

INSERT INTO Candidates (CandidateID, Fname, Name, Party, Sex, DOB) VALUES (2, 'Jim', 'Brown', 'Republican', 'M', '1970-01-01'), (3, 'Sally', 'Green', 'Independent', 'F', '1985-05-05');

INSERT INTO Areas (AreaCode, Property, CriminalRecord, Population) VALUES (2, 'Suburban', 'Moderate', 2000), (3, 'Rural', 'High', 500);

INSERT INTO Wards (WardNo, AreaCode) VALUES (2, 2), (3, 3);

INSERT INTO Votes (Cid, Vid) VALUES (2, 2), (3, 3);

CREATE TRIGGER AgeCheck BEFORE INSERT ON Candidates FOR EACH ROW BEGIN IF (YEAR(CURDATE()) - YEAR(NEW.DOB)) < 18 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Candidates must be at least 18 years old'; END IF; END;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Inductive Databases And Constraint Based Data Mining

Authors: Saso Dzeroski ,Bart Goethals ,Pance Panov

2010th Edition

ISBN: 1489982175, 978-1489982179

More Books

Students also viewed these Databases questions

Question

b. Explain how you initially felt about the communication.

Answered: 1 week ago