0% found this document useful (0 votes)
11 views

MySql Database commands

The document outlines the creation of a PHP CMS database with various tables including users, roles, user_roles_mapping, categories, and blogs. Each table is defined with specific fields, data types, and relationships through foreign keys to ensure data integrity and traceability. Notably, the document highlights the nullable and non-nullable constraints for certain fields to manage user creation and updates effectively.

Uploaded by

shifaamir04
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

MySql Database commands

The document outlines the creation of a PHP CMS database with various tables including users, roles, user_roles_mapping, categories, and blogs. Each table is defined with specific fields, data types, and relationships through foreign keys to ensure data integrity and traceability. Notably, the document highlights the nullable and non-nullable constraints for certain fields to manage user creation and updates effectively.

Uploaded by

shifaamir04
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

PHP CMS : On localhost phpmyadmin:

--------------------------------------

-Create Database:
create database phpCMS;

-Create Table:

-Table users
CREATE TABLE users (
user_id INT AUTO_INCREMENT PRIMARY KEY,
user_name VARCHAR(100) NOT NULL,
user_email VARCHAR(50) NOT NULL,
user_password VARCHAR(50) NOT NULL,
user_status ENUM('active', 'inactive'),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
created_by INT,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
updated_by INT NOT NULL,
last_login TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (created_by) REFERENCES users(user_id),
FOREIGN KEY (updated_by) REFERENCES users(user_id)
);

-NUll and NOT NULL:


-created_by Nullable: Allows flexibility during the initial record creation. The
system or an unknown entity can create the user without enforcing an immediate
reference to another user.
-updated_by NOT NULL: Enforces a strict rule that every update must be performed by
a known user, thus ensuring every change is traceable to a specific user.

-Table roles
CREATE TABLE roles (
role_id INT AUTO_INCREMENT PRIMARY KEY,
role_name VARCHAR(100) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
created_by INT,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
updated_by INT NOT NULL,
FOREIGN KEY (created_by) REFERENCES users(user_id),
FOREIGN KEY (updated_by) REFERENCES users(user_id)
);

-Table user_roles_mapping
CREATE TABLE users_roles_mapping (
user_id INT,
role_id INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
created_by INT,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
updated_by INT NOT NULL,
FOREIGN KEY (created_by) REFERENCES users(user_id),
FOREIGN KEY (updated_by) REFERENCES users(user_id),
FOREIGN KEY (user_id) REFERENCES users(user_id),
FOREIGN KEY (role_id) REFERENCES roles(role_id)
);

-Table categories
CREATE TABLE categories (
category_id INT AUTO_INCREMENT PRIMARY KEY,
created_by INT,
category_name VARCHAR(100) NOT NULL,
category_slug VARCHAR(100) NOT NULL,
parent_category_id INT,
category_image VARCHAR(300),
category_description LONGTEXT(200),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_by INT NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (created_by) REFERENCES users(user_id),
FOREIGN KEY (updated_by) REFERENCES users(user_id),
FOREIGN KEY (parent_category_id) REFERENCES categories(category_id)
);

-Table blogs
CREATE TABLE blogs (
blog_id INT AUTO_INCREMENT PRIMARY KEY,
created_by INT,
blog_title LONGTEXT NOT NULL,
blog_description LONGTEXT NOT NULL,
blog_date DATE,
category_id INT,
blog_tags VARCHAR(100) NOT NULL,
blog_meta_title VARCHAR(100) NOT NULL,
blog_meta_description LONGTEXT NOT NULL,
blog_keywords VARCHAR(100) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_by INT NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (created_by) REFERENCES users(user_id),
FOREIGN KEY (updated_by) REFERENCES users(user_id),
FOREIGN KEY (category_id) REFERENCES categories(category_id)
);

You might also like