RDBMS PROGRAMS
Key
A key is a minimal set of columns used to uniquely define any row in a table.
Primary key
When a single column used as a unique identifier it is known as primary
key. A single column key is used to describe each row if it is uniquely defines
each row.
Roll no Student_nm
1 Kumar
2 anand
3 vimal
(Fig 8) Primary Key
Here the column Roll_no uniquely identifies each row. Therefore the Roll_no
acts as a primary key.
CREATE TABLE
A table's structure, including column names, data types, and constraints
like NOT NULL, PRIMARY KEY, and CHECK, are defined when it is
created in SQL.
Syntax:
CREATE table table_name
(
Column1 datatype (size),
column2 datatype (size),
.
.
columnN datatype(size)
);
Key Terms
table_name: The name you assign to the new table.
column1, column2, ... : The names of the columns in the table.
datatype(size): Defines the data type and size of each column.
INSERT INTO command
INSERT INTO Customer (CustomerID, CustomerName, LastName,
Country, Age, Phone)
Syntax:
CREATE TABLE new_table_name AS
SELECT column1, column2,...
FROM existing_table_name
WHERE ....;
CREATE TABLE SubTable AS
SELECT CustomerID, CustomerName
FROM customer;
PROGRAM
1.Create a table for Employee details with Employee Number as primary key and
following fields:
Name,
Designation,
Gender,
Age,
Date of Joining and Salary.
Insert at least ten rows and perform various queries using any one Comparison,
Logical,
Set,
Sorting and Grouping operators.
creating an Employee Details table, inserting data, and performing various SQL
queries:
1. Create Table
CREATE TABLE EmployeeDetails (
EmployeeNumber INT PRIMARY KEY,
Name VARCHAR(50),
Designation VARCHAR(50),
Gender CHAR(1),
Age INT,
DateOfJoining DATE,
Salary DECIMAL(10, 2)
);
2. Insert Data
Copy the codeINSERT INTO EmployeeDetails (EmployeeNumber, Name,
Designation, Gender, Age, DateOfJoining, Salary)
VALUES
(101, 'Arun Kumar', 'Manager', 'M', 45, '2015-06-01', 75000.00),
(102, 'Meena Devi', 'HR', 'F', 30, '2020-03-15', 50000.00),
(103, 'Ravi Shankar', 'Developer', 'M', 28, '2018-07-10', 60000.00),
(104, 'Priya Ramesh', 'Developer', 'F', 26, '2019-11-20', 58000.00),
(105, 'Karthik Raja', 'Tester', 'M', 32, '2017-05-05', 45000.00),
(106, 'Divya Bharathi', 'HR', 'F', 29, '2021-01-12', 52000.00),
(107, 'Suresh Kumar', 'Manager', 'M', 50, '2010-09-25', 80000.00),
(108, 'Anitha Mohan', 'Tester', 'F', 27, '2022-06-18', 40000.00),
(109, 'Vikram Singh', 'Developer', 'M', 35, '2016-02-14', 65000.00),
(110, 'Lakshmi Narayan', 'Manager', 'F', 40, '2013-08-30', 78000.00);
3. Queries
a) Comparison Query: Find employees with a salary greater than ₹60,000.
Copy the codeSELECT * FROM EmployeeDetails
WHERE Salary > 60000;
b) Logical Query: Find female employees who are either HR or have a
salary above ₹50,000.
Copy the codeSELECT * FROM EmployeeDetails
WHERE Gender = 'F' AND (Designation = 'HR' OR Salary > 50000);
c) Set Query: Find employees who joined before 2018 or are Managers.
Copy the codeSELECT * FROM EmployeeDetails
WHERE DateOfJoining < '2018-01-01' OR Designation = 'Manager';
d) Sorting Query: Display all employees sorted by salary in descending
order.
Copy the codeSELECT * FROM EmployeeDetails
ORDER BY Salary DESC;
e) Aggregate Query: Find the average salary of all employees.
Copy the codeSELECT AVG(Salary) AS AverageSalary FROM
EmployeeDetails;
f) Group Query: Count the number of employees in each designation.
Copy the codeSELECT Designation, COUNT(*) AS EmployeeCount
FROM EmployeeDetails
GROUP BY Designation;