database query and sql
Post on 04-Apr-2018
241 Views
Preview:
TRANSCRIPT
-
7/30/2019 Database Query and SQL
1/43
Ms AccessMs Access
-
7/30/2019 Database Query and SQL
2/43
What is Microsoft Access?What is Microsoft Access?
Microsoft Access is a relational database
management system (DBMS or RDBMS). At the
very core, it is a software engine that provides an
interface between physical data and user.
Other examples of DBMS applications include:
OraclemySQL
SQL Server (Microsoft)
DB2 (IBM)
Informix
-
7/30/2019 Database Query and SQL
3/43
Why choose MSWhy choose MS--Access over SPSS /Access over SPSS /
Excel?Excel?Although there is always overlap, the following rules might help
when deciding when / when not to use MS Access:
MS Access is best used for long-term data storage and/or data
sharing.
MS Excel is best used for minor data collection, manipulation, and
especially visualization.
SPSS is best used for minor data collection and especially dataanalysis.
It is easy to export data from MS Access to Excel SPSS
-
7/30/2019 Database Query and SQL
4/43
Why choose MSWhy choose MS--Access over otherAccess over other
DBMS systems?DBMS systems?
Cheap, readily available (packaged with MS-Office
Premium).
Eas to use relative to other s stems Oracle ma
require one FTE to maintain the server as a database
administrator and another FTE to serve as an
application developer).
Includes front-end tools for rapid application
development (RAD). This also makes MS-Access a
good prototype environment.
-
7/30/2019 Database Query and SQL
5/43
Why choose other DBMS systemsWhy choose other DBMS systems
over MSover MS--Access?Access?
MS-Access can handle a large number of records, but
is somewhat slow compared to some of the high-end
platforms.
Multiple users may use the database simultaneously,
but MS-Access is known to become unstable with
greater than 3-5 users.
-
7/30/2019 Database Query and SQL
6/43
What is in an MSWhat is in an MS--Access file ?Access file ?
Although the term database typically refers to a
collection of related data tables, an Access databaseincludes more than just data. In addition to tables,
you can add:
Saved queries (stored procedures) - organizingand/or manipulating data
Forms GUI interaction with data, eventprogrammingReports customized results for printing
Macros and VB programs for extending functionality
-
7/30/2019 Database Query and SQL
7/43
Table RelationshipsTable Relationships
One of the most powerful features of arelational database management system is
its ability to define relationships between
tables
Use a common field to relate one table to
another
-
7/30/2019 Database Query and SQL
8/43
There are three basic types of relationships
One-to-One Relationship
One-to-Many Relationship
Many-to-Many Relationship
-
7/30/2019 Database Query and SQL
9/43
One-to-One Relationship
A relationship between two tables where both the primary key and the
foreign key are unique in considered one-to-one. For each record in the
first table, there will be one and only one record in the second table.
In a one-to-one relationship, each record in the primary table can have
only one matching record in the foreign table and each record in the
foreign table can have only one matching record in the primary table.
A one-to-one relationship is created if both of the related fields are
primary keys or have unique indexes.
-
7/30/2019 Database Query and SQL
10/43
One-to-many Relationship
A one-to-many relationship existsbetween two tables when one record in
the first table matches zero, one, or manrecords in the second table, and whenone record in the second table matches
at most one record in the first table The Primary table is the one in a one-to-
many relationship
The Related table is the many table
-
7/30/2019 Database Query and SQL
11/43
Referential integrity is a set of rules
that Access enforces to maintainconsistency between related tables whenyou update data in a database
The Relationships window illustrates
the relationships among a databasestables
Click the Database Tools tab on the
Ribbon In the Show/Hide group on the Database
Tools tab, click the Relationships button
-
7/30/2019 Database Query and SQL
12/43
-
7/30/2019 Database Query and SQL
13/43
-
7/30/2019 Database Query and SQL
14/43
Many-to-Many Relationship
A relationship between two tables when many records in one table can
relate to many records in another table is considered many-to-many.
Many-to-Many relationships cannot be created in Access.
Figure . Many-to-Many Relationship (Using a Junction Table)
In the above example, with a many-to-many relationship, a record in the
Employees table can have many matching records in the Project table,
and a record in the Projects table can have many matching records in
the Employees.
This type of relationship is only possible by defining a third table (called a
junction table) whose primary key consists of two fields the foreign keys
from both the Employees and Projects Tables.
A many-to-many relationship is really two one-to-many relationships with
a third table.
-
7/30/2019 Database Query and SQL
15/43
What is QueryWhat is Query
An MS-Access query is a set of storedSQL instructions that manipulate and/orselect data from one or more tables.
SQL (Structured Query Language) is avery wi e y use ata ase anguagedesigned specifically for communicatingwith databases
SQL is not proprietary almost everyDBMS supports SQL (including MS-Access).
-
7/30/2019 Database Query and SQL
16/43
What is QueryWhat is Query
Queries are extremely easy to set
up/use and provide an up-to-date
snapshot of your data at any time.
Queries may be used to calculate values
based upon existing fields, join fields
from separate tables, globally update ordelete data, and export linked/calculated
data to external programs.
-
7/30/2019 Database Query and SQL
17/43
Types of QueryTypes of Query
Select Query
Update Querya e- a e uery
Append Query
Delete Query
-
7/30/2019 Database Query and SQL
18/43
Types of QueryTypes of Query
Select Query A Select Query extractdata from a table ( or from another
query) and present in desired / specifiedormat
-
7/30/2019 Database Query and SQL
19/43
Types of QueryTypes of Query
Make-Table Query Select data andcreates/populates new table.
Update Query Updates fields fromspecified table data
Append Query Runs query on onetable, appends results to another table
Delete Query Delete selected recordsfrom table
-
7/30/2019 Database Query and SQL
20/43
Creating QueryCreating Query
Create Query Design Select
Table/Tables from Show table DialogSelect type of query
-
7/30/2019 Database Query and SQL
21/43
QueryQuery Filtering DataFiltering Data
This query will return all records in the database for:Females
who are not white
whose height are greater than 150 cm
and who weigh between 60 and 70 kg
-
7/30/2019 Database Query and SQL
22/43
QueryQuery Filter OperatorsFilter Operators
= equals
> greater than
>= greater than or equal
< less than
-
7/30/2019 Database Query and SQL
23/43
Run a QueryRun a Query
Query Tools
Run Query
-
7/30/2019 Database Query and SQL
24/43
Creating an Update QueryCreating an Update Query
Create a query with the necessary fields andselection criteria
Click the Update button in the Query Type
group on the Design tab. Access replaces theow an ort rows n t e es gn gr w t t e
Update To row
Click the Run button, and then click the Yes
button to confirm changing the records
-
7/30/2019 Database Query and SQL
25/43
QueryQuery Calculating FieldsCalculating Fields
If there is ambiguity in the field names
between tables, you may need to type. .
Ex: emp.DA=30%*[emp.basic]
-
7/30/2019 Database Query and SQL
26/43
Creating a MakeCreating a Make--Table QueryTable Query
Create a query with the necessary fields and selection
criteria
Click the Make Table button in the Query Type group
on the Design tab
In the Make Table dialo box, t e the new table name
in the Table Name list box. Make sure the Current
Database option button is selected to include the new
table in the current database, or click the Another
Database option button and enter the database name inthe File Name text box. Then click the OK button
Click the Run button, and then click the Yes button to
confirm the creation of the new table
-
7/30/2019 Database Query and SQL
27/43
Creating an Append QueryCreating an Append Query
Create a query with the necessary fields and selection
criteria
Click the Append button in the Query Type group on
the Design tab
In the A end dialo box, select the table name in the
Table Name list box. Make sure the Current Database
option button is selected to include the new table in the
current database, or click the Another Database option
button and enter the database name in the File Nametext box. Then click the OK button. Access replaces the
Show row in the design grid with the Append To row
Click the Run button, and then click the Yes button to
confirm appending the records to the table
-
7/30/2019 Database Query and SQL
28/43
Creating a Delete QueryCreating a Delete Query
Create a query with the necessary fields andselection criteria
click the Delete button in the Query Type
group on the Design tab. Access replaces theow an ort rows n t e es gn gr w t t e
Delete row
Click the Run button, and then click the Yes
button to confirm deleting the records
-
7/30/2019 Database Query and SQL
29/43
SQLSQL
SQL (Structured Query Language) is avery widely used database languagedesigned specifically for
communicating with databases
SQL is not proprietary almost everyDBMS supports SQL (including MS-Access).]
SQL is a language that all commercialRDBMS implementations understand.
-
7/30/2019 Database Query and SQL
30/43
SQL is relatively easy to learn, butextremely powerful one of theeasiest ways to learn is to use MS-Access Query by Examplemethods, then look at the
enerated SQL command
Remember that a query is nothingmore than the database engine
running the stored SQL command(it looks and sometimes acts like atable, but really adds little mass tothe database file)
-
7/30/2019 Database Query and SQL
31/43
SQL supports various data types
Decimal numbers--- NUMBER, INTEGER .
Number is an oracle data type. Integer is an ANSI data type. Integer is
equivalent of NUMBER(38)
The syntax for NUMBER is NUMBER(P,S) p is the precision and s is
the scale.
Floating point numbers---- FLOATxe eng c arac er s r ngs---- en
Fixed length character data of length len bytes. This should be used
for fixed length data.
Variable length character strings --- Varchar2(len)
Variable length character string having maximum length len bytes. We
must specify the size
Dates-----DATE
-
7/30/2019 Database Query and SQL
32/43
Operators
Arithmetic operators like +,-,*,/
Logical operators: AND, OR
Relational operators: =,=, < >
Retrieval Operator : Like, In, Between , Not NULL
SELECT * FROM EMPLOYEE WHERE SAL > 10000;
SELECT * FROM EMPLOYEE WHERE (SAL > 10000 AND
AGE > 25);SELECT * FROM SP WHERE QTY IS NOT NULL
SELECT * FROM P WHERE PNAME LIKE C*
SELECT SNO FROM S WHERE CITY IN (PARIS ,ROME)
-
7/30/2019 Database Query and SQL
33/43
NULL
Missing/unknown/inapplicable data represented as a null value
NULL is not a data value. It is just an indicator that the value is
unknown
-
7/30/2019 Database Query and SQL
34/43
SQL has three flavours of statements. The DDL, DML
and DCL.
DDL is Data Definition Language statements. Some examples:
CREATE - to create objects in the database
ALTER - alters the structure of the database
DROP - delete objects from the database
GRANT - gives user's access privileges to database
REVOKE - withdraw access privileges given with the GRANT
command
-
7/30/2019 Database Query and SQL
35/43
DML is Data Manipulation Language statements. Some
examples:
SELECT - retrieve data from the a database
INSERT - insert data into a table
UPDATE - updates existing data within a table
DELETE - deletes all records from a table, the space for the records
remain
-
7/30/2019 Database Query and SQL
36/43
DCL is Data Control Language statements.
Examples:
COMMIT - save work done
SAVEPOINT - identify a point in a transaction to which you can
later roll back
ROLLBACK - restore database to original since the last
COMMIT
-
7/30/2019 Database Query and SQL
37/43
SQL - CREATE TABLE
Syntax:
CREATE TABLE tablename (column_name data_ type
constraints, )
Example:
CREATE TABLE Emp (
Job Char(10) UNIQUE,
Hiredate Date,Sal single,Comm single,
DeptNo REFERENCES DEPT(DeptNo));
-
7/30/2019 Database Query and SQL
38/43
SQL - DROP TABLE
DROP TABLE TABLENAME
Deletes table structure
Cannot be recovered
Use with caution
DROP TABLE EMP;
-
7/30/2019 Database Query and SQL
39/43
SQL - INSERT TABLE
Syntax: INSERT INTO tablename VALUES (value list)
INSERT INTO S VALUES(S3,SUP3,BLORE,10)
-
7/30/2019 Database Query and SQL
40/43
SQL UPDATE TABLE
Syntax:
UPDATE tablename SET column_name =value [ WHERE
condition]
With or without WHERE clause
Examples:
UPDATE S SET CITY = KANPUR WHERE SNO=S1
UPDATE EMP SET SAL = 1.10 * SAL
-
7/30/2019 Database Query and SQL
41/43
SQL - DELETE TABLE
With or without WHERE clause
Syntax: DELETE FROM tablename WHERE condition
=
DELETE FROM SP
-
7/30/2019 Database Query and SQL
42/43
SQL SELECT TABLE
To select a particular column:
SELECT ColumnName FROM Tablename
To select set of column names,
SELECT column1, column2, FROM TableName
To select all columns from a table:
SELECT * FROM TableName
Examples:
Get the names of all the suppliers
SELECT SNAME FROM S;
Get the names and city of all the suppliers
SELECT SNAME, CITY FROM S
Get full details for all Suppliers in S table
SELECT * FROM S
-
7/30/2019 Database Query and SQL
43/43
SQL - Aggregate functions
Used when information you want to extract from a table has to do
with the data in the entire table taken as a set.
Aggregate functions are used in place of column names in the
SELECT statement
The aggregate functions in SQL are :
SUM( ) , AVG( ) , MAX( ) , MIN( ), COUNT( )
SELECT SUM (QTY) FROM SP WHERE PNO=P2
SELECT AVG(QTY) FROM SP WHERE SNO=S1
SELECT MAX(QTY) FROM SP WHERE SNO =S1
SELECT COUNT(Qty) FROM SP WHERE PNO=P2
top related