Advertisement
DevilVeNom

ADB_Labs

Sep 28th, 2023 (edited)
1,899
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 3.95 KB | None | 0 0
  1. -- Q.NO.1
  2. CREATE TABLE DEPARTMENT(
  3.     ID INT IDENTITY(1,1),
  4.     Dept_Name VARCHAR(50) NOT NULL,
  5.     Dept_Location VARCHAR(50) NOT NULL,
  6.     CONSTRAINT PK_DEPARTMENT_DNUMBER PRIMARY KEY(ID)
  7. )
  8. GO
  9.  
  10. -- Insert data in departments
  11. INSERT INTO DEPARTMENT (Dept_Name, Dept_Location)
  12. VALUES
  13.     ('Human Resources', 'Kathmandu'),
  14.     ('Marketing', 'Pokhara'),
  15.     ('Finance', 'Biratnagar'),
  16.     ('Engineering', 'Lalitpur');
  17.  
  18. GO
  19.  
  20. CREATE TABLE EMPLOYEE(
  21.     ID INT IDENTITY(1,1),
  22.     Fname VARCHAR(50) NOT NULL,
  23.     Mname VARCHAR(50),
  24.     Lname VARCHAR(50) NOT NULL,
  25.     FullName AS CONCAT(Fname, ' ', Lname),
  26.     Gender CHAR(1) NOT NULL,
  27.     DOB DATE NOT NULL,
  28.     [Address] VARCHAR(50) NOT NULL,
  29.     Salary DECIMAL(18,2) NOT NULL,
  30.     Supervisor_ID INT NOT NULL,
  31.     Dept_ID INT,
  32.     CONSTRAINT PK_EMPLOYEE_SSN PRIMARY KEY(ID),
  33.     CONSTRAINT FK_EMPLOYEE_DEPT_ID FOREIGN KEY(Dept_ID) REFERENCES DEPARTMENT(ID)
  34. )
  35. GO
  36.  
  37. -- Insert data in employee
  38. INSERT INTO EMPLOYEE (Fname, Mname, Lname, Gender, DOB, [Address], Salary, Supervisor_ID, Dept_ID)
  39. VALUES
  40.     ('Ramesh', 'Kumar', 'Shrestha', 'M', '1990-05-15', 'Kathmandu, Nepal', 55000.00, 1, 1)
  41. GO
  42.  
  43. ALTER TABLE EMPLOYEE
  44. ADD CONSTRAINT FK_EMPLOYEE_SUPERVISOR_ID FOREIGN KEY(Supervisor_ID) REFERENCES EMPLOYEE(ID)
  45. GO
  46.  
  47. INSERT INTO EMPLOYEE (Fname, Mname, Lname, Gender, DOB, [Address], Salary, Supervisor_ID, Dept_ID)
  48. VALUES
  49.     ('Sita', '', 'Rai', 'F', '1985-12-10', 'Pokhara, Nepal', 48000.00, 1, 2),
  50.     ('Hari', 'Bahadur', 'Tamang', 'M', '1993-08-25', 'Lalitpur, Nepal', 60000.00, 1, 3),
  51.     ('Gita', 'Devi', 'Thapa', 'F', '1988-03-20', 'Biratnagar, Nepal', 52000.00, 1, 4),
  52.     ('Nabin', 'Shankar', 'Bista', 'M', '1995-06-05', 'Kathmandu, Nepal', 56000.00, 1, 1);
  53. GO
  54.  
  55. CREATE TABLE PROJECT(
  56.     ID INT IDENTITY(1,1),
  57.     Pname VARCHAR(50) NOT NULL,
  58.     Dept_ID INT,
  59.     CONSTRAINT PK_PROJECT_PNUM_PLOC PRIMARY KEY(ID),
  60.     CONSTRAINT FK_PROJECT_DEPT_ID FOREIGN KEY(Dept_ID) REFERENCES DEPARTMENT(ID)
  61. )
  62. GO
  63.  
  64. -- Inserting values into the PROJECT table
  65. INSERT INTO PROJECT (Pname, Dept_ID)
  66. VALUES
  67.     ('Project A', 1),
  68.     ('Project B', 2),
  69.     ('Project C', 1),
  70.     ('Project D', 3);
  71. GO
  72.  
  73. CREATE TABLE [DEPENDENT](
  74.     ID INT IDENTITY(1,1),
  75.     EMP_ID INT NOT NULL,
  76.     Dependent_Name VARCHAR(50) NOT NULL,
  77.     Gender CHAR(1) NOT NULL,
  78.     DOB DATE NOT NULL,
  79.     Relationship VARCHAR(50) NOT NULL,
  80.     CONSTRAINT PK_DEPENDENT_ID PRIMARY KEY(ID),
  81.     CONSTRAINT FK_DEPENDENT_EMP_ID FOREIGN KEY(EMP_ID) REFERENCES EMPLOYEE(ID)
  82. )
  83. GO
  84.  
  85. -- Inserting one dependent for each employee
  86. INSERT INTO [DEPENDENT] (EMP_ID, Dependent_Name, Gender, DOB, Relationship)
  87. VALUES
  88.     (1, 'Sarita Shrestha', 'F', '2010-02-28', 'Daughter'),
  89.     (2, 'Amit Rai', 'M', '2008-07-15', 'Son'),
  90.     (3, 'Sushma Tamang', 'F', '2015-09-10', 'Daughter'),
  91.     (4, 'Rohan Thapa', 'M', '2012-04-05', 'Son'),
  92.     (5, 'Sima Bista', 'F', '2017-11-20', 'Daughter');
  93. GO
  94.  
  95. SELECT TOP 1 FullName, D.Dept_Name, P.Pname, E.Salary
  96. FROM EMPLOYEE E
  97.         INNER JOIN DEPARTMENT D ON E.Dept_ID = D.ID
  98.         INNER JOIN PROJECT P ON P.Dept_ID = D.ID
  99. ORDER BY E.Salary DESC
  100.  
  101. ==========================================================================================================================
  102.  
  103. -- Q.NO. 2
  104.  
  105. CREATE TABLE EMPLOYEE(
  106.     ID INT IDENTITY(1,1)
  107.     , [NAME] VARCHAR(50)
  108.     , OCCUPATION VARCHAR(50)
  109.     , WORKING_DATE DATE
  110.     , WORKING_HOUR INT
  111.     , CONSTRAINT PK_EMPLOYEE_ID PRIMARY KEY(ID)
  112. )
  113. GO
  114.  
  115.  
  116. CREATE TRIGGER BEFORE_EMPLOYEE_INSERT
  117. ON EMPLOYEE
  118. INSTEAD OF INSERT
  119. AS
  120. BEGIN
  121.     SET NOCOUNT ON;
  122.    
  123.     INSERT INTO EMPLOYEE([NAME], OCCUPATION, WORKING_DATE, WORKING_HOUR)
  124.     SELECT i.[name], i.OCCUPATION, i.WORKING_DATE, (CASE WHEN i.WORKING_HOUR >0 THEN i.WORKING_HOUR ELSE 0 END)
  125.     FROM inserted i
  126. END
  127. GO
  128.  
  129. SELECT * FROM EMPLOYEE
  130.  
  131. INSERT INTO EMPLOYEE([NAME], OCCUPATION, WORKING_DATE, WORKING_HOUR)
  132. VALUES  ('John Doe', 'Engineer', '2023-09-28', 8),
  133.         ('Jane Smith', 'Manager', '2023-09-28', -2),
  134.         ('Alice Johnson', 'Designer', '2023-09-27', 6),
  135.         ('Bob Brown', 'Technician', '2023-09-27', 0),
  136.         ('Eva Davis', 'Analyst', '2023-09-26', -4);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement