Skip to content

Instantly share code, notes, and snippets.

@rts-rob
Created September 11, 2017 06:23
Show Gist options
  • Save rts-rob/5ddab18719ffbe43e72e5c2b4dda30b1 to your computer and use it in GitHub Desktop.
Save rts-rob/5ddab18719ffbe43e72e5c2b4dda30b1 to your computer and use it in GitHub Desktop.
User and Type example showing foreign key constraints and builtin uuid() function for generating id
CREATE TABLE userType (
id int NOT NULL,
description varchar(255) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE user (
id char(32) NOT NULL, -- use uuid() on INSERT
username varchar(24) NOT NULL, -- rob
passwordHash char(40) NOT NULL, -- c8fed00eb2e87f1cee8e90ebbe870c190ac3848c
passwordSalt char(36) NOT NULL, -- 880c991a-1827-4d60-8491-db415dcd1914
userTypeId int NOT NULL,
PRIMARY KEY (`id`),
-- FOREIGN KEY (`userTypeId`) REFERENCES `mydb`.`userType` (`id`)
CONSTRAINT `fk_user_userType`
FOREIGN KEY (`userTypeId`)
REFERENCES `mydb`.`userType` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
);
INSERT INTO user (id, username, password)
VALUES (uuid(), 'rob', 'somepassword');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment