SQL Commands –
टेबल पहले से बनी हुई है और Primary Key सेट नहीं है:
ALTER TABLE Student ADD CONSTRAINT PK_RollNo PRIMARY
KEY(RollNo);
नई टेबल बनाते समय Primary Key जोड़ना है:
CREATE TABLE Student (
RollNo INT NOT NULL,
Name VARCHAR(50),
Age INT,
PRIMARY KEY (RollNo)
);
Drop Primary Key –
ALTER TABLE Student DROP CONSTRAINT PK_RollNo;
1. To Add new column in Employee table -
ALTER TABLE Employee ADD Age INT;
2. To drop column age -
ALTER TABLE Employee DROP COLUMN Age;
3. To Change in the column age -
ALTER TABLE Employee MODIFY Age VARCHAR(3);
4. To Change the name of the table -
ALTER TABLE Employee RENAME TO Staff;
5. To Change the name of the column in employee table -
ALTER TABLE Employee RENAME COLUMN Age TO EmployeeAge;
Que : CBSE 802 IT Class 12 SQL
a) Create the above table
Create table Employee
(
Employee_ID integer PRIMARY KEY,
Employee_Name varchar(20),
Job_title varchar(20),
Salary decimal(10),
Bonus int,
Age int,
Manager_ID int,
FOREIGN KEY (Manager_ID) REFERENCES Employee(Employee_ID)
);
b) Insert value as shown above.
Insert into Employee values(1201,’Divya’,’President’,50000, NULL, 29, NULL);
Insert into Employee values(1205,’AMYRA’,’MANAGER’,30000, 2500, 26,
1201);
.
.
.
.
C) Delete the Employee having Employee_ID 1217.
Delete from Employee where Employee_ID=1217;
d) Update the salary of ‘Amyra’ to 40000.
Update Employee SET Salary=40000 where Employee_Name=’Amyra’;
e) Alter the table Employee so that NULL values are not allowed for the age
column.
Alter Table Employee Modify Age Integer NOT NULL;
f) Write a query to display the names and salaries of those employees whose
salaries are greater than 20000.
Select Employee_Name, Salary from Employee where Salary>20000;
g) Write a query to display details of employees who are not getting any bonus.
Select *from Employee where Bonus is NULL;
h) Write a query to display the names of employees whose name contains “a” as
the last alphabet.
Select Employee_Name from Employee where Employee_Name LIKE “%a”;
i) Write a query to display the name and job title of those employees whose
Manager_ID is 1201.
Select Employee_Name, Job_Title from Employee where Manager_ID=1201;
j) Write a query to display the name and job title of those employees whose
Manager is “Amyra”.
Select Employee_Name, Job_title from Employee where Manager_ID=1205;
OR
Select Employee_Name, Job_title from Employee where Manager_ID =
( Select Employee_ID from Employee where Employee_Name=”Amyra”);
k) Select Employee_Name, Job_title from Employee where Age BETWEEN 26
and 30;
Select Employee_Name, Job_title from Employee where Age>=26 and
Age<=30;
Q2. A Railway company uses machines to sell tickets. The machine details and
daily sales information are recorded in two tables:
Ans. a) Create Table Machine
(
Machine_ID char(3) PRIMARY KEY,
Station char(30) NOT NULL
);
Create Table Sales
(
Machine_ID char(3)
Date Date,
Tickets_Sold Intger,
PRIMARY KEY(Machine_ID, Date),
FOREIGN KEY(Machine_ID)REFERENCES(Machine_ID)
);
c) Select Station, count(Machine_ID) from Machine Group by station;
d) Select date, sum(Income) from Sales, Machine where
Sales.Machine_ID=Machine.Machine_ID and Station= “New Delhi”;
e) Select sum(Tickets_sold) from Sales where Machine_ID=122;
or
Select sum(Tickets_sold) from sales where Machine_ID=122;