This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client

Similar documents
Using MySQL on the Winthrop Linux Systems

TUTORIAL FOR IMPORTING OTTAWA FIRE HYDRANT PARKING VIOLATION DATA INTO MYSQL

WHAT IS A DATABASE? There are at least six commonly known database types: flat, hierarchical, network, relational, dimensional, and object.

SQL. Often times, in order for us to build the most functional website we can, we depend on a database to store information.

TUTORIAL FOR IMPORTING OTTAWA FIRE HYDRANT PARKING VIOLATION DATA INTO MYSQL

Draft. Students Table. FName LName StudentID College Year. Justin Ennen Science Senior. Dan Bass Management Junior

Lab # 2 Hands-On. DDL Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia

Lecture 5. Monday, September 15, 2014

More MySQL ELEVEN Walkthrough examples Walkthrough 1: Bulk loading SESSION

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

Operating systems fundamentals - B07

MySQL: an application

Simple Quesries in SQL & Table Creation and Data Manipulation

SQL Data Definition Language: Create and Change the Database Ray Lockwood

Review. Objec,ves. Example Students Table. Database Overview 3/8/17. PostgreSQL DB Elas,csearch. Databases

Getting Started With Oracle

Chapter 3 Introduction to relational databases and MySQL

Unit 1 - Chapter 4,5

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Creating the Data Layer

Chapter 3. Introduction to relational databases and MySQL. 2010, Mike Murach & Associates, Inc. Murach's PHP and MySQL, C3

Databases (MariaDB/MySQL) CS401, Fall 2015

Poom Malakul Na Ayudhya

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

STOP DROWNING IN DATA. START MAKING SENSE! An Introduction To SQLite Databases. (Data for this tutorial at

MySQL by Examples for Beginners

Model Question Paper. Credits: 4 Marks: 140

MySQL Workshop. Scott D. Anderson

Data and Tables. Bok, Jong Soon

Exact Numeric Data Types

MySQL 5.0 Certification Study Guide

Provider: MySQLAB Web page:

The M in LAMP: MySQL CSCI 470: Web Science Keith Vertanen Copyright 2014

Basic SQL. Basic SQL. Basic SQL

ORACLE Reference. 2. Modify your start-up script file using Option 1 or 2 below, depending on which shell you run.

Relational databases and SQL

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

Chapter 9: MySQL for Server-Side Data Storage

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 2

Text Input and Conditionals

Module 3 MySQL Database. Database Management System

Lecture 5: SQL s Data Definition Language

Data Definition and Data Manipulation. Lecture 5: SQL s Data Definition Language CS1106/CS5021/CS6503 Introduction to Relational Databases

Lecture 07. Spring 2018 Borough of Manhattan Community College

Lab 1 Introduction to UNIX and C

SQL stands for Structured Query Language. SQL is the lingua franca

Intro to Database Commands

MIS2502: Data Analytics MySQL and SQL Workbench. Jing Gong

Full file at

sqoop Automatic database import Aaron Kimball Cloudera Inc. June 18, 2009

GlobAl EDITION. Database Concepts SEVENTH EDITION. David M. Kroenke David J. Auer

MySQL Introduction. Practical Course Integrative Bioinformatics. March 7, 2012

Mysql Tutorial Show Table Like Name Not >>>CLICK HERE<<<

Introduction to relational databases and MySQL

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

CSE 530A. Inheritance and Partitioning. Washington University Fall 2013

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

GEO 425: SPRING 2012 LAB 9: Introduction to Postgresql and SQL

Lab # 1. You will be using MySQL as a database management system during the labs. The goal of this first lab is to familiarize you with MySQL.

Lab 4: Tables and Constraints

In this exercise you will practice some more SQL queries. First let s practice queries on a single table.

HOW TO EXPORT BUYER NAMES & ADDRESSES FROM PAYPAL TO A CSV FILE

SQL functions fit into two broad categories: Data definition language Data manipulation language

CGS 3066: Spring 2017 SQL Reference

Documentation Accessibility. Access to Oracle Support. Supported Browsers

Data Manipulation Language (DML)

Overview of MySQL Structure and Syntax [2]

MIT AITI Python Software Development

Mastering phpmyadmiri 3.4 for

DATABASE SYSTEMS. Introduction to MySQL. Database System Course, 2016

QW5 -QWSQLWizard Overview

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Oracle 1Z MySQL 5.6 Developer.

Experimental Finance, IEOR. Mike Lipkin, Alexander Stanton

PostgreSQL Installation Guide

Lab 1 Introduction to UNIX and C

Careerarm.com. 1. What is MySQL? MySQL is an open source DBMS which is built, supported and distributed by MySQL AB (now acquired by Oracle)

MySQL and MariaDB. March, Introduction 3

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus

Lesson 13 Transcript: User-Defined Functions

Chapter 5. Exploring Navicat and Sequel Pro

Mysql Manual Error 1064 Insert Into Select >>>CLICK HERE<<<

CISC 181 Lab 2 (100 pts) Due: March 7 at midnight (This is a two-week lab)

This lecture. Basic Syntax

Week 11 ~ Chapter 8 MySQL Command Line. PHP and MySQL CIS 86 Mission College

MySQL Schema Review 101

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Oracle Database 10g Express

SIT772 Database and Information Retrieval

Introduction to MySQL /MariaDB and SQL Basics. Read Chapter 3!

CSC Web Programming. Introduction to SQL

Chapter 9: Working With Data

CS/CIS 249 SP18 - Intro to Information Security

Outline. Introduction to SQL. What happens when you run an SQL query? There are 6 possible clauses in a select statement. Tara Murphy and James Curran

Mysql interview questions and answers

Kaivos User Guide Getting a database account 2

How to use SQL to create a database

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

Transcription:

Lab 2.0 - MySQL CISC3140, Fall 2011 DUE: Oct. 6th (Part 1 only) Part 1 1. Getting started This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client host name: 146.245.250.215 Then start up mysql: unix-prompt$ mysql -u <username> -p Your username and password will be given out in class, use passwd to change your password A database for you that is the same as your username. After you have logged in to MySQL, you can select that database: mysql> use <username>; Then do the steps below to become familiar with SQL and the MySQL interactive environment. 2. Creating a table The SQL command for creating a table is CREATE TABLE. The simple syntax is: CREATE TABLE <tablename> ( <column-definitions> ); where <column-definition> includes the name of each column and its data type. You can define multiple columns, separated by commas. The numeric data types are: INTEGER, SMALLINT, DECMIAL, NUMERIC, FLOAT, REAL and DOUBLE PRECISION. The date and time data types are: DATETIME, DATE, TIMESTAMP, TIME and YEAR. The string data types are: CHAR, VARCHAR, BINARY, VARBINARY, BLOB, TEXT, ENUM and SET. For detailed descriptions of each type, see the MySQL online documentation. Here is an example CREATE TABLE statement: CREATE TABLE birthday (thename VARCHAR(30) NOT NULL PRIMARY KEY, themonth INT, theday INT, theyear INT); Type the command all on one line and hit enter at the end. Note that I capitalize the SQL keywords, for clarity. Note that year is a SQL data type, so I use the variable name theyear instead.

The command DESCRIBE <table-name> (also abbreviated DESC) will list the definition of a table, for example: DESC birthday. DO THIS: 1. In your database, create the birthday table, as above. 2. Use the DESC command to check that you created the table correctly. You should get something like this: mysql> DESC birthday; +----------+-------------+------+-----+---------+-------+ Field Type Null Key Default Extra +----------+-------------+------+-----+---------+-------+ thename varchar(30) PRI themonth int(11) YES NULL theday int(11) YES NULL theyear int(11) YES NULL +----------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) 3. Storing data in your table Now that you have a table, you should store some data in it. The SQL command for storing data in a table is INSERT. There are two simple ways to use this command. The first way is: INSERT INTO <table-name> SET <column-name> = <value>; You can insert data into multiple columns with a single statement by repeating <column-name> = <value>, separated by commas. The second way is: INSERT INTO <table-name> (<column-name>) VALUES (<value>); Again, you can insert data into multiple columns with a single statement by repeating <column-name> and <value>, separated by commas. Here is an example INSERT statement using the first method: INSERT INTO birthday SET thename= Alex, themonth= 5, theday= 9, theyear= 1995 ; and another example using the second method: ( Alex, 5, 9, 1995 );

DO THIS: 1. Insert yourself into your birthday table using the first method. 2. Insert yourself into your birthday table using the second method. The first way should work fine. The second way should give you an error message ( Duplicate entry ) because you defined your table such that the thename field is the PRIMARY KEY. This means that you cannot have multiple entries (i.e., records or rows) in your table that have the same value for the primary key (i.e., the thename field). 4. Selecting information from a table Now that you ve put some data in your table, you probably want to be able to see what is there. The SQL command for displaying data in a table is SELECT. This command is extremely useful and has a large number of ways it can be used. We ll show a couple of the simplest ways here. The very simplest way is to display everything in the table. This syntax is: For example: SELECT * FROM <table-name>; SELECT * FROM birthday; The second way is to display only some of the columns (fields) in the table. That syntax is: SELECT <column-name> FROM <table-name>; Note that you can select more than one column, by listing multiple <column-name>s, separated by commas. For example: or SELECT thename FROM birthday; SELECT thename,theyear FROM birthday; The third way is to display only some of the rows (entries) in the table. That syntax involves using a where clause to list conditions under which you would like to list rows. For example, you could decide to list only the rows of people in your birthday table who have a birthday in March. That statement would be: SELECT thename FROM birthday WHERE themonth=3;

DO THIS: 1. Select all the columns and rows in your birthday table. 2. Select only the thename column, for all rows in your birthday table. 3. Select all the columns in your birthday table for those rows with a birth month in March. 4. Select only the thename column in your birthday table for those rows with a birth month in March. AND THEN DO THIS: 1. Create entries in your birthday table for everyone in your family. 2. Now repeat the SELECT statements listed above. 3. Try selecting different months or days or years. 5. Using external files for input You can imagine that it would be quite tedious to enter a lot of data into MySQL tables by manually typing INSERT commands a million times. There are several ways to input data from a file, without having to type everything manually. The simplest way is to create a PLAIN TEXT file (using a text editor like Notepad, NOT a word processor like Word) and fill it with INSERT commands. Then at the MySQL command prompt, you can execute that file using the \. command this is the general command for executing a MySQL script. Typically, MySQL script files are given the extension.sql. For example, suppose I have a text file named bday.sql that contains the following: ( Ann, 1, 2, 2000 ); ( Bev, 3, 4, 2001 ); ( Cyd, 5, 6, 2002 ); Then, I can execute all these commands at once by doing this: mysql> \. bday.sql You can also use the LOAD DATA command to read entries from a formatted file, such as a comma-separated values ( csv ) file exported from Excel. The syntax is: LOAD DATA INFILE <filename> INTO TABLE <table-name> Check the online MySQL manual for more details.

6. Writing output to external files If you are going to perform any analysis of data stored in your database, it is useful to be able to write the output of a query (i.e., SELECT) statement (or any other output, for that matter, such as the output of a DESC command) to a file. This is done using the \T ( tee ) command at the MySQL prompt, as follows: mysql> \T myfile.dat After you enter the \T command, everything that is displayed will be output to the file named myfile.dat. You turn off this output by using the \t ( notee ) command: mysql> \t 7. Getting help There are two recommended places to get help with mysql. Inside mysql, you can get help with the available interactive commands by typing \? or \h at the mysql> command prompt. This will give you a list of the mysql commands. Note that it will NOT give you a list of SQL commands. To get help with SQL commands, the best place to look is the on-line manual on the MySQL web site: http://dev.mysql.com/ 8. More advanced things to do When you complete these examples, you may want to explore more advanced features of MySQL. 1. Try listing everyone in order of the year they were born: SELECT * FROM birthday ORDER BY theyear; 2. Try counting the number of people born in each month: SELECT themonth, count(*) FROM birthday GROUP BY the month ORDER BY the month; 3. Try a join : Create a second table in your database called favorite that contains two columns: thename as a primary key and color as a text field that will contain the favorite color of the person whose name is thename. Insert an entry in this second table for each person you have entered in your birthday table. Now join the two tables: SELECT color FROM birthday,favorite WHERE birthday.thename=favorite.thename AND birthday.themonth=5;

Part 2 1. Creating the users table Design a table that will contain the information about users of your project web-based system. Think about what information you want to keep (like name ). For example: password? preferences (e.g., favorite food)? Create the table in your MySQL database. 2. Storing data in your table Now that you have created your users table, write a MySQL script that you can use to store data in the table (using the INSERT command). Generate the script and run it. 3. Selecting information from a table Now that you ve put some data in your table, you probably want to be able to see what is there. Generate a script that will let you display some of the fields in your users table (using the SELECT command). Create multiple scripts to let you display different combinations of data. For example, one script might display only user names. Another script might display user names and users favorite foods. 4. Editing information in your table At some point, you may need to edit fields that are in your table. For example, a user named Suzanne may decide that her favorite food is really sushi instead of ice cream. The MySQL command to edit the data in a table is UPDATE. You need to specify the name of the table you are editing (e.g., users), the name of the column you want to edit and a where clause which indicates which rows you want to edit. Without the where clause, all the rows in the table will be edited! Here is an example: UPDATE users SET food="sushi" WHERE username="suzanne"; You can try this from the command line, but also write a script to do this. 5. Adding information to your table At some point, you will need to add rows to the table, for example, when new users register. You can use the same INSERT command that you used above to do this. Write a script that lets you add a new user to the table.

6. Removing information from your table At some point, you may want to delete rows from the table. For example, a user may abuse their privileges and you want to deny them access to the system. The MySQL command to delete rows in a table is DELETE. You need to specify the name of the table from which to delete rows and a where clause specifying which row to delete. The default (without the where clause) is to delete all the rows in the table (and you probably don t want that). Try this from the command line, but also write a script to do it.