Consider the student relation containing the attributes ’ID’, ’Dept_name’, ’Credits’. Write a trigger statement in sql that doesn’t allow inserting students data having ’Credits’ more than 10.0
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below is an example of a SQL trigger statement that prevents the insertion of student data with credits exceeding 10.0. This example assumes you are using a database management system like MySQL, PostgreSQL, Microsoft SQL Server, or Oracle, as the trigger syntax can vary slightly between systems. I'll provide examples for MySQL, PostgreSQL, SQL Server, and Oracle, as these are four of the most commonly used databases.
For MySQL:
sql
DELIMITER $$
CREATE TRIGGER tr_limit_credits_before_insert
BEFORE INSERT ON student
FOR EACH ROW
BEGIN
IF NEW.Credits IS NULL OR NEW.Credits > 10.0 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cannot insert student with credits more than 10.0 or NULL value';
END IF;
END$$
DELIMITER ;
For PostgreSQL:
sql
CREATE OR REPLACE FUNCTION limit_credits_on_insert()
RETURNS TRIGGER AS $$
BEGIN
IF NEW.Credits IS NULL OR NEW.Credits > 10.0 THEN
RAISE EXCEPTION 'Cannot insert student with credits more than 10.0 or NULL value';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER tr_limit_credits_before_insert
BEFORE INSERT ON student
FOR EACH ROW
EXECUTE PROCEDURE limit_credits_on_insert();
For SQL Server:
sql
CREATE FUNCTION limit_credits_on_insert()
RETURNS @valid_credits TABLE (valid BIT)
AS
BEGIN
INSERT INTO @valid_credits (valid)
SELECT CASE
WHEN i.Credits IS NULL OR i.Credits > 10.0 THEN 0
ELSE 1
END AS valid
FROM inserted i;
DECLARE @is_valid BIT;
SELECT TOP 1 @is_valid = valid FROM @valid_credits;
IF @is_valid = 0
BEGIN
RAISERROR ('Cannot insert student with credits more than 10.0 or NULL value', 16, 1);
ROLLBACK TRANSACTION;
END;
RETURN;
END;
GO
CREATE TRIGGER tr_limit_credits_before_insert
ON student
AFTER INSERT
AS
BEGIN
DECLARE @count INT;
SELECT @count = COUNT(*) FROM inserted i INNER JOIN student s ON i.ID = s.ID WHERE i.Credits IS NULL OR i.Credits > 10.0;
IF @count > 0
BEGIN
RAISERROR ('Cannot insert student with credits more than 10.0 or NULL value', 16, 1);
ROLLBACK TRANSACTION;
END;
END;
For Oracle:
sql
CREATE OR REPLACE TRIGGER tr_limit_credits_before_insert
BEFORE INSERT ON student
FOR EACH ROW
DECLARE
BEGIN
IF :NEW.Credits IS NULL OR :NEW.Credits > 10.0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Cannot insert student with credits more than 10.0 or NULL value');
END IF;
END;
/
Explanation:
MySQL Version:
DELIMITER $$: Changes the delimiter to $$ to allow using semicolons within the trigger definition.
CREATE TRIGGER tr_limit_credits_before_insert: Names the trigger.
BEFORE INSERT ON student: Specifies the trigger should activate before an insert operation on the student table.
FOR EACH ROW: Applies the trigger for each row being inserted (necessary for row-level checks).
SIGNAL SQLSTATE '45000': Throws an error with a custom message when the condition is met. 45000 is a generic code for "unhandled user-defined exception".
NEW.Credits IS NULL checks for invalid or missing values, enhancing robustness.
PostgreSQL Version:
CREATE OR REPLACE FUNCTION limit_credits_on_insert(): Defines a reusable function for the trigger. If it already exists, it gets replaced.
RETURNS TRIGGER AS $$ ... END; $$ LANGUAGE plpgsql;: Specifies the function returns a trigger and defines its body in PL/pgSQL.
CREATE TRIGGER tr_limit_credits_before_insert ... EXECUTE PROCEDURE limit_credits_on_insert();: Creates the trigger, linking it to the defined function.
NEW.Credits IS NULL checks for invalid or missing values, enhancing robustness.
RAISE EXCEPTION is used to stop execution and report the error, which is specific to PostgreSQL.
SQL Server Version:
CREATE FUNCTION limit_credits_on_insert(): Defines a function to validate credits.
RETURNS @valid_credits TABLE (valid BIT): Returns a table variable indicating whether the credits are valid.
INSERT INTO @valid_credits ... SELECT CASE ...: Checks each inserted row's credits and marks invalid ones.
RAISERROR is used to raise a custom error message, and ROLLBACK TRANSACTION cancels the insert if invalid credits are found.
Note: SQL Server does not support FOR EACH ROW triggers like MySQL or PostgreSQL. Instead, it uses a single trigger execution per statement, checking all rows in the inserted virtual table.
Oracle Version:
CREATE OR REPLACE TRIGGER tr_limit_credits_before_insert: Defines a trigger that replaces any existing one with the same name.
Testing the Trigger:
To ensure the trigger works as expected, you can perform a simple test: