Database management systems Part 3/4: Putting data, getting data -orleans.fr/lifo/Members/David.Teller/

advertisement
Database management systems
Database management systems
Part 3/4: Putting data, getting data
David.Teller@univ-orleans.fr
http://www.univ-orleans.fr/lifo/Members/David.Teller/
2006-16-02
Database management systems
The program
The program
Data Manipulation Language
Inserting Data
Getting your data
Juggling with data
Bottom line
Working with the world
Bottom line
Database management systems
The program
The story so far
What we have so far :
I
a DBMS
I
I
relations
domains
I
constraints.
Database management systems
The program
Today
I
putting data
removing data
I
locating data
I
extracting data.
I
Database management systems
The program
By the way
I hope you succeeded in creating your relations with Access/OOBase.
Database management systems
The program
By the way
I hope you succeeded in creating your relations with Access/OOBase.
Any questions ?
Database management systems
The program
By the way
I hope you succeeded in creating your relations with Access/OOBase.
Any questions ?
Keep in mind that the instructions I give here must be tested. SQL is a
standard but it is hardly implemented by everyone.
Database management systems
Data Manipulation Language
Inserting Data
The program
Data Manipulation Language
Inserting Data
Getting your data
Juggling with data
Bottom line
Working with the world
Bottom line
The program
Data Manipulation Language
Inserting Data
Getting your data
Juggling with data
Bottom line
Working with the world
Bottom line
Database management systems
Data Manipulation Language
Inserting Data
So you want to have some data ?
What’s the process ?
Database management systems
Data Manipulation Language
Inserting Data
So you want to have some data ?
What’s the process ?
1. get the data into the computer
Database management systems
Data Manipulation Language
Inserting Data
So you want to have some data ?
What’s the process ?
1. get the data into the computer
2. compute/divide the data so it fits your relations
Database management systems
Data Manipulation Language
Inserting Data
So you want to have some data ?
What’s the process ?
1. get the data into the computer
2. compute/divide the data so it fits your relations
3. insert the data.
Database management systems
Data Manipulation Language
Inserting Data
Let’s start with easy stuff
The easiest thing to do is writing down data.
INSERT INTO TheTable ( Column_1 , Column_2 . . . )
VALUES ( value_1 , value_2 . . . ) ;
Database management systems
Data Manipulation Language
Inserting Data
A few notes
INSERT INTO C h a r a c t e r s ( " F i r s t ␣Name" , " L a s t ␣Name" , IQ )
VALUES ( " Joe " , " D a l t o n " , 1 0 0 ) ;
I
Character strings must be enclosed between quotation marks.
I
You can enclose field names between quotation marks, too, if they
contain strange characters.
You don’t have to write all of the entry at once – missing data
shows as NULL.
I
Database management systems
Data Manipulation Language
Inserting Data
A few notes
INSERT INTO C h a r a c t e r s ( " F i r s t ␣Name" , " L a s t ␣Name" , IQ )
VALUES ( " Joe " , " D a l t o n " , 1 0 0 ) ;
I
Character strings must be enclosed between quotation marks.
I
You can enclose field names between quotation marks, too, if they
contain strange characters.
You don’t have to write all of the entry at once – missing data
shows as NULL.
I
Q Anything we shouldn’t forget ?
Database management systems
Data Manipulation Language
Inserting Data
A few notes
INSERT INTO C h a r a c t e r s ( " F i r s t ␣Name" , " L a s t ␣Name" , IQ )
VALUES ( " Joe " , " D a l t o n " , 1 0 0 ) ;
I
Character strings must be enclosed between quotation marks.
I
You can enclose field names between quotation marks, too, if they
contain strange characters.
You don’t have to write all of the entry at once – missing data
shows as NULL.
I
Q Anything we shouldn’t forget ?
A
I
Constraints.
I
Types.
Keys.
I
Database management systems
Data Manipulation Language
Inserting Data
Import
Q What if the data cannot be written ?
Database management systems
Data Manipulation Language
Inserting Data
Import
Q What if the data cannot be written ?
Q Any examples, by the way ?
Database management systems
Data Manipulation Language
Inserting Data
Import
Q What if the data cannot be written ?
Q Any examples, by the way ?
A DNA, mp3s, save games. . .
Database management systems
Data Manipulation Language
Inserting Data
Import
Q What if the data cannot be written ?
Q Any examples, by the way ?
A DNA, mp3s, save games. . .
A For this, you’ll need to work from outside of SQL.
Database management systems
Data Manipulation Language
Inserting Data
Critical data
Q Remember how we ensure security of critical data ?
Database management systems
Data Manipulation Language
Inserting Data
Critical data
Q Remember how we ensure security of critical data ?
A Transactions.
Database management systems
Data Manipulation Language
Inserting Data
Critical data
Q Remember how we ensure security of critical data ?
A Transactions.
A transaction starts with START TRANSACTION and ends with either
COMMIT (to complete it) or ROLLBACK (to cancel it).
Database management systems
Data Manipulation Language
Inserting Data
Critical data
Q Remember how we ensure security of critical data ?
A Transactions.
A transaction starts with START TRANSACTION and ends with either
COMMIT (to complete it) or ROLLBACK (to cancel it).
Q What does a transaction ensure ?
Database management systems
Data Manipulation Language
Inserting Data
Critical data
Q Remember how we ensure security of critical data ?
A Transactions.
A transaction starts with START TRANSACTION and ends with either
COMMIT (to complete it) or ROLLBACK (to cancel it).
Q What does a transaction ensure ?
A That the operation won’t be interrupted, interleaved or left incomplete.
Database management systems
Data Manipulation Language
Inserting Data
Critical data
Q Remember how we ensure security of critical data ?
A Transactions.
A transaction starts with START TRANSACTION and ends with either
COMMIT (to complete it) or ROLLBACK (to cancel it).
Q What does a transaction ensure ?
A That the operation won’t be interrupted, interleaved or left incomplete.
Q What’s the point of ROLLBACK ?
Database management systems
Data Manipulation Language
Inserting Data
Critical data
Q Remember how we ensure security of critical data ?
A Transactions.
A transaction starts with START TRANSACTION and ends with either
COMMIT (to complete it) or ROLLBACK (to cancel it).
Q What does a transaction ensure ?
A That the operation won’t be interrupted, interleaved or left incomplete.
Q What’s the point of ROLLBACK ?
A That’s something you call when you know there has been some
problem (user cancelling his credit card debit, system failure. . .) and you
wish to make sure nothing has happened.
Database management systems
Data Manipulation Language
Getting your data
The program
Data Manipulation Language
Inserting Data
Getting your data
Juggling with data
Bottom line
Working with the world
Bottom line
Database management systems
Data Manipulation Language
Getting your data
Operators
Remember our three operators ?
restriction extracts entries from a table
projection extracts columns from a table
junction combines two tables based on identical attributes
Database management systems
Data Manipulation Language
Getting your data
Data set
First Name
Joe
Jack
Lucky
William
Last Name
Dalton
Dalton
Luke
Bonney
..
.
Age
30
28
30
12
Size
110
140
175
145
First Name
Lucky
William
Joe
Jack
Last Name
Luke
Bonney
Dalton
Dalton
..
.
Reward
0$
2000$
1000$
800$
IQ
100
90
105
90
...
...
...
...
...
Characters
...
...
...
...
...
Rewards
Database management systems
Data Manipulation Language
Getting your data
Restriction
Restriction is about choosing entries.
Database management systems
Data Manipulation Language
Getting your data
Restriction
Restriction is about choosing entries.
SELECT ( ∗ )
FROM C h a r a c t e r s
WHERE C h a r a c t e r s . IQ >= 1 0 0 ;
Database management systems
Data Manipulation Language
Getting your data
Restriction
Restriction is about choosing entries.
SELECT ( ∗ )
FROM C h a r a c t e r s
WHERE C h a r a c t e r s . IQ >= 1 0 0 ;
Q What’s the result, by the way ?
Database management systems
Data Manipulation Language
Getting your data
Restriction
Restriction is about choosing entries.
SELECT ( ∗ )
FROM C h a r a c t e r s
WHERE C h a r a c t e r s . IQ >= 1 0 0 ;
Q What’s the result, by the way ?
A It’s a (virtual) table. The operation has no visible side-effect.
Database management systems
Data Manipulation Language
Getting your data
Restricting
First Name
Joe
Lucky
Last Name
Dalton
Luke
..
.
Age
30
30
Size
110
175
IQ
100
105
...
...
...
Database management systems
Data Manipulation Language
Getting your data
Projection
Projection is about choosing columns.
Database management systems
Data Manipulation Language
Getting your data
Projection
Projection is about choosing columns.
SELECT
C h a r a c t e r s . " F i r s t ␣Name" ,
C h a r a c t e r s . " L a s t ␣Name"
FROM C h a r a c t e r s ;
Database management systems
Data Manipulation Language
Getting your data
Projection
Projection is about choosing columns.
SELECT
C h a r a c t e r s . " F i r s t ␣Name" ,
C h a r a c t e r s . " L a s t ␣Name"
FROM C h a r a c t e r s ;
First Name
Joe
Jack
⇒ Lucky
William
Last Name
Dalton
Dalton
Luke
Bonney
..
.
Database management systems
Data Manipulation Language
Getting your data
Junction
Junction is about combining relations.
Database management systems
Data Manipulation Language
Getting your data
Junction
Junction is about combining relations.
SELECT ( ∗ )
FROM C h a r a c t e r s , Rewards
USING " F i r s t ␣Name" , " L a s t ␣Name" ;
First Name
Joe
Jack
Lucky
William
Last Name
Dalton
Dalton
Luke
Bonney
Age
30
28
30
12
..
.
Size
110
140
175
145
IQ
100
90
105
90
Reward
1000$
800$
0$
2000$
...
...
...
...
...
Database management systems
Data Manipulation Language
Getting your data
Natural Junction
Alternative syntax:
SELECT ( ∗ )
FROM C h a r a c t e r s NATURAL JOIN Rewards ;
Database management systems
Data Manipulation Language
Getting your data
Putting it all together
SELECT
C h a r a c t e r s . " F i r s t ␣Name" ,
C h a r a c t e r s . " L a s t ␣Name" ,
C h a r a c t e r s . Size
FROM
C h a r a c t e r s NATURAL JOIN Rewards
WHERE
Rewards . Reward > 0 ;
Database management systems
Data Manipulation Language
Getting your data
Additional operators
You can
Database management systems
Data Manipulation Language
Getting your data
Additional operators
You can
I
count items with COUNT(...)
Database management systems
Data Manipulation Language
Getting your data
Additional operators
You can
I
I
count items with COUNT(...)
sum items with SUM(...)
Database management systems
Data Manipulation Language
Getting your data
Additional operators
You can
I
count items with COUNT(...)
sum items with SUM(...)
I
choose the maximal item with MAX(...)
I
Database management systems
Data Manipulation Language
Getting your data
Additional operators
You can
I
count items with COUNT(...)
sum items with SUM(...)
I
choose the maximal item with MAX(...)
I
average items with AVG(...)
I
Database management systems
Data Manipulation Language
Getting your data
Additional operators
You can
I
count items with COUNT(...)
sum items with SUM(...)
I
choose the maximal item with MAX(...)
I
average items with AVG(...)
remove duplicate with DISTINCT(...)
I
I
Database management systems
Data Manipulation Language
Getting your data
Additional operators
You can
I
count items with COUNT(...)
sum items with SUM(...)
I
choose the maximal item with MAX(...)
I
I
average items with AVG(...)
remove duplicate with DISTINCT(...)
I
concatenate results with CONCAT(...)
I
Database management systems
Data Manipulation Language
Getting your data
Additional operators
You can
I
count items with COUNT(...)
sum items with SUM(...)
I
choose the maximal item with MAX(...)
I
average items with AVG(...)
remove duplicate with DISTINCT(...)
I
I
I
I
concatenate results with CONCAT(...)
give a name to a virtual table SELECT [...] AS [...]
Database management systems
Data Manipulation Language
Getting your data
Additional operators
You can
I
count items with COUNT(...)
sum items with SUM(...)
I
choose the maximal item with MAX(...)
I
average items with AVG(...)
remove duplicate with DISTINCT(...)
I
I
I
concatenate results with CONCAT(...)
give a name to a virtual table SELECT [...] AS [...]
I
order the results with SELECT [...] ORDER BY [...]
I
Database management systems
Data Manipulation Language
Getting your data
Additional operators
You can
I
count items with COUNT(...)
sum items with SUM(...)
I
choose the maximal item with MAX(...)
I
average items with AVG(...)
remove duplicate with DISTINCT(...)
I
I
I
concatenate results with CONCAT(...)
give a name to a virtual table SELECT [...] AS [...]
I
order the results with SELECT [...] ORDER BY [...]
I
Database management systems
Data Manipulation Language
Getting your data
Additional operators
You can
I
count items with COUNT(...)
sum items with SUM(...)
I
choose the maximal item with MAX(...)
I
average items with AVG(...)
remove duplicate with DISTINCT(...)
I
I
I
concatenate results with CONCAT(...)
give a name to a virtual table SELECT [...] AS [...]
I
order the results with SELECT [...] ORDER BY [...]
I
Plus you can nest selections.
Database management systems
Data Manipulation Language
Getting your data
All in all
SELECT
C h a r a c t e r s . " F i r s t ␣Name" ,
C h a r a c t e r s . " L a s t ␣Name" ,
CONCAT(SUM( Rewards . Reward ) , ’ $ ’ )
FROM
C h a r a c t e r s NATURAL JOIN Rewards
WHERE
Rewards . Reward > 0 ;
Database management systems
Data Manipulation Language
Getting your data
RAD ?
Note that, while your DBMS can do all this, your GUI probably can’t.
Good reason to learn SQL.
Database management systems
Data Manipulation Language
Getting your data
Normal Forms ?
Q If the source is in Normal Form, is the result always in Normal Form ?
Database management systems
Data Manipulation Language
Getting your data
Normal Forms ?
Q If the source is in Normal Form, is the result always in Normal Form ?
A No, it isn’t.
Database management systems
Data Manipulation Language
Getting your data
Normal Forms ?
Q If the source is in Normal Form, is the result always in Normal Form ?
A No, it isn’t.
Exercise Prove it.
Database management systems
Data Manipulation Language
Getting your data
Bottom line
By now, you have everything you need to locate and filter your data.
Database management systems
Data Manipulation Language
Getting your data
Bottom line
By now, you have everything you need to locate and filter your data.
So, what else do you want to do ?
Database management systems
Data Manipulation Language
Getting your data
Views
Views are a mechanism used either to restrict access to data or to
bookmark a request.
Database management systems
Data Manipulation Language
Getting your data
Views
Views are a mechanism used either to restrict access to data or to
bookmark a request.
CREATE VIEW T o t a l R e w a r d
AS
SELECT
C h a r a c t e r s . " F i r s t ␣Name" ,
C h a r a c t e r s . " L a s t ␣Name" ,
CONCAT(SUM( Rewards . Reward ) , ’ $ ’ )
FROM
C h a r a c t e r s NATURAL JOIN Rewards
WHERE
Rewards . Reward > 0 ;
Database management systems
Data Manipulation Language
Getting your data
Views
Views are a mechanism used either to restrict access to data or to
bookmark a request.
CREATE VIEW T o t a l R e w a r d
AS
SELECT
C h a r a c t e r s . " F i r s t ␣Name" ,
C h a r a c t e r s . " L a s t ␣Name" ,
CONCAT(SUM( Rewards . Reward ) , ’ $ ’ )
FROM
C h a r a c t e r s NATURAL JOIN Rewards
WHERE
Rewards . Reward > 0 ;
TotalReward may now be used as a table.
Database management systems
Data Manipulation Language
Getting your data
Views
Views are a mechanism used either to restrict access to data or to
bookmark a request.
CREATE VIEW T o t a l R e w a r d
AS
SELECT
C h a r a c t e r s . " F i r s t ␣Name" ,
C h a r a c t e r s . " L a s t ␣Name" ,
CONCAT(SUM( Rewards . Reward ) , ’ $ ’ )
FROM
C h a r a c t e r s NATURAL JOIN Rewards
WHERE
Rewards . Reward > 0 ;
TotalReward may now be used as a table.
Q What’s the point of views ?
Database management systems
Data Manipulation Language
Getting your data
Views
Views are a mechanism used either to restrict access to data or to
bookmark a request.
CREATE VIEW T o t a l R e w a r d
AS
SELECT
C h a r a c t e r s . " F i r s t ␣Name" ,
C h a r a c t e r s . " L a s t ␣Name" ,
CONCAT(SUM( Rewards . Reward ) , ’ $ ’ )
FROM
C h a r a c t e r s NATURAL JOIN Rewards
WHERE
Rewards . Reward > 0 ;
TotalReward may now be used as a table.
Q What’s the point of views ?
A
I It hides the internal representation.
I It’s (lazily) updated when you modify the underlying tables.
Database management systems
Data Manipulation Language
Juggling with data
The program
Data Manipulation Language
Inserting Data
Getting your data
Juggling with data
Bottom line
Working with the world
Bottom line
Database management systems
Data Manipulation Language
Juggling with data
Updating
Updating is the act of modifying an existing record.
UPDATE
Characters
SET
C h a r a c t e r s . Age = 1 3 ,
C h a r a c t e r s . S i z e = 150
WHERE
C h a r a c t e r s . " F i r s t ␣Name" = " W i l l i a m "
AND C h a r a c t e r s . " L a s t ␣Name" = " Bonney " ;
Database management systems
Data Manipulation Language
Juggling with data
Updating
Updating is the act of modifying an existing record.
UPDATE
Characters
SET
C h a r a c t e r s . Age = 1 3 ,
C h a r a c t e r s . S i z e = 150
WHERE
C h a r a c t e r s . " F i r s t ␣Name" = " W i l l i a m "
AND C h a r a c t e r s . " L a s t ␣Name" = " Bonney " ;
That looks slow.
Database management systems
Data Manipulation Language
Juggling with data
Moving around
Of course, you can combine UPDATE and SELECT to grab data and put it
somewhere else.
Database management systems
Data Manipulation Language
Juggling with data
Moving around
Of course, you can combine UPDATE and SELECT to grab data and put it
somewhere else.
Q What’s the downside ?
Database management systems
Data Manipulation Language
Juggling with data
Moving around
Of course, you can combine UPDATE and SELECT to grab data and put it
somewhere else.
Q What’s the downside ?
A You’re probably adding redundancy to your database.
Database management systems
Data Manipulation Language
Juggling with data
Removing data
Of course, you can remove needless data
DELETE
Characters
WHERE
C h a r a c t e r s . " F i r s t ␣Name" = " W i l l i a m "
AND C h a r a c t e r s . " L a s t ␣Name" = " Bonney " ;
Database management systems
Data Manipulation Language
Bottom line
Enough about DML
That should be enough about DML.
You now have the tools to
I add data
I
I
locate data
remove data.
There are plenty of variants and plenty of other features. You’ll need to
find these by yourselves.
Database management systems
Working with the world
The program
Data Manipulation Language
Inserting Data
Getting your data
Juggling with data
Bottom line
Working with the world
Bottom line
Database management systems
Working with the world
Something is missing
You still don’t know how to get data into your DBMS or out of it.
Database management systems
Working with the world
Something is missing
You still don’t know how to get data into your DBMS or out of it.
For this, you’ll use external tools and APIs.
Database management systems
Working with the world
There’s a world out there
To import/export data, you’ll usually need another programming
language.
Database management systems
Working with the world
There’s a world out there
To import/export data, you’ll usually need another programming
language.
Q Why ?
Database management systems
Working with the world
There’s a world out there
To import/export data, you’ll usually need another programming
language.
Q Why ?
A Because general-purpose programming languages have
I
memory allocation
I
I
plenty of libraries
access to hardware
I
access to operating system
I
GUIs
loops !
I
Database management systems
Working with the world
Importing data
Example with Visual Basic:
dim
ar
a s ADODB. r e c o r d s e t
s e t a r=new a d o d b . r e c o r d s e t
a r . o p e n {SQL S t a t e m e n t }
a r . addnew
a r ! F i e l d _ 1=Value_1
a r ! F i e l d _ 2=Value_2
...
a r . addnew
a r ! F i e l d _ 1=V a l u e ’ _1
a r ! F i e l d _ 2=V a l u e ’ _2
...
ar . update
Database management systems
Working with the world
If you’re lucky
LOAD DATA INFILE ’ /tmp/ p r o s p e c t s . t x t ’
INTO TABLE p r o s p e c t _ c o n t a c t
FIELDS TERMINATED BY ’ | ’ ;
Database management systems
Working with the world
If you’re lucky
LOAD DATA INFILE ’ /tmp/ p r o s p e c t s . t x t ’
INTO TABLE p r o s p e c t _ c o n t a c t
FIELDS TERMINATED BY ’ | ’ ;
Or mysqlimport or equivalent.
Database management systems
Working with the world
Exporting data
Exporting data is the same kind of deal.
You’ll use some external tool or API, for instance mysqldump.
Database management systems
Bottom line
The program
Data Manipulation Language
Inserting Data
Getting your data
Juggling with data
Bottom line
Working with the world
Bottom line
Database management systems
Bottom line
What we’ve seen so far
I
What DBMSes are all about.
I
How to design relations.
I
How to get data into the DBMS.
I
How to get data into your relations.
How to find your data.
I
I
I
How to remove your data.
Hot to get your data out of the DBMS.
Database management systems
Bottom line
What we’ve seen so far
I
What DBMSes are all about.
I
How to design relations.
I
How to get data into the DBMS.
I
How to get data into your relations.
How to find your data.
I
I
I
How to remove your data.
Hot to get your data out of the DBMS.
Next week: security.
Database management systems
Bottom line
Exercises
1. Add content to your database.
2. Create a view for the well-thinking league, so that they can harrass
alcohol drinkers and people with non-respectable professions.
3. Create a view for bounty hunters, so that they can find the best
bargains (i.e. stupid outlaws with big total rewards).
4. Select everything you need for a "Wanted" poster (name, size, total
reward, list of rewards. . .).
5. Using either OOBase or Access’s "Forms", actually generate the
poster.
Download