Advertisement
skylight_animation

Database Constraints | Create Table with Constraints MYSQL

Apr 30th, 2020
4,438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.20 KB | None | 0 0
  1. -- create table User
  2. CREATE TABLE `user` (
  3.   `user_id` char(5) PRIMARY KEY,
  4.   `user_name` varchar(255) NOT NULL,
  5.   `user_phone` char(20) UNIQUE KEY NOT NULL,
  6.   `user_email` varchar(255) UNIQUE KEY NOT NULL,
  7.   `user_password` varchar(255) NOT NULL,
  8.   `user_balance` int(12) NOT NULL,
  9.   `user_status` enum('unvalid','validated','disabled') NOT NULL,
  10.   `user_role` enum('customer','admin','cashier') NOT NULL
  11. );
  12.  
  13. -- create table Product
  14. CREATE TABLE `product` (
  15.   `product_id` char(4) PRIMARY KEY,
  16.   `product_name` varchar(255) NOT NULL,
  17.   `product_price` int(11) NOT NULL,
  18.   `product_amount` int(11) NOT NULL
  19. );
  20.  
  21. -- create table Order
  22. CREATE TABLE `order` (
  23.   `order_id` char(15) PRIMARY KEY,
  24.   `order_date` datetime NOT NULL,
  25.   `order_payment` int(11) NOT NULL,
  26.   `user_id` char(5) NOT NULL,
  27.   FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`)
  28. );
  29.  
  30. -- create table Order detail
  31. CREATE TABLE `order_detail` (
  32.   `order_detail_id` char(19) PRIMARY KEY,
  33.   `order_detail_amount` int(11) NOT NULL,
  34.   `product_id` char(4) NOT NULL,
  35.   `order_id` char(15) NOT NULL,
  36.   FOREIGN KEY (`product_id`) REFERENCES `product` (`product_id`),
  37.   FOREIGN KEY (`order_id`) REFERENCES `order` (`order_id`)
  38. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement