SQL Basics+ Brandon Checketts. Why SQL? Structured Query Language Structured Query Language Frees programmers from dealing with specifics of data persistence.

Slides:



Advertisements
Similar presentations
Database Chapters.
Advertisements

MySQL. To start go to Login details: login: labuser password:macimd15 – There.
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.
MySQL-Database Teppo Räisänen Oulu University of Applied Sciences School of Business and Information Management.
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
Creating Database Tables CS 320. Review: Levels of data models 1. Conceptual: describes WHAT data the system contains 2. Logical: describes HOW the database.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
ADVM420- Class #4 Web Design with PHP and MySQL Adding and Listing from a MySQL Database.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
SQLite 1 CS440. What is SQLite?  Open Source Database embedded in Android  SQL syntax  Requires small memory at runtime (250 Kbytes)  Lightweight.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
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.
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.
DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.
Copyright © Curt Hill SQL The Data Definition Language.
MySQL Dr. Hsiang-Fu Yu National Taipei University of Education
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Lecture 9 – MYSQL and PHP (Part1) SFDV3011 – Advanced Web Development 1.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
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.
1 Working with MS SQL Server Textbook Chapter 14.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Working with MSSQL Server Code:G0-C# Version: 1.0 Author: Pham Trung Hai CTD.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
Chapter 4 Introduction to MySQL. MySQL “the world’s most popular open-source database application” “commonly used with PHP”
LIS651 lecture 6 mySQL Thomas Krichel
Introduction to MySQL Lab no. 10 Advance Database Management System.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
Introduction to Internet Databases MySQL Database System Database Systems.
CSC 2720 Building Web Applications Database and SQL.
SQL Data Definition Language (DDL) Using Microsoft SQL Server 1SDL Data Definition Language (DDL)
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
MySQL More… 1. More on SQL In MySQL, the Information Schema is the “Catalog” in the SQL standard SQL has three components: Data definition Data manipulation.
What’s a database? Data stored in a structured format that lends itself to easy manipulation and recall.
Advanced Web 2012 Lecture 3 Sean Costain What is a Database? Sean Costain 2012 A database is a structured way of dealing with structured information.
Visual Programing SQL Overview Section 1.
Sql DDL queries CS 260 Database Systems.
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
>> 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.
DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.
1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL.
Relational Databases and MySQL. Relational Databases Relational databases model data by storing rows and columns in tables. The power of the relational.
IS6146 Databases for Management Information Systems Lecture 3: SQL III – The DDL Rob Gleasure robgleasure.com.
Introduction to MySQL Ullman Chapter 4. Introduction MySQL most popular open-source database application Is commonly used with PHP We will learn basics.
Relational Databases and MySQL Charles Severance
Working with MySQL A290/A590, Fall /07/2014.
1 Working with MS SQL Server Beginning ASP.NET in C# and VB Chapter 12.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
LECTURE FOUR Introduction to SQL DDL with tables DML with tables.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
Introduction to MySQL  Working with MySQL and MySQL Workbench.
Programming for the Web MySQL Command Line Using PHP with MySQL Dónal Mulligan BSc MA
Lecture 1.21 SQL Introduction Steven Jones, Genome Sciences Centre.
CS320 Web and Internet Programming SQL and MySQL
MySQL-Database Jouni Juntunen Oulu University of Applied Sciences
Database application MySQL Database and PhpMyAdmin
Database systems Lecture 2 – Data Types
MySQL Dr. Hsiang-Fu Yu National Taipei University of Education
Paul Jacobs The iSchool University of Maryland Thursday, Oct. 5, 2017
CS3220 Web and Internet Programming SQL and MySQL
Data.
MySQL Database System Installation Overview SQL summary
CS3220 Web and Internet Programming SQL and MySQL
MySQL Database System Installation Overview SQL summary
Presentation transcript:

SQL Basics+ Brandon Checketts

Why SQL? Structured Query Language Structured Query Language Frees programmers from dealing with specifics of data persistence Frees programmers from dealing with specifics of data persistence Cross-platform, language independent Cross-platform, language independent Indexing and data optimization Indexing and data optimization Data integrity Data integrity

Some Pitfalls Vendor-Specific features Vendor-Specific features Standardization is not great Standardization is not great Complexity? Additional Overhead? Complexity? Additional Overhead?

SQL Engines MySQL MySQL PostgreSQL PostgreSQL Informix Informix Oracle Oracle MSSQL MSSQL Many others Many others

Database Organization A database server may have multiple databases A database server may have multiple databases Each database is made up of one or more tables Each database is made up of one or more tables Queries can select from multiple databases and tables. Queries can select from multiple databases and tables.

Accessing your Database Command Line Command Line Web / GUI Interfaces Web / GUI Interfaces Programmatically Programmatically Spreadsheets (Excel) Spreadsheets (Excel) Reporting Applications (Crystal Reports) Reporting Applications (Crystal Reports)

INSERT and SELECT INSERT INTO kids INSERT INTO kids SET name = ‘Noah’, SET name = ‘Noah’, status = ‘nice’; status = ‘nice’; SELECT * FROM kids WHERE name = ‘Noah’

Table Manipulation CREATE CREATE CREATE TABLE `christmas`.`kids` ( CREATE TABLE `christmas`.`kids` ( `name` VARCHAR( 40 ) NOT NULL, `status` VARCHAR( 7 ) NOT NULL `name` VARCHAR( 40 ) NOT NULL, `status` VARCHAR( 7 ) NOT NULL ) ; ) ; ALTER ALTER ALTER TABLE `kids` ALTER TABLE `kids` CHANGE `name` `first_name` VARCHAR( 40 ), CHANGE `name` `first_name` VARCHAR( 40 ), ADD `last_name` VARCHAR( 40 ) NOT NULL AFTER `first_name` ; ADD `last_name` VARCHAR( 40 ) NOT NULL AFTER `first_name` ; DROP DROP

Column Types Char, varchar, text, longtext Char, varchar, text, longtext Int, tinyint, smallint, mediumint, bigint Int, tinyint, smallint, mediumint, bigint Float, double, decimal, Float, double, decimal, Blob (binary large objects) Blob (binary large objects) Date, datetime, timestamp, year, Date, datetime, timestamp, year, Enum, bool Enum, bool

Santa’s Database Santa would like to move into the 21 st century and start keeping all of his required information in a database. Santa would like to move into the 21 st century and start keeping all of his required information in a database. Lets try developing it ourselves Lets try developing it ourselves Demonstrate creating a ‘christmas’ database using phpMyAdmin (including user/pass) Demonstrate creating a ‘christmas’ database using phpMyAdmin (including user/pass) Create kids table Create kids table What columns might we need? What types? What columns might we need? What types?

Santa’s Christmas App Santa decided that developing this entire application by himself is too complicated. Santa decided that developing this entire application by himself is too complicated. He found an open-source application that he wants to use to track his lists. We’ve installed it at: He found an open-source application that he wants to use to track his lists. We’ve installed it at:

Kids Table mysql> describe kids; | Field | Type | Null | Key | Default | Extra | | id | int(11) | NO | PRI | NULL | auto_increment | | first_name | varchar(40) | NO | | | | | last_name | varchar(40) | NO | | | | | status | varchar(7) | NO | | | | | zip | varchar(5) | NO | | | | | modified | timestamp | NO | | CURRENT_TIMESTAMP | | Mysql> show create table kids; …… CREATE TABLE `kids` ( CREATE TABLE `kids` ( `id` int(11) NOT NULL auto_increment, `id` int(11) NOT NULL auto_increment, `first_name` varchar(40) NOT NULL, `first_name` varchar(40) NOT NULL, `last_name` varchar(40) NOT NULL, `last_name` varchar(40) NOT NULL, `status` varchar(7) NOT NULL, `status` varchar(7) NOT NULL, `zip` varchar(5) NOT NULL, `zip` varchar(5) NOT NULL, `modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, `modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, PRIMARY KEY (`id`) PRIMARY KEY (`id`) ) ENGINE=MyISAM; ) ENGINE=MyISAM;

Adding reports Santa is very happy with his new application. Now he’d like to add some additional features Santa is very happy with his new application. Now he’d like to add some additional features What reports might we want to add? What reports might we want to add? What have kids wished for? What have kids wished for? Kids who have been naughty Kids who have been naughty Kids who have been nice Kids who have been nice Kids who are avoiding being checked up on Kids who are avoiding being checked up on

Gift Lists (Importing from CSV) Santa Elves have compiled gift lists and have them available in a CSV format Santa Elves have compiled gift lists and have them available in a CSV format We can create a table for them and load them directly from CSV We can create a table for them and load them directly from CSV mysql>CREATE TABLE `christmas`.`gifts` ( `kid_id` INT NOT NULL, `kid_id` INT NOT NULL, `gift` VARCHAR( 255 ) NOT NULL `gift` VARCHAR( 255 ) NOT NULL ) ENGINE = MYISAM ; mysql> LOAD DATA local infile ‘gifts.csv' INTO TABLE gifts INTO TABLE gifts FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n'

Manufacturing Report Santa is an optimist and hopes that all kids will be good and get what they asked for. He needs a report to pass on to his elves so that they know what to manufacture Santa is an optimist and hopes that all kids will be good and get what they asked for. He needs a report to pass on to his elves so that they know what to manufacture SELECT gift, COUNT(gift) AS count FROM gifts GROUP BY gift ORDER BY count DESC;

Date/Time Functions SELECT * FROM sometable SELECT * FROM sometable WHERE timestamp > NOW() WHERE timestamp > NOW() WHERE timestamp > DATE_SUB( NOW(), INTERVAL 7 DAY) WHERE timestamp > DATE_SUB( NOW(), INTERVAL 7 DAY)

Sleigh Loading Report We only want to load gifts for kids that have been nice We only want to load gifts for kids that have been nice We’ll introduce a JOIN on the kids table We’ll introduce a JOIN on the kids table SELECT gift, COUNT(gift) AS count SELECT gift, COUNT(gift) AS count FROM gifts FROM gifts JOIN kids ON kids.id = gifts.kid_id JOIN kids ON kids.id = gifts.kid_id WHERE kids.status = 'nice' WHERE kids.status = 'nice' GROUP BY gifts.gift GROUP BY gifts.gift ORDER BY count DESC ORDER BY count DESC

What is Santa’s sleight doesn’t have enough room for all toys? He might have to reload his sleigh based on geography He might have to reload his sleigh based on geography We could query kids within a radius of a given location, that would be helpful We could query kids within a radius of a given location, that would be helpful We have the kids zip codes. Maybe we could group those together? We have the kids zip codes. Maybe we could group those together?

Exporting and Importing Mysqldump to export Mysqldump to export mysqldump db zipcode |gzip -c > zipcode.sql.gz Import with: Import with: zcat zipcode.sql.gz| mysql christmas zcat zipcode.sql.gz| mysql christmas

SQL Arithmetic SQL Can do semi-complicated arithmetic: SQL Can do semi-complicated arithmetic: Find all zip codes with in a distance of a lat/lon: SELECT zc_zip, 6371*acos(sin('$lat')*sin(zc_lat*pi()/180)+cos('$lat')*cos(zc_lat*pi() /180)*cos('$lon'-zc_lon*pi()/180))/ AS distance FROM zipcode WHERE 6371*acos(sin('$lat')*sin(zc_lat*pi()/180)+cos('$lat')*cos(zc_lat*p i()/180)*cos('$lon'-zc_lon*pi()/180))<$radius *1.6093

Complicated Queries Now that we have a zip code database, we can figure out what toys to load for all kids who have been good and live within a given radius of some zip code Now that we have a zip code database, we can figure out what toys to load for all kids who have been good and live within a given radius of some zip code

SQL Injection Attacks The Grinch wants to stop Christmas from coming, and is attempting to delete Santa’s list. We have an SQL injection vulnerability in index.php This will select more ids than we intend to: I’ve tried to construct something that will drop a table, but have been unsuccessful so far…. A good page about SQL injection that I found is at:

Other Useful Features Encryption Encryption Full-Text search Full-Text search Conditionals Conditionals String functions String functions Spacial functions (GIS) Spacial functions (GIS) Precision Math Precision Math

Alternatives to SQL MemCache MemCache RRD RRD