Ex 1:
Create a table called Employee & execute the following. Employee(EMPNO,ENAME,JOB, MANAGER_NO, SAL, COMMISSION) 1. Create a user and grant all permissions to theuser. 2. Insert the any three records in the employee table contains attributes EMPNO,ENAME JOB, MANAGER_NO, SAL, COMMISSION and use rollback. Check the result. 3. Add primary key constraint and not null constraint to the employee table. 4. Insert null values to the employee table and verify the result
I. Create a user and grant all permissions to the user.
CREATE USER 'emp_jones'@'localhost' IDENTIFIED BY 'emp123';
GRANT ALL PRIVILEGES ON *.* TO 'emp_user'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
What this does:
1 Create a user
CREATE USER 'emp_user'@'localhost' IDENTIFIED BY 'emp123';
emp_user → username
@'localhost' → user can log in only from the local machine
IDENTIFIED BY 'emp123' → sets the password
2 Grant all privileges
GRANT ALL PRIVILEGES ON *.* TO 'emp_user'@'localhost' WITH GRANT OPTION;
EXPLANATION
ALL PRIVILEGES→ gives all permissions (SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, etc.)-
*.*→ applies to all databases and all tables -
WITH GRANT OPTION→ allowsemp_userto grant permissions to other users
Result:
emp_user has full control over the entire MySQL server.
⚠️ Note: This is fine for lab/practice, but unsafe for production.
3 Apply changes immediately
FLUSH PRIVILEGES;
EXPLANATION
Reloads the privilege tables
-
Ensures the new permissions take effect immediately
Result:
No need to restart MySQL — changes are active right away.
Result:
A new MySQL user named emp_user is created with password emp123.
Step 1: Make EMPNO the Primary Key
This means no duplicate EMPNO and no empty EMPNO.
note:
This line tells the database:
“EMPNO is the main ID for each employee.”
ALTER TABLE EMPLOYEE
ADD PRIMARY KEY (EMPNO);
Step 2: Make columns NOT NULL
This means these columns must have values.
“ENAME, JOB, and SAL cannot be empty/Null.”
Before Make columns NOT NULL
After Make columns NOT NULL
cant use this commamd for Empno, Ename,Job,Sal because we changed these as NOT NULL.
So we do remaining Managerno and Commission.
SOLUTION:
Insert NULL into nullable columns (works)
INSERT INTO EMPLOYEE (EMPNO, ENAME, JOB, SAL, MANAGER_NO, COMMISSION)
VALUES (202, 'Ravi', 'Sales', 18000, NULL, NULL);
No comments:
Post a Comment