Untitled document
Untitled document
Categorisation of commands
Terminology
Database :
Advantages :
Terminology :
Relation:
and columns.
Tuples:
Attributes:
Degree:
Cardinality:
Eg
Database: Employee
Relations: Personal,Payslip
Personal
ID Nam Phone Address
e
Degree:4
Cardinality:2
Payslip
ID Month Amount
1 May 89200
2 May 12300
1 Jun 78000
2 Jun 20000
Degree:3
Cardinality:4
Eg.
Use school
Syntax:
Eg.
Regno int(3),
Name char(20),
Dob date,
Stream char(20)
);
Syntax:
Desc <tablename>
Eg.
Desc report
d. Insert command: Insert command is used to add records in
a table. It has two forms:
Form I:
Syntax:
Eg.
Important:
i. The form of insert can only be used when all fields of the
table are entered and in the same order as they are present.
Form II:
Syntax:
Eg.
Important:
ii. The field which is not specified will take a NULL value.
e. Select command:
Syntax
Eg
Syntax
Eg
Syntax
Eg1:
Eg2:
f. Operators in SQL:
i.Relational operators: These operators are used to compare the
values. <,>,>=.<=,!=.=.
Eg:
Eg1:
Eg2:
Syntax:
Eg1:
Eg2:
Syntax:
Eg1:
Eg2:
Eg3:
Syntax:
Eg1:
(Will Display the details of students who were born on the year
2008)
Eg3:
Eg1:
Eg2:
Important:
Note: count when used with * will count the complete rows and
when used with a field name will count the entries which are not
NULL.
Eg1:
(will display max marks for each stream individually along with
name of the stream)
Important:
Eg1:
Eg2:
Important:
1. Having cannot be used without a group by.
Syntax:
Eg1:
Eg2:
Eg1:
Eg2:
Eg1:
Eg2:
Eg3:
Syntax:
Eg1:
Syntax:
Syntax:
Or
Eg3:
Or
Syntax:
Eg4:
Syntax:
Alter table <tablename> rename <newtablename>
Eg5:
Syntax:
Eg1:
Eg2:
Eg3:
Eg1:
Eg2:
Syntax:
Eg:
Concept of Keys
Domain: This refers to the pool from which actual value appears. For eg. regno of
a student table may be drawn from a domain of valid regno assigned in a table.
Personal Table
The values specified in the circle are the values from which the Regno of
examination table will withdraw its data. This area is known as domain
Examination Table
1011 1 90
1022 1 45
1033 2 89
1044 1 43
1011 2 78
Keys
Primary Key: A primary key is a set of one or more attributes that can uniquely
identify tuples within a relation.
Alternate Key: A candidate key that is not a primary key is termed as alternate
key.
Constraints in SQL
The constraints are the restrictions which are applied to a field or group of fields
to ensure validity of data. The constraints can be applied during creation of table,
while using create table command or after creation of table using Alter table
command.
SQL constraints
Please Note and read following description twice after going through above
command:
1. Primary key and Unique both ensure that the field cannot have duplicate
values. The difference is Primary key constraint can be applied only once and
unique constraint can be applied multiple times in table.
1. Field level Constraint: This constraint involves only one field of the table. These
constraints are specified next to the field of the table.
Concept of Keys
Domain: This refers to the pool from which actual value appears. For eg. regno of
a student table may be drawn from a domain of valid regno assigned in a table.
Personal Table
The values specified in circle, are the values from which Regno of examination
table will withdraw its data. This area is known as domain
Examination Table
Regno Term Marks
1011 1 90
1022 1 45
1033 2 89
1044 1 43
1011 2 78
Keys
Primary Key: A primary key is a set of one or more attributes that can uniquely
identify tuples within a relation.
Alternate Key: A candidate key that is not a primary key is termed as alternate
key.
Constraints in SQL
The constraints are the restrictions which are applied to a field or group of fields
to ensure validity of data. The constraints can be applied during creation of table,
while using create table command or after creation of table using Alter table
command.
SQL constraints
1. Not Null: Ensures that column cannot have null values.
Table :DATA
Please Note and read following description twice after going through above
command:
1. Primary key and Unique both ensure that filed cannot have duplicate
values. The difference is Primary key constraint can be applied only once and
unique constraint can be applied multiple times in table.
Foreign Key Constraint
Foreign Key constraint ensures referential integrity. Referential integrity is a
system of rules that a DBMS uses to ensure that relationship between records of
related table are valid , and that users don not accidentally delete or change data.
All the cases have been explained while discussion of foreign key. Please refer to
that part.
Defining Foreign Key: A field is eligible to be foreign key in a table , if and only if
it is primary kay in the related table .
Personal Table
Examination Table
1022 1 45
1033 2 89
1044 1 43
1011 2 78
1024 1 78
(regno int(4),
Term int(1),
Marks float(3),
Please Note:
(enrollment_no int(4),
Term int(1),
Marks float(3),
Personal Table
Examination Table
1011 1 90
1022 1 45
1033 2 89
1044 1 43
1011 2 78
Foreign Key: A non-key attribute whose values are derived from primary key of
some other table.
Personal Table
Examination Table
1011 1 90
1022 1 45
1033 2 89
1044 1 43
1011 2 78
examination table.
What will foreign key ensures? Please carefully check the records of personal
and examination table.
1.Will allow the foreign key field to have any value unless and until its present in
Personal table
Insert into examination values(1024,1,45);, will flag an error as 1024 is not present
in personal table.
1011 1 90
1024 1 45
1033 2 89
1044 1 43
1011 2 78
Second entry(marked as red ) won’t be allowed because Personal table does not
contain 1024 as Regno.
2. If user tries to change regno from 1011 to 1012 in personal table. The update
command will flag an error because examination table contains 1011.
Update personal set regno=1012 where regno=1011, will flag an error.
Personal Table
Examination Table
1011 1 90
1022 1 45
1033 2 89
1044 1 43
1011 2 78
3. If user tries to delete record of a student with regno 1011 from personal table
The delete command will flag an error.
Delete from personal where regno=1011;, will flag an error because examination
table contains 1011.
Personal Table
Examination Table
1011 1 90
1022 1 45
1033 2 89
1044 1 43
1011 2 78
Sql provides us the option to extract data from more than one table. Before we
start learning about extraction of data. Lets look at JOINS.
A join is a query that combines rows from two or more tables. In a join query
more than one table name is listed with FROM clause. The operation of
combining data of multiple tables is termed as joining.
1. Cross join
2. Equi join
3. Natural Join
Cross Join: A sql query without any join condition generates cartesian product
.This means each record of first table is combined with each record of second
table. For eg if table1 contains 10 records and table2 cobtains 5 records ….
The above command is trying to retrieve records from table1 and table2. This
command will yield 50 rows ie 10 X5=50
Personal Table
1011 Amit
1022 Kabir
1033 Simon
Examination Table
1011 1 90
1022 1 45
1033 2 89
1011 2 78
1022 1 34
1033 1 60
Examination = 6 records.
Select * from personal ,examination;
Output=18 records
Please check each record of personal is combined with each row of examination
table.
The statement: Select * from personal ,examination; creates what we called in SQL a
cross join. The output obtained is not a valid output.
Equi Join: The join, in which column are compared for equality.
Personal Table
1011 Amit
1022 Kabir
1033 Simon
Examination Table
Regno(Foreign Key) Term Marks
1011 1 90
1022 1 45
1033 2 89
1011 2 78
1022 1 34
1033 1 60
The command will pick name from personal table and marks from examination
table. It will compare the regno of personal table with regno of examination table.
If the value matches th the record will be shown.
Name Marks
Amit 90
Amit 78
Kabir 45
Kabir 34
Simon 89
Simon 60
Situation II: Display name ,Term , marks of those students who are scoring marks
less than 50.
Or
Kabir 1 45
Kabir 1 34
Or
Simon 89
Amit 78
Natural Join:
Will give the same number of rows and one less column. The
Regno will be displayed only once.
Eg. Display all the details from both tables who were challaned in
the month of April.
Or