Professional Documents
Culture Documents
What is a database?
a collection of data
Usually consists of entities and
relations
An entity is an individual object that exists and is distinguishable from other individuals. Example: specific person, company, event, plant Entities have attributes Example: people have names and addresses A relationship is an association among several entities
Data Models
A data model is a collection of concepts for describing data. A schema is a description of a particular collection of data, using the given data model. The relational model of data is the most widely used model today.
Main concept: relation, basically a table with
rows and columns. Every relation has a schema, which describes the columns, or fields.
Levels of Abstraction
Relational Databases
Data is logically perceived as a two-
dimensional table Relational databases are sets of tables tables are relations
Example Table
Manipulation Language of a DBMS (we also need to create, update, insert data) Language: Structured Query Language (SQL)
in that two tables share the same attribute (like the accession number) In fact, in a relational DB, this is the only way to connect distinct tables, at the logical level anyway A link between one table and another is called a foreign key
access. Reduced application development time. Data integrity and security. Uniform data administration. Concurrent access, recovery from crashes.
Example
Suppose you created a file to hold names, ID numbers and faculty/student status This was a flat file that resembled a table in a database What if you wanted to now add new data for some of the faculty with credit card information? How would you connect the two tables?
Example
Fred 1234567
Mark
George Quinn ID 1234567 4567890
2345678
3456789 4567890 Credit Card 44444444 55555555
Database
mysql
test
tmp
Creating Tables
CREATE TABLE image (
image_id INT, image_type CHAR(3), filename CHAR(40), url CHAR(128), Primary Key(image_id)); creates a table with 4 columns and no rows
Another table
create table image_decoder
REAL - floating point number BIT - single boolean value DATE - year, month, day TIME TIMESTAMP - date/time VARCHAR(length) - variable length string <= length BLOB - Binary Large Object
Values must be the order specified. But, you dont need to fill all columns.
More
insert into image_decoder
values('jpg','/usr/bin/jpgview,);
Selecting Rows
SELECT image_type from image
WHERE filename=image1 SELECT image_decoder.decoder_program FROM image_decoder, image WHERE image.filename=image1 AND image.image_type=image_decoder.image_type
The Join operation can be viewed as creating a virtual table
ISNULL - checks for null value IN - contained in a set (usually for subqueries)
WHERE image_id IN (1,2) WHERE image_id IN
Updating Rows
UPDATE Image
SET url=http://newhost/image1 WHERE filename=image1
The where clause may select multiple rows e.g. WHERE image_id < 50 If the WHERE clause is excluded, the SET operation is applied to every row in the table
Deleting Rows
DELETE from Image
WHERE image_id=2 Entire row is removed from the table
Redo load with up arrow select title, accession from seqs; update seqs set accession = 'H0794' where title = 'Human-01'; select * from seqs order by title;
More commands
mysql> select * from seqs where title
like 'Human%';
More commands
use mysql;
show tables; describe db;
PERL DBI
$dbh = DBI->connect("dbi:mysql: database=sequences; host=paintball:1236;", "phylo","") or die("Couldn't connect"); $SQL= "select * from seqs";
Perl A perl module and specification that defines a consistent database interface independent of the actual database being used
Sybase, Postgress,
Quickbase,
functionality and non-SQL databases Conform to applicable standards (ODBC etc.) Enable the creation of database-independent Perl scripts without being limited to the lowest functionality Be free.
drivers Not limited to the lowest common denominator mechanism provided for driver specific extensions Designed and built for speed Valuable detailed call tracing/debugging built-in