Database language
In general there are two types of database languages. These are:
This is used to describe the structure of your database. In other words this language is used to:
attributes, their names and types and length of each of attributes.
Some database packages such as Microsoft Access, Paradox, Oracle ..etc provide both graphical user interface and commands to perform the above tasks.
This type of languages are used to retrieve, store, manipulate data , create forms, queries, reports and application programs. Example of these languages are: SQL, Query by example and relational Algebra and calculus languages. In general there are two types of Data Manipulation languages; these are:
Select Name, Phone
From Employee
Where Salary > 30000 and sex = 'female'
Query-By-Example is another example of nonprocedural languages. Incidentally Calculus language was the first nonprocedural language which was based on mathematical concepts. However the notations were very hard and much more user friendly languages such as SQl and Query-By-Exam were developed and more or less were based on calculus languages
Relational Algebra
Relational Algebra is a procedural Data Manipulation Language. It has the following operations:
In relational database terminology tables are called relations, rows of the tables called tuples.
Union:
The union of two relations is formed by adding the tuples of the two relations to create a third relation.Difference:
The difference of two relations is a third relation containing tuples that occurs in the first relation but not in the second relation.Intersection:
The intersection of two relations is a third relation containing the tuples that appear in both relations.Product
(sometimes called Cartesian product): The product of two relations is the concatenation of every tuple of one relation with every tuple of a second relation.Projection:
The projection is an operation that selects specified attributes (columns) from a relationSelection:
The selection is an operation that selects specified tuples (rows) from a relation.Join:
The join operation is a combination of the product, selection and possibly projection operations.Divide:
Isolates attribute values for attributes other than the join attributes.SQL (Structured Query Language)
SQL is a non procedural Data Manipulation Language. Following are some important SQL statements:
Select attribute(s)
From table(s)
Where conditions
Examples:
1. Select Name, Major
From Student
From Student
Where GP >3.50
3. Select Name, Major
From Student
Where GP >3.50 and Major ="Math"
Select *
From Student
Select Major
From Student
Select Distinct Major
From Student
Select Name
From Student
Where Major IN ["Math", "Accounting"]
Select Name
From Student
Where Major NOT IN ["Math", "Accounting"]
Select Name, Major, Age
From Student
Where Major IN ["Math", "Accounting"]
Order By Name
Select Name, Major, Age
From Student
Where Major IN ["Math", "Accounting"]
Order BY Major ASC, Age Desc
Some SQL Built-in Functions:
Count, Avg
Select count(*)
From Stdent
Select count (Major) From Student
Select count (Distinct Major) From Student
Major.
Select Major, count (*)
From Student
Grouped By Major
Select Major, count (*)
From Student
Grouped By Major
Having count (*) > 2
Joining with SQL
Select Student.Id, Student.Name, Enrollment.ClassName
From Student, Enrollment
Where Student.Id = Enrollment.Student Number
Changing Data
Inserting Data:
Insert Into Enrollment
Value (44456, IS380, 55)
Insert Into Enrollment
(Student Number, ClassName)
Value (44456, IS380)
Insert Into Junior
Values
(Select Sid, Name, Major
From Student
Where GradeLLevel = "JR")
Delete Data
Delete Student
Where Sid = 44456
Delete Enrollment
Where Enrollment.StudentNumber
In
(Select Student.ID
From Student
Where Student.Major = "Math")
Update
Update Enrollment
Set Position Number = 66
Where Sid = 400