Theory Dbms
Theory Dbms
Objective: Write the SQL queries for data definition and data manipulation language.
Char (n)- A fixed length character length string with user specified length .
Varchar (n)- A variable character length string with user specified maximum length n.
Number (p, d)-A Fixed point number with user defined precision.
Real, double precision- Floating point and double precision floating point numbers
with machine dependent precision.
Float (n)- A floating point number, with precision of at least n digits.
Date- A calendar date containing a (four digit) year, month and day of the month.
Time- The time of day, in hours, minutes and seconds Eg. Time ’09:30:00’.
DDL statement for creating a table
Syntax-
Create table tablename
(columnname datatype(size), columnname datatype(size));
Rules:
1. MySQL reserved words cannot be used.
2. Underscore, numerals, letters are allowed but not blank space.
3. Maximum length for the table name is 30 characters.
4. 2 different tables should not have same name.
5. We should specify a unique column name.
6. We should specify proper data type along with width.
7. We can include “not null” condition when needed. By default it is ‘null’.
The SELECT DISTINCT * SQL syntax scans through entire rows, and eliminates rows that
have exactly the same contents in each column.
Syntax:
SELECT DISTINCT *
FROM TableName;
Deletion Operation:-
We can delete whole tuple ( rows) we can delete values on only particulars attributes.
Deletion of all rows
Syntax:
Delete from tablename;
Syntax:
Select column name result_columnname, columnname result_columnname
From tablename;
Logical Operators:
The logical operators that can be used in SQL sentenced are
AND all of must be included
OR any of may be included
NOT none of could be included
Pattern Searching:
The most commonly used operation on string is pattern matching using the operation ‘like’
we describe patterns by using two special characters.
Percent (%): the % character matches any substring we consider the following
examples.
o ‘Perry %’ matches any string beginning with perry
o ‘% idge % matches any string containing’ idge as substring.
o ‘ - - - ‘ matches any string exactly three characters.
o ‘ - - - % matches any string of at least of three characters.
The ‘order by’ clause is used to sort the table data according to one or more columns
of the table.
The table rows are ordered in ascending order of the column values by default. The
keyword used for the same is ‘asc’. For sorting the table data according to colname in
descending order, keyword ‘desc’ is used.
Example: select colname1, colname2,… from tablename where search condition order by
colname1 asc/desc, colname2 asc/desc,…;
Practical Assignment – 1
Create these tables which consist of following attributes
College Student
Column Column
Data type Size Data type Size
Name Name
cName varchar 10 sID int
state varchar 10 sName varchar 10
enrollment int GPA number 2,1
sizeHS int
DoB date
Apply
Column
Data type Size
Name
sID int
cName varchar 10
major varchar 20
decision char 1
About Database: This database is College Application Database which contain 3 tables.
First one is Student that contain information of student such as ID of Student, his
name, GPA that he/she scored, size of his High School class i.e. number of students in his/her
high school class and student’s Date of Birth.
Second table in database is College this table contains college information such as
name of college, state where it is situated, and its enrollment i.e. number seats in that college.
Third table contains data about applications and this table is named Apply each row
of this table will contain information about one application. Each application has
sID: This will contain ID (similar to Roll No.) of student who is applying. [Each
Application only contain sID of Applicant all other information about student such as his
name or GPA can be check from Student table]
cName: Name of college where applicant is applying [similar to sID all the other
information about college such as its state or enrollment can be retrieve from College table]
major: it is the major in which applicant is applying e.g. CS, EE, biology etc.
decision: it is Y or N shows Acceptance or Rejection of Application
Insert the following data to these tables:
Student Apply
sID sName GPA sizeHS DoB sID cName major decision
123 Amy 3.9 1000 1996-06-26 123 Stanford CS Y
234 Bob 3.6 1500 1995-04-07 123 Stanford EE N
345 Craig 3.5 500 1995-02-04 123 Berkeley CS Y
456 Doris 3.9 1000 1997-07-24 123 Cornell EE Y
567 Edward 2.9 2000 1996-12-21 234 Berkeley biology N
678 Fay 3.8 200 1996-08-27 345 MIT bioengineering Y
789 Gary 3.4 800 1996-10-08 345 Cornell bioengineering N
987 Helen 3.7 800 1997-03-27 345 Cornell CS Y
876 Irene 3.9 400 1996-03-07 345 Cornell EE N
765 Jay 2.9 1500 1998-08-08 678 Stanford history Y
654 Amy 3.9 1000 1996-05-26 987 Stanford CS Y
987 Berkeley CS Y
543 Craig 3.4 2000 1998-08-27
876 Stanford CS N
College 876 MIT biology Y
cName state enrollment 876 MIT marine biology N
Stanford CA 15000 765 Stanford history Y
Berkeley CA 36000 765 Cornell history N
MIT MA 10000 765 Cornell psychology Y
Cornell NY 21000 543 MIT CS N
Harvard MA 50040
Exercise
Determine the appropriate data type:
Dept
Column
deptno dname loc datatype size
name
1 ACCOUNTING ST LOUIS Deptno
NEW Dname
2 RESEARCH
YORK
loc
3 SALES ATLANTA
4 OPERATIONS SEATTLE
Employee
empno ename job mgr hiredate sal comm dept
1 JOHNSON ADMIN 6 12/17/1990 18000 (null) 4
2 HARDING MANAGER 9 2/2/1998 52000 300 3
3 TAFT SALES I 2 1/2/1996 25000 500 3
4 HOOVER SALES I 2 4/2/1990 27000 (null) 3
5 LINCOLN TECH 6 6/23/1994 22500 1400 4
6 GARFIELD MANAGER 9 5/1/1993 54000 (null) 4
7 POLK TECH 6 9/22/1997 25000 (null) 4
8 GRANT ENGINEER 10 3/30/1997 32000 (null) 2
9 JACKSON CEO (null) 1/1/1990 75000 (null) 4
10 FILLMORE MANAGER 9 8/9/1994 56000 (null) 2
11 ADAMS ENGINEER 10 3/15/1996 34000 (null) 2
12 WASHINGTON ADMIN 6 4/16/1998 18000 (null) 4
13 MONROE ENGINEER 10 12/3/2000 30000 (null) 2
14 ROOSEVELT CPA 9 10/12/1995 35000 (null) 1
15 HANCOCK SALES I 2 3/2/1990 27500 (null) 3
Solve the following queries:
Q1. Employee Name and Hire Date sorted by Hire Date (Recent to Old).
Q2. Employee Name and Job sorted by Job
Column
(Alphabetically) datatype size
name
Q3. Employee Name and Job for all Engineers,
empno
sorted by Employee Name Alphabetically
ename
Q4. Job, Employee Name, Salary and
Commission for employees with salary over job
50000 sorted by Salary (Largest to Smallest). mgr
Q5. Job, Employee Name, Salary and hiredate
Commission for employees with a sal
Commission sorted by Salary (Largest to comm
Smallest). dept
Q6. Job, Employee Name, Salary and Commission for employees whose name starts with the
letter H
Q7. Job, Employee Name, Salary and Commission for employees whose name starts with the
letter H and who do not get commission.
Q8. Job, Employee Name for employees in Dept No. 3.
Q9. Dept Name and Loc for employees in Dept No. 3.
Q10. Job, Employee Name, Dept, Salary sorted first by Dept (Smallest to Largest) and then
Salary (Largest to Smallest)
1. What is Relation?
2. What are attributes?
3. What are different DML commands?
4. What is the need of pattern matching?