Lab 1
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 the user.
2. Insert any three records in the employee table containing 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.
Solution:
create database mydatabase1;
--1. creating employee table
CREATE TABLE Employee1 (
EMPNO INT,
ENAME VARCHAR(50),
JOB VARCHAR(50),
MANAGER_NO INT,
SAL DECIMAL(10, 2),
COMMISSION DECIMAL(10, 2)
);
---------------------------------------------------
--2. Create a user
CREATE LOGIN myUser WITH PASSWORD = 'myPassword';
-- Create a user in the database
CREATE USER myUser FOR LOGIN myUser;
-- Grant all privileges to the user (assuming 'ALL PRIVILEGES' means
all permissions)
GRANT ALL TO myUser;
-----------------------------------------------------
--3. Insert records
INSERT INTO Employee1 (EMPNO, ENAME, JOB, MANAGER_NO, SAL,
COMMISSION) VALUES (1, 'deepika', 'Manager', 101, 5000.00, 100.00);
INSERT INTO Employee1 (EMPNO, ENAME, JOB, MANAGER_NO, SAL,
COMMISSION) VALUES (2, 'pooja', 'tech lead', 1, 3000.00, 50.00);
INSERT INTO Employee1 (EMPNO, ENAME, JOB, MANAGER_NO, SAL,
COMMISSION) VALUES (3, 'lakshmi', 'software devloper', 1, 2500.00,
25.00);
select * from Employee1;
-- 4. Add not null constraint to EMPNO column
ALTER TABLE Employee1
ALTER COLUMN EMPNO INT NOT NULL;
--4. Add primary key constraint to EMPNO column
ALTER TABLE Employee1
ADD CONSTRAINT PK_Employee_EMPNO PRIMARY KEY (EMPNO);
--------------------------------------------------------------
--5. Inserting NULL values into the Employee table
INSERT INTO Employee1 (EMPNO, ENAME, JOB, MANAGER_NO, SAL,
COMMISSION)
VALUES (4, 'bhavya', NULL, NULL, NULL, NULL),
(5, NULL, 'Manager', NULL, NULL, NULL),
(6, 'sichana', 'Clerk', 1, NULL, NULL);
-- Verify the result
SELECT * FROM Employee1;