Vous êtes sur la page 1sur 31

Contents

1. CREATE TABLE Command................................................................................................................1

2. DESC Command................................................................................................................................1

3. INSERT INTO Command...................................................................................................................2

4. ALTER TABLE Commands...............................................................................................................3

5. UPDATE TABLE Commands............................................................................................................5

6. SELECT Command............................................................................................................................7

7. Aggregate Functions.......................................................................................................................16

8. Numeric Functions..........................................................................................................................18

9. String Functions..............................................................................................................................20

10. UNION, OUTER JOIN, INNER JOIN Commands............................................................................22

11. DELETE, DROP, TRUNCATE Commands......................................................................................28


Amity School of Engineering and Technology

1. CREATE TABLE Command


S.NO. Command Explanation Output Statement

1. CREATE TABLE Student(Name An SQL relation is defined using the SELECT * FROM Student;
varchar(20), Age integer(5), Section CREATE TABLE command
varchar(10), Address varchar(30), Marks
float(5));

Output

2. DESC Command
S.NO. Command Explanation Output Statement

2. DESC Student; DESC or DESCRIBE command DESC Student;


displays the table attributes along
with their size, type, (if defined) key
and if the column can accept null
values

Database Management System Program File Page 1 of 30


Amity School of Engineering and Technology

Output

3. INSERT INTO Command


S.NO. Command Explanation Output Statement

3. INSERT INTO Student VALUES ('Tanu', INSERT command is used to load SELECT * FROM Student;
'19', '3CS1', 'Sector - 102', '8.6'); data into relation. The values are
specified in the order in which the
INSERT INTO Student VALUES ('Preeti',
corresponding attributes are listed in
'19', '3CS2', 'Sector - 12', '7.2');
the relation schema.
INSERT INTO Student VALUES ('Raj',
'19', '3CS3', 'Sector - 93A', '8.2');
INSERT INTO Student VALUES
('Ramsagar', '18', '2CS2', 'Sector - 13',
'4.9');
INSERT INTO Student VALUES ('Niyati',
'18', '2CS1', 'Sector - 37', '6.4');
INSERT INTO Student VALUES

Database Management System Program File Page 2 of 30


Amity School of Engineering and Technology

('Sheena', '18', '2CS3', 'Sector - 34', '8.9');


INSERT INTO Student VALUES
('Shyam', '17', '1CS1', 'Sector - 93A', '9.2');
INSERT INTO Student VALUES
('Shalini', '17', '1CS2', 'Sector - 44', '5.5');

Output

4. ALTER TABLE Commands


S.NO. Command Explanation Output Statement

4. ALTER TABLE Student ADD EnrlNo ALTER TABLE command is used SELECT * FROM Student;
varchar(10); to add attributes to an existing
relation. All tuples in the relation are
assigned null as the values for the
new attribute

Database Management System Program File Page 3 of 30


Amity School of Engineering and Technology

Output

S.NO. Command Explanation Output Statement

5. ALTER TABLE Student Sets EnrlNo column as primary key SELECT * FROM Student;
ADD PRIMARY KEY (EnrlNo);

Output

Database Management System Program File Page 4 of 30


Amity School of Engineering and Technology

S.NO. Command Explanation Output Statement


ALTER TABLE Student MODIFY Name
6. Changes the size of column Name to SELECT * FROM Student;
varchar(5); 5 from 10. Since some names are
exceeding the size limit therefore the
names ultimately displayed are
shortened

Output

5. UPDATE TABLE Commands


S.NO. Command Explanation Output Statement

7. UPDATE Student SET EnrlNo = '2001' UPDATE command helps to assign SELECT * FROM Student;
WHERE Name = 'Tanu'; values to the attributes or records or
change the same in the predefined

Database Management System Program File Page 5 of 30


Amity School of Engineering and Technology

UPDATE Student SET EnrlNo = '2002' relation


WHERE Name = 'Raj';
UPDATE Student SET EnrlNo = '2003'
WHERE Name = 'Ramsagar';
UPDATE Student SET EnrlNo = '2004'
WHERE Name = 'Niyati';
UPDATE Student SET EnrlNo = '2005'
WHERE Name = 'Sheena';
UPDATE Student SET EnrlNo = '2006'
WHERE Name = 'Shyam';
UPDATE Student SET EnrlNo = '2007'
WHERE Name = 'Shalini';

Output

S.NO. Command Explanation Output Statement

8. UPDATE Student SET EnrlNo = '2007' Will not allow duplicate values in UPDATE Student SET EnrlNo =
WHERE Name = 'Shalini'; primary key EnrlNo '2007' WHERE Name = 'Shalini';

Database Management System Program File Page 6 of 30


Amity School of Engineering and Technology

Output
Duplicate entry ‘207’ for key 1

6. SELECT Command
S.NO. Command Explanation Output Statement

9. SELECT Address This statement displays all the rows SELECT Address
under the column Address
FROM Student; FROM Student;

Output

Database Management System Program File Page 7 of 30


Amity School of Engineering and Technology

S.NO. Command Explanation Output Statement

10. SELECT DISTINCT Address This statement displays all the rows SELECT DISTINCT Address
under the column Address and
FROM Student; FROM Student;
removes the duplicate values

Output

S.NO. Command Explanation Output Statement

11. SELECT ALL Address This statement displays all the rows SELECT ALL Address
under the column Address
FROM Student; FROM Student;

Database Management System Program File Page 8 of 30


Amity School of Engineering and Technology

Output

S.NO. Command Explanation Output Statement

12. SELECT Name, Marks * 10 This statement displays all the rows SELECT Name, Marks * 10
under the column Name and will
FROM Student; FROM Student;
display all the elements of column
Merks by multiplying each element
by 10

Output

Database Management System Program File Page 9 of 30


Amity School of Engineering and Technology

S.NO. Command Explanation Output Statement

13. SELECT Name Displays those names that have SELECT Name
address Sector-93A
FROM Student FROM Student
WHERE Address = 'Sector - 93A'; WHERE Address = 'Sector -
93A';

Output

S.NO. Command Explanation Output Statement

14. SELECT SUM(Marks / 3), Age HAVING is used in aggregate SELECT SUM(Marks / 3), Age
functions and WHERE is used for
FROM Student FROM Student
general use
GROUP BY Age GROUP BY Age
HAVING SUM(Marks / 3) > '6.0'; HAVING SUM(Marks / 3) >
'6.0';

Database Management System Program File Page 10 of 30


Amity School of Engineering and Technology

Output

S.NO. Command Explanation Output Statement

15. SELECT Name Displays those names that have SELECT Name
address Sector-93A and has EnrlNo
FROM Student FROM Student
2003
WHERE Address = 'Sector - 93A' and WHERE Address = 'Sector -
EnrlNo = '2003'; 93A' and EnrlNo = '2003';

Output

S.NO. Command Explanation Output Statement

16. SELECT Name, Marks Displays those names and their SELECT Name, Marks
respective marks that have Marks
FROM Student FROM Student
falling between 8.0 and 9.0
WHERE Marks BETWEEN '8.0' AND WHERE Marks BETWEEN '8.0'
'9.0'; AND '9.0';

Database Management System Program File Page 11 of 30


Amity School of Engineering and Technology

Output

S.NO. Command Explanation Output Statement

17. SELECT Name, Marks * 10 AS This statement displays all the rows SELECT Name, Marks * 10 AS
Percentage under the column Name and will Percentage
display all the elements of column
FROM Student; FROM Student;
Marks by multiplying each element
by 10. AS renames the column
Marks * 10 to Percentage.

Output

S.NO. Command Explanation Output Statement

Database Management System Program File Page 12 of 30


Amity School of Engineering and Technology

18. SELECT Name, Marks This statement displays only those SELECT Name, Marks
rows under the column Name and
FROM Student FROM Student
Marks where Name starts with a
WHERE Name LIKE '_h%'; letter, then h and then any number of WHERE Name LIKE '_h%';
letters

Output

S.NO. Command Explanation Output Statement

19. SELECT Name, Marks This statement displays all the rows SELECT Name, Marks
under the column Name and Marks.
FROM Student FROM Student
The output is printed in descending
ORDER BY Name DESC; order of Name ORDER BY Name DESC;

Database Management System Program File Page 13 of 30


Amity School of Engineering and Technology

Output

S.NO. Command Explanation Output Statement

20. SELECT Name, Address, Marks This statement displays all the rows SELECT Name, Address, Marks
under the column Name, Address
FROM Student FROM Student
and Marks. The output is printed in
ORDER BY Address ASC, Marks DESC; ascending order of Address and ORDER BY Address ASC,
descending order of Marks(if two Marks DESC;
addresses are same then second
condition for arranging Marks in
descending order is followed for
those two addresses)

Database Management System Program File Page 14 of 30


Amity School of Engineering and Technology

Output

S.NO. Command Explanation Output Statement

21. SELECT * INTO StudInfo FROM A new table of name StudInfo is SELECT * INTO StudInfo
Student; created which is an exact replica of FROM Student;
table Student

Output

Database Management System Program File Page 15 of 30


Amity School of Engineering and Technology

S.NO. Command Explanation Output Statement

22. SELECT * All the columns are displayed and SELECT *


the repeated values of age are
FROM Student FROM Student
ignored
GROUP BY Age; GROUP BY Age;

Output

7. Aggregate Functions
S.NO. Command Explanation Output Statement

23. SELECT SUM(Marks / 8) AS Class_Avg Adds all the elements of Marks and SELECT SUM(Marks / 8) AS
divide the sum by 8. The column Class_Avg
FROM Student;
displayed is named as Class_Avg FROM Student;

Output

Database Management System Program File Page 16 of 30


Amity School of Engineering and Technology

S.NO. Command Explanation Output Statement

24. SELECT MIN(Marks), Age, MAX(Marks) Displays the minimum and SELECT MIN(Marks), Age,
maximum marks obtained, among MAX(Marks)
FROM Student
each age group FROM Student
GROUP BY Age;
GROUP BY Age;

Output

S.NO. Command Explanation Output Statement

25. SELECT COUNT(*), AVG(Marks), Age Displays the number of rows and the SELECT COUNT(*),
average of marks obtained for each AVG(Marks), Age
FROM Student
age group FROM Student
GROUP BY Age;
COUNT counts all records in a GROUP BY Age;
relation except for the repetition.
COUNT(*) also counts the
exceptions

Database Management System Program File Page 17 of 30


Amity School of Engineering and Technology

Output

8. Numeric Functions
S.NO. Command Explanation Output Statement

26. SELECT ABS(-15) FROM Dual; Displays the absolute value of the SELECT ABS(-15) FROM
number inside bracket Dual;

Output

S.NO. Command Explanation Output Statement

27. SELECT POWER(2,4) FROM Dual; Displays 2 power 4 i.e. 2 * 2 * 2 * 2 SELECT POWER(2,4) FROM
Dual;

Database Management System Program File Page 18 of 30


Amity School of Engineering and Technology

Output

S.NO. Command Explanation Output Statement

28. SELECT ROUND(2.345, 3), ROUND reduces the decimal SELECT ROUND(2.345, 3),
ROUND(2.345, 2), ROUND(2.345, 5) number to the number of places ROUND(2.345, 2),
FROM Dual; specified. Till 4 the number is not ROUND(2.345, 5) FROM Dual;
incremented, after 4 the number is
incremented

Output

S.NO. Command Explanation Output Statement

29. SELECT SQRT(3), EXP(1), 2 + 10 SQRT – Calculates square root SELECT SQRT(3), EXP(1), 2 +
"SUM" FROM Dual; 10 "SUM" FROM Dual;
EXP – Calculates e power the
number in brackets
SUM – Adds the number shown

Database Management System Program File Page 19 of 30


Amity School of Engineering and Technology

Output

S.NO. Command Explanation Output Statement

30. SELECT GREATEST(2, 5, 3), LEAST(2, GREATEST – Chooses greatest of SELECT GREATEST(2, 5, 3),
5, 3), MOD(24, 2) FROM Dual; the numbers in bracket LEAST(2, 5, 3), MOD(24, 2)
FROM Dual;
LEAST – Chooses least of the
numbers in bracket
MOD – Calculates 24 % 2 and prints
the remainder

Output

9. String Functions
S.NO. Command Explanation Output Statement

31. SELECT LOWER('TAnu'), LOWER - Converts all letters of SELECT LOWER('TAnu'),


UPPER('TAnu') FROM Dual; Tanu to lower case UPPER('TAnu') FROM Dual;
UPPER - Converts all letters of
Tanu to upper case

Database Management System Program File Page 20 of 30


Amity School of Engineering and Technology

Output

S.NO. Command Explanation Output Statement

32. SELECT SUBSTR('TANU KANVAR', 2, SUBSTR – Extracts the letters that SELECT SUBSTR('TANU
6), ASCII('TAnu'), LENGTH('TAnu') are defined within the bounds KANVAR', 2, 6),
FROM Dual; defined in bracket ASCII('TAnu'),
LENGTH('TAnu') FROM Dual;
ASCII – Calculates the ASCII value
LENGTH – Defines the length of
the word

Output

S.NO. Command Explanation Output Statement

33. SELECT INITCAP('tAnu') FROM Dual; Converts only the first letter to the SELECT INITCAP('tAnu')
upper case FROM Dual;

Database Management System Program File Page 21 of 30


Amity School of Engineering and Technology

Output

10. UNION, OUTER JOIN, INNER JOIN Commands


Creating another table for union, intersection and except commands
CREATE TABLE Fees(BankName varchar(20), Cheque_No varchar(10), Amount float(10), EnrlNo varchar(10), Bank_Loc
varchar(20), Sector varchar(10));
INSERT INTO Fees VALUES('ICICI', 'X123Y234', '70000', '2001', 'Noida', 'Sec-2');
INSERT INTO Fees VALUES('State Bank', 'A1276Y45', '140000', '2002', 'Noida', 'Sec-10')
INSERT INTO Fees VALUES('Axis Bank', 'T167U845', '140000', '2003', 'Delhi', 'Sec-90');
INSERT INTO Fees VALUES('State Bank', 'L137R545', '70000', '2004', 'Chandigarh', 'Sec-100');
INSERT INTO Fees VALUES('City Bank', 'H137P545', '70000', '2005', 'Ghaziabad', Sec-46');
INSERT INTO Fees VALUES('ICICI', 'K153Z230', '140000', '2006', 'Mumbai', 'Sec-10');
INSERT INTO Fees VALUES('City Bank', 'J153D420', '140000', '2007', 'Ghaziabad', 'Sec-12');
INSERT INTO Fees VALUES('City Bank', 'O153F420', '140000', '2008', 'Mumbai', 'Sec-15');
INSERT INTO Fees VALUES('Axis Bank', 'A153U420', '70000', '2009', 'Mumbai', 'Sec-27');

Database Management System Program File Page 22 of 30


Amity School of Engineering and Technology

Output

S.NO. Command Explanation Output Statement

34. SELECT * FROM Student For SELECT * the number of SELECT * FROM Student
columns of both the tables should be
UNION UNION
same. Here all the elements of 1st
SELECT * FROM Fees; column are added with the elements SELECT * FROM Fees;
of 1st column of another table in a
new table having attribute name as
that of 1st relation referred

Database Management System Program File Page 23 of 30


Amity School of Engineering and Technology

Output

S.NO. Command Explanation Output Statement

35. SELECT * Joins the two tables where all the SELECT *
elements of left table are included.
FROM student INNER JOIN Fees FROM student INNER JOIN
Only those elements of second table Fees
ON Student.EnrlNo = Fees.EnrlNo; are included. The rest are ignored.
ON Student.EnrlNo =
This statement is equivalent to Fees.EnrlNo;
INTERSECT

Database Management System Program File Page 24 of 30


Amity School of Engineering and Technology

Output

S.NO. Command Explanation Output Statement

36. SELECT * Joins the two tables where all the SELECT *
elements of left table are included.
FROM student LEFT OUTER JOIN Fees FROM student LEFT OUTER
The Values corresponding to those JOIN Fees
ON Student.EnrlNo = Fees.EnrlNo; values if present in second table then
those elements are displayed else ON Student.EnrlNo =
NULL is put Fees.EnrlNo;

Database Management System Program File Page 25 of 30


Amity School of Engineering and Technology

Output

S.NO. Command Explanation Output Statement

37. SELECT * Joins the two tables where all the SELECT *
FROM student RIGHT OUTER JOIN Fees elements of right table are included. FROM student RIGHT OUTER
The Values corresponding to those JOIN Fees
ON Student.EnrlNo = Fees.EnrlNo; values if present in first table then
those elements are displayed else ON Student.EnrlNo =
NULL is put Fees.EnrlNo;

Database Management System Program File Page 26 of 30


Amity School of Engineering and Technology

Output

S.NO. Command Explanation Output Statement

38. SELECT * Joins the two tables where all the SELECT *
elements of both tables are included.
FROM student FULL OUTER JOIN Fees FROM student FULL OUTER
The Values corresponding to those JOIN Fees
ON Student.EnrlNo = Fees.EnrlNo; values if present in both tables then
those elements are displayed else ON Student.EnrlNo =
NULL is put Fees.EnrlNo;

Database Management System Program File Page 27 of 30


Amity School of Engineering and Technology

Output

11.DELETE, DROP, TRUNCATE Commands


S.NO. Command Explanation Output Statement

39. DELETE FROM Student Deletes particular rows in a relation SELECT * FROM Student;
WHERE EnrlNo = '2008';

Database Management System Program File Page 28 of 30


Amity School of Engineering and Technology

Output

S.NO. Command Explanation Output Statement

40. DELETE FROM Fees; Removes all the records of the SELECT * FROM Fees;
relation though its attributes still
exist
Deletes the selected/all rows from a
table. Here we can delete set of
records by specifying the condition
in where clause

Output

Database Management System Program File Page 29 of 30


Amity School of Engineering and Technology

S.NO. Command Explanation Output Statement

41. TRUNCATE TABLE Student; Deletes all rows together. This is a SELECT * FROM Student;
faster way to delete the records of a
table than delete command. This
once used cannot be undone

Output

S.NO. Command Explanation Output Statement

42. DROP TABLE Student; Deletes the whole relation along SELECT * FROM Student;
with its structure.

Output
Table ‘Student’ does not exist

Database Management System Program File Page 30 of 30

Vous aimerez peut-être aussi