SQL Commands
SQL Commands
3.INSERT Command
We can add records in the object
Syntax1:
INSERT INTO <tablename> VALUES (value1,
value2….. valuen);
Example
INSERT INTO student
VALUES(1,’amit’,23,’amnit@gmail.com’);
4.Drop Command
Use : its used to delete the database object
Syntax: DROP
TABLE<databaseobjectname>;
Example DROP TABLE Student;
SQL Constraints
Constraints in SQL helps to ensure that
the data entered by the user is correct.
SQL supports 6 types of constraints
1.Primary key: to uniquely identify each
record in the table.
Primary key: Not NULL + unique
Example
Create table student
(
Sid number PRIMARY KEY,
Sname char(25),
Marks number
);
Create table student
(
Sid number,
Sname char(25),
Marks number,
Emailed char(25),
PRIMARY KEY (sid)
);
2.NOT NULL
It enforce that the column can not be
left blank. The field or the column will
always contain a value.
Example 1
Example 2
Create table student
(
Sid number,
Sname char(25),
Marks number,
Emailed char(25),
PRIMARY KEY (sid),
NOT NULL (sname)
);
3.Unique Constraint
Identifies each record uniquely in a
database table.
Example 1
Create table student
(
Sid number PRIMARY KEY,
Sname char(25) NOT NULL,
Marks number
Email char(30) UNIQUE
);
Example 2
4.CHECK Constraint:
It is used to limit the value range that
can be placed in a column.
Example
CREATE TABLE Persons
(
P_id number PRIMARY KEY,
P_Name Char(40) NOT NULL,
Address char(40) UNIQUE,
City varchar(30),
CHECK (P_id>0)
);
5.DEFAULT constraint
Example
CREATE TABLE Persons
(
P_id number PRIMARY KEY,
P_Name Char(40) NOT NULL,
Address char(40) UNIQUE,
City varchar(30) DEFAULT ‘DELHI’,
CHECK (P_id>0)
);
6.Foreign Key Constraint
It is used to link two tables. It will be
more used in joins, subquery
Persons table
P_ID,
Lastname
Firstname
Address, City, Dept_id
( P_Id is the primary key)
Dept_id foreign key)
Persons table and department table
Example
CREATE TABLE PERSONS
(
P_ID number,
Lastname char(30),
Firstname char(30),
Address char(50),
City char(15)
Dept_id number,
PRIMARY KEY(P_ID),
FOREIGN KEY (DEPT_ID) REFERENCES
DEPARTMENT(DEPT_ID)
);
Sql CONSTRAINTS
1.PRIMARY KEY
2.FOREIGN KEY
3.CHECK
4.DEFAULT
5.UNIQUE
6.NOT NULL
1.SELECT STATEMENT:
Use: it is used to select or retrieve the
data from the database.
Syntax:
EXAMPLE:
Q1. WSQ (Write SQL Query) TO VIEW
THE LIST OF TABLES PRESENT IN THE
DATABASE.
SYNTAX:
SELECT FROM < DATABASENAME>
ANSWER:
SELECT FROM JIMS;
( It will show all the tables present in
the jims database.)
Q2. WSQ to show the lastname and
firstname column from persons table
Logical Operator
1.AND: when both conditions are
true then it will be executed.
Truth Table
C1 C2 Output
T T T
T F F
F T F
F F F
2.OR: when any one of the condition
is true it will be executed
Truth Table
C1 C2 Output
T T T
T F T
F T T
F F F
3.NOT Operator
Opposite of the condition
Truth Table
C1 Output
T F
F T
Q8. WSQ to show all the records
from persons table sorted in the
ascending order of the firstname.
Example
ALTER TABLE employee ALTER
COLUMN eid char(30);
4.To rename the columnname in the
existing table.
SYNTAX:
ALTER TABLE <tablename> RENAME
<old columnname> TO <new
columname>;
Example
ALTER TABLE employee RENAME
eid TO empid;
5.To rename a complete table
Syntax:
ALTER TABLE <tablename> RENAME
TO <newtablename>
Example
ALTER TABLE emp RENAME TO
Employee;
DCL Command:
SELECT AVG(orderprice) AS
averageorderprice FROM orders;
Q WSQ to find the customername
from order table whose orderprice
is greater than the average
orderprice value.
SELECT COUNT(*) AS
noofcustomers FROM customer;
SELECT MAX(orderprice) AS
laregestprice FROM orders;
Student
StudentCourse
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
table1: First table.
table2: Second table
matching_column: Column common to both the tables.
This query will show the names and age of students enrolled in different
courses.
SELECT StudentCourse.COURSE_ID, Student.NAME, Student.AGE
FROM Student
INNER JOIN StudentCourse
ON Student.ROLL_NO = StudentCourse.ROLL_NO;
2 types of inner join
LEFT JOIN: This join returns all the rows of the table on the left side of the
join and matching rows for the table on the right side of join. The rows for
which there is no matching row on right side, the result-set will contain null.
LEFT JOIN is also known as LEFT OUTER JOIN.Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
table1: First table.
table2: Second table
matching_column: Column common to both the tables.
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
LEFT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
RIGHT JOIN: RIGHT JOIN is similar to LEFT JOIN. This join returns all the
rows of the table on the right side of the join and matching rows for the table
on the left side of join. The rows for which there is no matching row on left
side, the result-set will contain null. RIGHT JOIN is also known as RIGHT
OUTER JOIN.Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
RIGHT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
FULL JOIN: FULL JOIN creates the result-set by combining result of both
LEFT JOIN and RIGHT JOIN. The result-set will contain all the rows from
both the tables. The rows for which there is no matching, the result-set will
contain NULL values.Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
https://www.geeksforgeeks.org/sql-join-set-1-
inner-left-right-and-full-joins/