Introduction to SQL. Relational databases  Practical level: A data storage that can effectively been queried with SQL query language  A database consists.

Slides:



Advertisements
Similar presentations
Introduction to SQL, OleDB interface to Access from VB.NET.
Advertisements

Session 2Introduction to Database Technology Data Types and Table Creation.
SQL Rohit Khokher.
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Day 3 - Basics of MySQL What is MySQL What is MySQL How to make basic tables How to make basic tables Simple MySQL commands. Simple MySQL commands.
The University of Akron Dept of Business Technology Computer Information Systems The Relational Model: Query-By-Example (QBE) 2440: 180 Database Concepts.
Chapter 8 Special-Purpose Languages. SQL SQL stands for "Structured Query Language". Allows the user to pose complex questions of a database. It also.
Creating Database Tables CS 320. Review: Levels of data models 1. Conceptual: describes WHAT data the system contains 2. Logical: describes HOW the database.
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
Concepts of Database Management Sixth Edition
Microsoft Access 2010 Chapter 7 Using SQL.
SQL Basics+ Brandon Checketts. Why SQL? Structured Query Language Structured Query Language Frees programmers from dealing with specifics of data persistence.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
ASP.NET Programming with C# and SQL Server First Edition
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Concepts of Database Management Seventh Edition
2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Introduction to MySQL Lab no. 10 Advance Database Management System.
CSC 2720 Building Web Applications Database and SQL.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
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.
Information Technologies and Microsoft SQL Server Day 2 by Alper Özpınar
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
1 MySQL and SQL. 2 Topics  Introducing Relational Databases  Terminology  Managing Databases MySQL and SQL.
Relational Databases and MySQL. Relational Databases Relational databases model data by storing rows and columns in tables. The power of the relational.
Oracle & SQL. Oracle Data Types Character Data Types: Char(2) Varchar (20) Clob: large character string as long as 4GB Bolb and bfile: large amount of.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Lecture 1.21 SQL Introduction Steven Jones, Genome Sciences Centre.
3 A Guide to MySQL.
How to: SQL By: Sam Loch.
SQL Query Getting to the data ……..
Database Access with SQL
CS320 Web and Internet Programming SQL and MySQL
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Intro to PostgreSQL.
Chapter 7 Working with Databases and MySQL
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Structured Query Language
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
M1G Introduction to Database Development
Introduction To Structured Query Language (SQL)
CS3220 Web and Internet Programming SQL and MySQL
Data.
MySQL Database System Installation Overview SQL summary
SQL Basics BCHB697.
CS3220 Web and Internet Programming SQL and MySQL
The University of Akron College of Applied Science & Technology Dept
Structured Query Language Path from Unorganized to Organized….
Shelly Cashman: Microsoft Access 2016
JDBC II IS
Introduction to SQL Server and the Structure Query Language
Presentation transcript:

Introduction to SQL

Relational databases  Practical level: A data storage that can effectively been queried with SQL query language  A database consists of one or several tables  Common relational database program: Oracle Access MySQL PostgreSQL

Exel-sheet  Let's try to move this to SQL-database First_nameFamily_nameeye colourshoesizeweight LeenaLahtinenblue3858,6 PekkaSalobrown4175,9 HeikkiMäkeläbrown4385,3 PekkaVirtanenblue3654,8 PekkaVirtanenbrown4291,5 MattiLehtogreen4188,7

Creating tables  At kaivos.csc.fi, an empty database is created by CSC  The user must construct the tables used in the database  In table construction you must define the field (column) names and the type of data that will be stored to each field  You may also define test conditions or default values  Datatypes define what data can be stored and how much space each data item requires

Some data types in MySQL  String data CHAR(length) VARCHAR(length) TEXT long text BLOBbinary data, images, files  Numerical data INT or INTEGER: Can have values – Also TINYINT, SMALLINT, MEDUIMINT, BIGINT DECIMAL (digits, decimals): exact decimal numbers FLOAT and DOUBLE: approximate numerical values  DATES and time DATE, TIME and TIMESTAMP  String data (text) VARCHAR(length)  Numerical data INT or INTEGER (  String data (text) VARCHAR(length)  Numerical data INT or INTEGER Can have values

Exel-sheet CREATE TABLE `list` ( `first_name` VARCHAR(20), `family_name` VARCHAR(40), `eye` VARCHAR(5), `shoe_size` INT, `weight` DECIMAL(4,1) ) CREATE TABLE `list` ( `first_name` VARCHAR(20), `family_name` VARHAR(40), `phone` VARCHAR(30), `shoe_size` INT, `weight` DECIMAL(4,1) ) First_nameFamily_nameeye colourshoesizeweight LeenaLahtinenblue3858,6 AskoSalobrown4175,9 HeikkiMäkeläbrown4385,3 PekkaVirtanenblue3654,8 PekkaVirtanenbrown4291,5 MattiLehtogreen4188,7

Using the table You could now load in the data to the database 1. Store the data as csv or tab delimited text file 2. Use mysqlimport tool (note that the column order in the text file must match to that of database) When data is loaded, you can start doing queries using MySQL client and SELECT-statements: SELECT * FROM list WHERE first_name=”Pekka”; SELECT shoe_size, weight FROM list WHERE weight>60;

The table created is not efficient No primary key defined Data repetition To improve the situation Add primary key field Split file (normalization)

New tables ID First_na me Family_nameeye_type shoe size weight 1LeenaLahtinen13858,6 2PekkaSalo24175,9 3HeikkiMäkelä24385,3 4PekkaVirtanen13654,8 5PekkaVirtanen24291,5 6MattiLehto34188,7 eye_ty pe colour 1blue 2brown 3green PERSONS EYES

Table 1. IDFirst_nameFamily_nameeye_type shoe sizeweight 1LeenaLahtinen13858,6 2PekkaSalo24175,9 3HeikkiMäkelä24385,3 4PekkaVirtanen13654,8 5PekkaVirtanen14291,5 6MattiLehto34188,7 CREATE TABLE `persons` ( `ID` INT, `first_name` VARCHAR(20), `family_name` VARHAR(40), `eye_type` SMALLINT, `shoe_size` SMALLINT, `weight` DECIMAL(4,1), PRIMARY KEY (`ID`) ) CREATE TABLE `persons` ( `ID` INT, `first_name` VARCHAR(20), `family_name` VARHAR(40), `eye_type` SMALLINT, `shoe_size` SMALLINT, `weight` DECIMAL(4,1), )

eye_typecolour 1blue 2brown 3green EYES CREATE TABLE `eyes` ( `eye_type` SMALLINT, `colour` VARCHAR(5), PRIMARY KEY (`eye_type`) )

Using the new database structure  You could now load in the data to the database: – 1. Store the data as a tab delimited or csv text file – 2. Modify your data set to match the database structure – 3. Use mysqlimport tool When data is loaded, you can start doing queries using MySQL client and SELECT-statements: SELECT shoe_size, weight FROM persons WHERE weight>60; SELECT id, eye_colour FROM persons, eyes WHERE first_name=”Pekka” AND persons.eye_type=eyes_eye.type;

Main SQL commands SELECT retrieve data from the database DELETEdelete rows from database INSERT add data to a table UPDATEchange data in a table

SELECT SELECT column names FROM table name(s) WHERE selection condition ORDER BY how to order the results GROUP BY how to group the results HAVING what to search form the grouping results LIMIT maximun number of results to show SELECT DISTINCT print identical result lines only once SELECT column1, column2 FROM table_name

When constructing WHERE condition you can use: Comparison operations (not a complete list): =Equal >= Greater than or equal > Greater than GREATEST() Return the largest argument IS Boolean comparison LEAST() Return the smallest argument <= Less than or equal <Less than operator LIKE Simple pattern matching != Not equal operator Logical operations: AND OR NOT XOR Other things Use quotation marks with strings...WHERE first_name=”Pekka” Use brackets to define comparison order...WHERE a!=0 AND( b=0 OR C=0)

Aggregate functions for SELECT AVG() Return the average value of the argument COUNT(DISTINCT) Return the count of a number of different values COUNT() Return a count of the number of rows returned MAX() Return the maximum value MIN() Return the minimum value STD() Return the population standard deviation SUM() Return the sum VARIANCE() Return the population standard variance SELECT COUNT(*) FROM list WHERE first_name=”Pekka”; SELECT MAX(weight) FROM list;

DELETE and UPDATE Use DELETE to remove data from a table DELETE FROM list WHERE first_name=”test_patient”; Use UPDATE to change values in a table UPDATE list SET shoe_size=44 WHERE family_name=”Salo”

INSERT INTO You can add data to a table with command INSERT INTO Note that you must give proper values for all the columns if you wish to avoid null-values In scripts you should explicitly define the connection between a value and column name INSERT INTO list (First_name, Family_name, eyecolor, shoesize, weight) VALUES ( “Ville”, “Mäki”,”blue”,43,72.8)

With real (i.e. big) databases Use EXPLAIN command to test how heavy your query is Consider indexing some of the columns Keep the MySQL manual pages open and read them: