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.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizards Guide to PHP by David Lash.
Advertisements

Chapter 10: Designing Databases
1 Query Languages: How to build or interrogate a relational database Structured Query Language (SQL)
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-1.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education 4-1.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial Password: UWPstudent Password is case sensitive.
Introduction To Databases IDIA 618 Fall 2014 Bridget M. Blodgett.
1 CSE 480: Database Systems Lecture 9: SQL-DDL Reference: Read Chapter of the textbook.
Copyright © Curt Hill SQL The Data Definition Language.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Database Lecture # 1 By Ubaid Ullah.
Copyright © 2003 by Prentice Hall Module 4 Database Management Systems 1.What is a database? Data hierarchy and data organization Field, record, file,
ASP.NET Programming with C# and SQL Server First Edition
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
Database Technical Session By: Prof. Adarsh Patel.
Introduction to SQL Steve Perry
2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson.
CST203-2 Database Management Systems Lecture 2. One Tier Architecture Eg: In this scenario, a workgroup database is stored in a shared location on a single.
CS 474 Database Design and Application Terminology Jan 11, 2000.
Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Relational Databases Database Driven Applications Retrieving Data Changing Data Analysing Data What is a DBMS An application that holds the data manages.
Lecture2: Database Environment Prepared by L. Nouf Almujally & Aisha AlArfaj 1 Ref. Chapter2 College of Computer and Information Sciences - Information.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Concepts of Database Management Seventh Edition
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
1 Chapter 1 Introduction. 2 Introduction n Definition A database management system (DBMS) is a general-purpose software system that facilitates the process.
11 3 / 12 CHAPTER Databases MIS105 Lec15 Irfan Ahmed Ilyas.
Instructor: Dema Alorini Database Fundamentals IS 422 Section: 7|1.
Chapter 10: The Data Tier We discuss back-end data storage for Web applications, relational data, and using the MySQL database server for back-end storage.
Lecture2: Database Environment Prepared by L. Nouf Almujally 1 Ref. Chapter2 Lecture2.
Most information comes from Chapter 3, MySQL Tutorial: 1 MySQL: Part.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
Advanced Database CS-426 Week 1 - Introduction. Database Management System DBMS contains information about a particular enterprise Collection of interrelated.
Visual Programing SQL Overview Section 1.
CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.
GLOBEX INFOTEK Copyright © 2013 Dr. Emelda Ntinglet-DavisSYSTEMS ANALYSIS AND DESIGN METHODSINTRODUCTORY SESSION EFFECTIVE DATABASE DESIGN for BEGINNERS.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
1 CS 430 Database Theory Winter 2005 Lecture 10: Introduction to SQL.
CP476 Internet Computing Perl CGI and MySql 1 Relational Databases –A database is a collection of data organized to allow relatively easy access for retrievals,
Chapter 18 Object Database Management Systems. Outline Motivation for object database management Object-oriented principles Architectures for object database.
1 Geog 357: Data models and DBMS. Geographic Decision Making.
Working with MySQL A290/A590, Fall /07/2014.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Introduction: Databases and Database Systems Lecture # 1 June 19,2012 National University of Computer and Emerging Sciences.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
Introduction to Database Programming with Python Gary Stewart
CS 3630 Database Design and Implementation
Understanding SQL Statements
CS1222 Using Relational Databases and SQL
Chapter 8 Working with Databases and MySQL
CS1222 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
JDBC II IS
CS1222 Using Relational Databases and SQL
Presentation transcript:

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 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

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

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.

Levels of Abstraction

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

Example Table

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)

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’;

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

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

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.

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?

Example Fred Mark George Quinn IDCredit Card

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>

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

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>

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

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

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

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’, ‘ 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’, ‘ 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.

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 | 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 | rows in set (0.00 sec)

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

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

Updating Rows G UPDATE Image SET url=‘ 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=‘ 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

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!!!

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 |

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;

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

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

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();

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”

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

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

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

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

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