mirror of
https://github.com/Alvin-Zilverstand/Spik-en-span.git
synced 2026-03-06 13:26:49 +01:00
add unique_strings table and trigger for generating unique user codes
This commit is contained in:
21
database.sql
21
database.sql
@@ -30,6 +30,27 @@ CREATE TABLE scanned_tickets (
|
||||
UNIQUE (ticket_id)
|
||||
);
|
||||
|
||||
-- Table for storing unique strings for users
|
||||
CREATE TABLE unique_strings (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
user_id INT NOT NULL,
|
||||
unique_code CHAR(6) NOT NULL UNIQUE,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES employees(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- Trigger to automatically generate a unique 6-character alphanumeric string for each user
|
||||
DELIMITER $$
|
||||
CREATE TRIGGER generate_unique_code
|
||||
AFTER INSERT ON employees
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
DECLARE unique_code CHAR(6);
|
||||
SET unique_code = (SELECT SUBSTRING(CONCAT(MD5(RAND()), MD5(RAND())), 1, 6));
|
||||
INSERT INTO unique_strings (user_id, unique_code) VALUES (NEW.id, unique_code);
|
||||
END$$
|
||||
DELIMITER ;
|
||||
|
||||
-- Insert a default employee account (username: admin, password: password)
|
||||
INSERT INTO employees (username, password_hash)
|
||||
VALUES ('admin', SHA2('password', 256)); -- Replace with a secure password hashing method
|
||||
|
||||
Reference in New Issue
Block a user