Data manipulation

advertisement
Introduction - Databases
A database is a structured collection of data. The data is typically organized to model relevant
aspects of reality (for example, the availability of rooms in hotels), in a way that supports processes
requiring this information (for example, finding a hotel with vacancies).
The database data collection with DBMS is called a database system.
A general-purpose DBMS is typically a complex software system that meets many usage
requirements to properly maintain its databases which are often large and complex.
This is specially the case with client-server, near-real time transactional systems, in which
multiple users have access to data, data is concurrently entered.
Well known DBMSs include Oracle,Sybase, FoxPro, IBM DB2, Linter, Microsoft Access,
Microsoft SQL Server, MySQL, PostgreSQL and SQLite.
Databases are used all over the place for many purposes. Databases take many forms as well.
The larger datasets are stored in databases.
Whereever data is stored, one could argue that it's a database. So here are just a few places...
 Banks use database to track money in accounts and transactions to those accounts.
 Stores use databases to keep track of inventory and current prices.
 Phone companies use databases to keep track of your phone usage and the accounting
of charges to you.
 Electric companies use databases to track power usage and bill customers.
 Webshops use databases
 Online Railway timetable uses database
 Government uses databases to track tax payers and the payments they make
Recommended Reading:
http://en.wikipedia.org/wiki/Database
First we create tables, and fill the tables with data. Then we will create queries based on the
DataTables, with the usage of conditions.
Query: A precise request for information retrieval with database and information systems.
Query language, a computer language used to make queries into databases and information systems.
-1-
The main parts of an Access database
Tables
In Access, data is stored in tables. A table is a set of columns and rows, with each column
referred to as a field. Each value in a field represents a single type of data. Each row of a table is
referred to as a record. Within a table, each field must be given a name and no two fields can have the
same name.
Queries
You use queries to retrieve specific data from your database and to answer questions about
your data. For example, you can use a query to find the names of the employees in your database who
live in a particular state.
Reports
Reports organize or summarize your data so you can print it or view it on screen. You often
use reports when you want to analyze your data or present your data to others.
Forms
Forms allow you to create a user interface in which you can enter and edit your data. Forms
often contain command buttons and other controls that perform various tasks. You can create a
database without using forms by simply editing your data in the table datasheets. However, most
database users prefer to use forms for viewing, entering, and editing data in the tables.
Forms give you the ability to choose the format and arrangement of fields. You can use a
form to enter, edit, and display data.
-2-
Introduction – SQL
(Structured Query Language)
In MS-Access you can create a query by QBE form or with SQL statements. But in several databases
you can use only the SQL queries. SQL is used in more places than the QBE graphical queries.
QBE Queries: It is a graphical query language, using visual tables where the user would enter
commands, example elements and conditions.
http://www.cwnresearch.com/resources/databases/access/tutorials/access2000/CreatingQBEQ
ueries/CreatingQBEQueries.html
http://www.techterms.com/definition/qbe
http://en.wikipedia.org/wiki/Query_by_Example
SQL (pron.: /ˈɛs kjuː ˈɛl/ or Structured Query Language) is a special-purpose programming
language designed for managing data in relational database management systems (RDBMS).
Recommended Reading:
http://en.wikipedia.org/wiki/SQL
SQL was initially developed at IBM in the early 1970s. This version, initially called SEQUEL
(Structured English Query Language), was designed to manipulate and retrieve data stored in IBM's
original relational database management system.
The most common operation in SQL is the query, which is performed with the declarative
SELECT statement. SELECT retrieves data from one or more tables, or expressions.
Queries allow the user to describe desired data, leaving the database management system
(DBMS) responsible for planning, optimizing, and performing the physical operations necessary to
produce that result as it chooses.
A query includes a list of columns to be included in the final result immediately following the
SELECT keyword. An asterisk ("*") can also be used to specify that the query should return all
columns of the queried tables. SELECT is the most complex statement in SQL, with optional
keywords and clauses that include:


The FROM clause which indicates the table(s) from which data is to be retrieved. The FROM
clause can include optional JOIN subclauses to specify the rules for joining tables.
The WHERE clause includes a comparison predicate, which restricts the rows returned by the
query. The WHERE clause eliminates all rows from the result set for which the comparison
predicate does not evaluate to True.
-3-



The GROUP BY clause is used to project rows having common values into a smaller set of
rows. GROUP BY is often used in conjunction with SQL aggregation functions or to eliminate
duplicate rows from a result set. The WHERE clause is applied before the GROUP BY clause.
The HAVING clause includes a predicate used to filter rows resulting from the GROUP BY
clause. Because it acts on the results of the GROUP BY clause, aggregation functions can be
used in the HAVING clause predicate.
The ORDER BY clause identifies which columns are used to sort the resulting data, and in
which direction they should be sorted (options are ascending or descending). Without an
ORDER BY clause, the order of rows returned by an SQL query is undefined.
The following is an example of a SELECT query that returns a list of expensive books. The query
retrieves all rows from the Book table in which the price column contains a value greater than 100.00.
The result is sorted in ascending order by title. The asterisk (*) in the select list indicates that all
columns of the Book table should be included in the result set.
SELECT *
FROM Book
WHERE price > 100.00
ORDER BY title;
The example below demonstrates a query of multiple tables, grouping, and aggregation, by
returning a list of books and the number of authors associated with each book.
SELECT Book.title AS Title,
COUNT(*) AS Authors
FROM Book JOIN Book_author
ON Book.isbn = Book_author.isbn
GROUP BY Book.title;
Subqueries
Queries can be nested so that the results of one query can be used in another query via a
relational operator or aggregation function. A nested query is also known as a subquery.
While joins and other table operations provide computationally superior (i.e. faster)
alternatives in many cases, the use of subqueries introduces a hierarchy in execution which
can be useful or necessary. In the following example, the aggregation function AVG receives as
input the result of a subquery:
SELECT isbn, title, price
FROM Book
WHERE price < AVG(SELECT price FROM Book)
ORDER BY title;
-4-
Data manipulation
The Data Manipulation Language (DML) is the subset of SQL used to add, update and delete
data:

INSERT adds rows (formally tuples) to an existing table, e.g.:
INSERT INTO My_table
(field1, field2, field3)
VALUES
('test', 'N', NULL);

UPDATE modifies a set of existing table rows, e.g.:
UPDATE My_table
SET field1 = 'updated value'
WHERE field2 = 'N';

DELETE removes existing rows from a table, e.g.:
DELETE FROM My_table
WHERE field2 = 'N';
-5-
Download