Create Table in MySQL
CREATE TABLE employees (
employee_id INT,
first_name VARCHAR(50),
last_name VARCHAR(50),
hourly_pay DECIMAL (5, 2),
hire_date DATE
);
This SQL code creates a table named employees
with the following columns:
-
employee_id: An integer (whole number) used to uniquely identify each employee.
-
first_name: A variable-length string (up to 50 characters) to store the first name of each employee.
-
last_name: A variable-length string (up to 50 characters) to store the last name of each employee.
-
hourly_pay: A decimal number with a precision of 5 digits and a scale of 2 digits (5 digits in total, 2 after the decimal point) to store the hourly pay rate of each employee.
-
hire_date: A date type to store the date when each employee was hired.
In summary, this SQL code sets up a table structure to store employee information including their names, hourly pay rate, and hire date
Renaming a Table in MySQL
RENAME TABLE employees to workers;
Deleting a Table in MySQL
DROP TABLE workers;
Add a Column to the Table
ALTER TABLE employees
ADD phone_number VARCHAR(15);
The word “ALTER” originates from the Latin word “alterare,” which means “to change” or “to modify.” In English, “alter” as a verb means to make changes or modifications to something.
Renaming Column in Table
ALTER TABLE employees
RENAME COLUMN phone_number TO email;
Modify DataType in Column
ALTER TABLE employees
MODIFY COLUMN email_address VARCHAR(100);
Re-Arranging Columns
ALTER TABLE employees
MODIFY email_address VARCHAR(100)
AFTER last_name;
This command move email address column next to the last_name column. and if u wanna add this to the first column, replace after last line with FIRST;
ALTER TABLE employees
MODIFY email_address VARCHAR(100)
FIRST;
Delete Column
ALTER TABLE employees
DROP COLUMN email_address;