Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL. What is a database? G a collection of data G Usually consists of entities and relations G An entity is an individual “object” that exists and is.

Similar presentations


Presentation on theme: "SQL. What is a database? G a collection of data G Usually consists of entities and relations G An entity is an individual “object” that exists and is."— Presentation transcript:

1 SQL

2 What is a database? G a collection of data G Usually consists of entities and relations G An entity is an individual “object” that exists and is distinguishable from other individuals. Example: specific person, company, event, plant G Entities have attributes Example: people have names and addresses G A relationship is an association among several entities G a collection of data G Usually consists of entities and relations G An entity is an individual “object” that exists and is distinguishable from other individuals. Example: specific person, company, event, plant G Entities have attributes Example: people have names and addresses G A relationship is an association among several entities

3 Database Management System (DBMS) G A computerized record-keeping system G Allows operations such as: G Adding new files G Inserting data into existing files G Retrieving data from existing files G Changing data G Deleting data G Removing existing files from the database G A computerized record-keeping system G Allows operations such as: G Adding new files G Inserting data into existing files G Retrieving data from existing files G Changing data G Deleting data G Removing existing files from the database

4 Data Models G A data model is a collection of concepts for describing data. G A schema is a description of a particular collection of data, using the given data model. G The relational model of data is the most widely used model today. G Main concept: relation, basically a table with rows and columns. G Every relation has a schema, which describes the columns, or fields. G A data model is a collection of concepts for describing data. G A schema is a description of a particular collection of data, using the given data model. G The relational model of data is the most widely used model today. G Main concept: relation, basically a table with rows and columns. G Every relation has a schema, which describes the columns, or fields.

5 Levels of Abstraction

6 Relational Databases G Data is logically perceived as a two- dimensional table G Relational databases are sets of tables G tables are relations G Data is logically perceived as a two- dimensional table G Relational databases are sets of tables G tables are relations

7 Example Table

8 Relational Database Query G A relational database query is a question about the data, and the answer consists of a new relation containing the result. G Queries are one part of the Data Manipulation Language of a DBMS (we also need to create, update, insert data) G Language: Structured Query Language (SQL) G A relational database query is a question about the data, and the answer consists of a new relation containing the result. G Queries are one part of the Data Manipulation Language of a DBMS (we also need to create, update, insert data) G Language: Structured Query Language (SQL)

9 Example SQL query  Select G.Accession, G.Medline  From Genebank G  Where G.source=`baker’s yeast’;  Select G.Accession, G.Medline  From Genebank G  Where G.source=`baker’s yeast’;

10 No explicit links between tables G Of course, there may be implicit links in that two tables share the same attribute (like the accession number) G In fact, in a relational DB, this is the only way to connect distinct tables, at the logical level anyway G A link between one table and another is called a foreign key G Of course, there may be implicit links in that two tables share the same attribute (like the accession number) G In fact, in a relational DB, this is the only way to connect distinct tables, at the logical level anyway G A link between one table and another is called a foreign key

11 Tables and Keys image_idimage_typefilenameurl 1gifImage1…... image_typedecoder_programargs gifc:\gifdecoder…... Primary Keys Foreign Key

12 Why use a DBMS G Data independence and efficient access. G Reduced application development time. G Data integrity and security. G Uniform data administration. G Concurrent access, recovery from crashes. G Data independence and efficient access. G Reduced application development time. G Data integrity and security. G Uniform data administration. G Concurrent access, recovery from crashes.

13 Example G Suppose you created a file to hold names, ID numbers and faculty/student status G This was a flat file that resembled a table in a database G What if you wanted to now add new data for some of the faculty with credit card information? G How would you connect the two tables? G Suppose you created a file to hold names, ID numbers and faculty/student status G This was a flat file that resembled a table in a database G What if you wanted to now add new data for some of the faculty with credit card information? G How would you connect the two tables?

14 Example Fred1234567 Mark2345678 George3456789 Quinn4567890 IDCredit Card 123456744444444 456789055555555

15 How to use MySQL  Connect to MySQL Server shell> ~snell/mysqlrun/bin/mysql -h paintball -u CS360 -p password: passwd Welcome to the MySQL monitor. Type 'help' for help. mysql>  Connect to MySQL Server shell> ~snell/mysqlrun/bin/mysql -h paintball -u CS360 -p password: passwd Welcome to the MySQL monitor. Type 'help' for help. mysql>

16 How to use MySQL  Data Definition 1 mysql> SHOW DATABASES;  Data Definition 1 mysql> SHOW DATABASES; Database mysql test tmp

17 How to use MySQL Data Definition 2 mysql> CREATE DATABASE sequences-yourname; mysql> USE sequences-yourname Database changed mysql> SHOW TABLES; Empty set (0.00 sec) mysql>

18 Creating Tables G CREATE TABLE Image ( image_id INT, image_type CHAR(3), filename CHAR(40), url CHAR(128), Primary Key(image_id)); G creates a table with 4 columns and no rows G CREATE TABLE Image ( image_id INT, image_type CHAR(3), filename CHAR(40), url CHAR(128), Primary Key(image_id)); G creates a table with 4 columns and no rows

19 Basic Data Types G INT - signed integer value. Implementation-dependent # bits G NUMERIC(total length, number of decimal places) G NUMERIC(8,4) - 3 digits, a decimal point, 4 decimal places G REAL - floating point number G BIT - single boolean value G DATE - year, month, day G TIME G TIMESTAMP - date/time G VARCHAR(length) - variable length string <= length G BLOB - Binary Large Object G INT - signed integer value. Implementation-dependent # bits G NUMERIC(total length, number of decimal places) G NUMERIC(8,4) - 3 digits, a decimal point, 4 decimal places G REAL - floating point number G BIT - single boolean value G DATE - year, month, day G TIME G TIMESTAMP - date/time G VARCHAR(length) - variable length string <= length G BLOB - Binary Large Object

20 How to use MySQL G Data definition mysql> create table seqs (title varchar(20), -> accession varchar(20), -> sequence varchar(20)) -> ;

21 How to use MySQL G Data Manipulation 1 mysql> insert into seqs -> values('Human','u235','cgatcagt'); G INSERT INTO Image ( image_id, image_type, filename, url) VALUES ( 1, ‘jpg’, ‘image1’, ‘http://host/dir/image1’) G Data Manipulation 1 mysql> insert into seqs -> values('Human','u235','cgatcagt'); G INSERT INTO Image ( image_id, image_type, filename, url) VALUES ( 1, ‘jpg’, ‘image1’, ‘http://host/dir/image1’) Values must be in the right order and fill all columns Values must be the order specified. But, you don’t need to fill all columns. Values must be the order specified. But, you don’t need to fill all columns.

22 How to use MySQL mysql> select * from seqs where accession='u235'; +-------+-----------+----------+ | title | accession | sequence | +-------+-----------+----------+ | Human | u235 | cgatcagt | +-------+-----------+----------+ mysql> select sequence from seqs -> ; +----------+ | sequence | +----------+ | cgatcagt | | ccgtacgt | +----------+ 2 rows in set (0.00 sec) mysql> select * from seqs where accession='u235'; +-------+-----------+----------+ | title | accession | sequence | +-------+-----------+----------+ | Human | u235 | cgatcagt | +-------+-----------+----------+ mysql> select sequence from seqs -> ; +----------+ | sequence | +----------+ | cgatcagt | | ccgtacgt | +----------+ 2 rows in set (0.00 sec)

23 Selecting Rows G SELECT image_type from Image WHERE filename=‘image1’ G SELECT Image_Decoder.decoder_program FROM Image_Decoder, Image WHERE Image.filename=‘image1’ AND Image.image_type=Image_Decoder.image_type G The Join operation can be viewed as creating a virtual table on the fly from rows in two or more tables G SELECT * from Image GROUP by image_type G SELECT image_type from Image WHERE filename=‘image1’ G SELECT Image_Decoder.decoder_program FROM Image_Decoder, Image WHERE Image.filename=‘image1’ AND Image.image_type=Image_Decoder.image_type G The Join operation can be viewed as creating a virtual table on the fly from rows in two or more tables G SELECT * from Image GROUP by image_type

24 Basic Where Clauses G Operators G =,, =, != (or <>) G WHERE image_id = 2 G LIKE - wildcard comparison G WHERE decoder_program LIKE ‘c:%’ G ISNULL - checks for null value G IN - contained in a set (usually for subqueries) G WHERE image_id IN (1,2) G WHERE image_id IN SELECT image_id FROM Image G Operators G =,, =, != (or <>) G WHERE image_id = 2 G LIKE - wildcard comparison G WHERE decoder_program LIKE ‘c:%’ G ISNULL - checks for null value G IN - contained in a set (usually for subqueries) G WHERE image_id IN (1,2) G WHERE image_id IN SELECT image_id FROM Image

25 Updating Rows G UPDATE Image SET url=‘http://newhost/image1’ WHERE filename=‘image1’ G The where clause may select multiple rows e.g. WHERE image_id < 50 G If the WHERE clause is excluded, the SET operation is applied to every row in the table G UPDATE Image SET url=‘http://newhost/image1’ WHERE filename=‘image1’ G The where clause may select multiple rows e.g. WHERE image_id < 50 G If the WHERE clause is excluded, the SET operation is applied to every row in the table

26 Deleting Rows G DELETE from Image WHERE image_id=2 G Entire row is removed from the table G DELETE from Image G Every row is removed from the table!!! G DELETE from Image WHERE image_id=2 G Entire row is removed from the table G DELETE from Image G Every row is removed from the table!!!

27 How to use MySQL G Data manipulation 2 mysql> SELECT * FROM seqs; +-------+-----------+----------+ | title | accession | sequence | +-------+-----------+----------+ | Human | u235 | cgatcagt | +-------+-----------+----------+ mysql> insert into seqs -> values('Dog','u222','ccgtacgt'); mysql> SELECT * FROM seqs; +-------+-----------+----------+ | title | accession | sequence | +-------+-----------+----------+ | Human | u235 | cgatcagt | | Dog | u222 | ccgtacgt | +-------+-----------+----------+ G Data manipulation 2 mysql> SELECT * FROM seqs; +-------+-----------+----------+ | title | accession | sequence | +-------+-----------+----------+ | Human | u235 | cgatcagt | +-------+-----------+----------+ mysql> insert into seqs -> values('Dog','u222','ccgtacgt'); mysql> SELECT * FROM seqs; +-------+-----------+----------+ | title | accession | sequence | +-------+-----------+----------+ | Human | u235 | cgatcagt | | Dog | u222 | ccgtacgt | +-------+-----------+----------+

28 Add data from file G mysql> load data local infile ’/users/faculty/snell/CS360/sample.txt' into table seqs; G Delete it G mysql> delete from seqs G Redo load with up arrow G select title, accession from seqs; G update seqs set accession = 'H0794' where title = 'Human-01'; G select * from seqs order by title; G mysql> load data local infile ’/users/faculty/snell/CS360/sample.txt' into table seqs; G Delete it G mysql> delete from seqs G Redo load with up arrow G select title, accession from seqs; G update seqs set accession = 'H0794' where title = 'Human-01'; G select * from seqs order by title;

29 More commands G mysql> select * from seqs where title like 'Human%';

30 More commands G use mysql; G show tables; G describe db; G use mysql; G show tables; G describe db;

31 PERL DBI $dbh = DBI->connect("dbi:mysql: database=sequences; host=paintball:1236;", "phylo","") or die("Couldn't connect"); $SQL= "select * from seqs"; $Select = $dbh->prepare($SQL); $Select->execute(); while($Row=$Select->fetchrow_hashref) print "title $Row->{title}, sequence $Row->{sequence} \n"; $dbh->disconnect();

32 What Is the Perl DBI? G The standard Database Interface for Perl G “A perl module and specification that defines a consistent database interface independent of the actual database being used” G The standard Database Interface for Perl G “A perl module and specification that defines a consistent database interface independent of the actual database being used”

33 Why the Perl DBI? G Once upon a time… G One language, many database interfaces G Perl 5 - A new way G Modules and Objects. The DBI is born. G The future is now… G ODBC, Oracle, Informix, Ingres, mSQL, mysql, DB2, Solid, Sybase, Postgress, Quickbase, Empress, Fulcrum,... G The same database interface G Once upon a time… G One language, many database interfaces G Perl 5 - A new way G Modules and Objects. The DBI is born. G The future is now… G ODBC, Oracle, Informix, Ingres, mSQL, mysql, DB2, Solid, Sybase, Postgress, Quickbase, Empress, Fulcrum,... G The same database interface

34 Making simple things easy and difficult things possible G Goals G Be simple to use for simple applications G Have sufficient flexibility to accommodate unusual functionality and non-SQL databases G Conform to applicable standards (ODBC etc.) G Enable the creation of database-independent Perl scripts without being limited to the lowest functionality G Be free. G A ‘higher-level’ interface than ODBC/JDBC G Goals G Be simple to use for simple applications G Have sufficient flexibility to accommodate unusual functionality and non-SQL databases G Conform to applicable standards (ODBC etc.) G Enable the creation of database-independent Perl scripts without being limited to the lowest functionality G Be free. G A ‘higher-level’ interface than ODBC/JDBC

35 Under the Hood G DBI defines and implements an interface G Driver modules do much of the real work G DBI provides default methods, functions, tools etc for drivers G Not limited to the lowest common denominator - mechanism provided for driver specific extensions G Designed and built for speed G Valuable detailed call tracing/debugging built-in G DBI defines and implements an interface G Driver modules do much of the real work G DBI provides default methods, functions, tools etc for drivers G Not limited to the lowest common denominator - mechanism provided for driver specific extensions G Designed and built for speed G Valuable detailed call tracing/debugging built-in

36 A Picture is Worth... DBI Module Perl Application DBD::OtherDBD::InformixDBD::Oracle Oracle ServerInformix ServerOther Server

37 So why use the Perl DBI? G Because... G It delivers what it promises G It’s here, there and everywhere G It’s fast, flexible and well proven G It’s free, with source G Commercial support is available G It has a large user base and a strong future G Because... G It delivers what it promises G It’s here, there and everywhere G It’s fast, flexible and well proven G It’s free, with source G Commercial support is available G It has a large user base and a strong future


Download ppt "SQL. What is a database? G a collection of data G Usually consists of entities and relations G An entity is an individual “object” that exists and is."

Similar presentations


Ads by Google