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

Similar documents
MySQL Creating a Database Lecture 3

Basic SQL. Basic SQL. Basic SQL

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

CMPT 354: Database System I. Lecture 2. Relational Model

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

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

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

SQL: Data Definition Language

Exact Numeric Data Types

CS2300: File Structures and Introduction to Database Systems

NCSS: Databases and SQL

Exploring Microsoft Office Access Chapter 2: Relational Databases and Multi-Table Queries

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Databases CSCI 201 Principles of Software Development

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

LAB 4.1 Relational Operators and the if Statement

Database Management Systems,

More MySQL ELEVEN Walkthrough examples Walkthrough 1: Bulk loading SESSION

SQL CSCI 201 Principles of Software Development

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

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

Operating systems fundamentals - B07

Read this before starting!

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

INFORMATION TECHNOLOGY NOTES

Islamic University of Gaza Faculty of Engineering Department of Computer Engineering Fall 2011 ECOM 4113: Database System Lab Eng.

user specifies what is wanted, not how to find it

MySQL: an application

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture


Slides by: Ms. Shree Jaswal

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

CS143: Relational Model

Deepak Bhinde PGT Comp. Sc.

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

Simple Quesries in SQL & Table Creation and Data Manipulation

Networks and Web for Health Informatics (HINF 6220)

Lab # 4. Data Definition Language (DDL)

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

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

Module 3 MySQL Database. Database Management System

SQL Data Definition Language

Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

VARIABLES AND TYPES CITS1001

Unit 7: Database Development (Basic)

HKTA TANG HIN MEMORIAL SECONDARY SCHOOL SECONDARY 3 COMPUTER LITERACY. Name: ( ) Class: Date: Databases and Microsoft Access

Advanced SQL. Nov 21, CS445 Pacific University 1

Introduction to Databases. MySQL Syntax Guide: Day 1

SQL Structured Query Language Introduction

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

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

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

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

Today Learning outcomes LO2

SQL Data Definition and Data Manipulation Languages (DDL and DML)

2.9 Table Creation. CREATE TABLE TableName ( AttrName AttrType, AttrName AttrType,... )

SQL Commands & Mongo DB New Syllabus

MySQL. A practical introduction to database design

Oracle 1Z MySQL 5.6 Developer.

SQL Introduction. CS 377: Database Systems

Simple SQL. Peter Y. Wu. Dept of Computer and Information Systems Robert Morris University

Intro to Database Commands

ASSIGNMENT NO 2. Objectives: To understand and demonstrate DDL statements on various SQL objects

Relational Database Management Systems for Epidemiologists: SQL Part II

Relational databases and SQL

COMP519: Web Programming Autumn 2015

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

CS 327E Lecture 5. Shirley Cohen. February 8, 2016

Information Systems Engineering. SQL Structured Query Language DDL Data Definition (sub)language

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

Unit 1 - Chapter 4,5

The Relational Model. Suan Lee

Lecture 07. Spring 2018 Borough of Manhattan Community College

Databases and MySQL: The Basics

Full file at

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

FIT 100 More Microsoft Access and Relational Databases Creating Views with SQL

Access Module 2: Building and Using Queries

Exploring Popular SQL Implementations. Query the database to retrieve the data stored therein.

1Z MySQL 5 Database Administrator Certified Professional Exam, Part II Exam.

Access Intermediate

3344 Database Lab. 1. Overview. 2. Lab Requirements. In this lab, you will:

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

SQL: The Query Language Part 1. Relational Query Languages

Records Maintenance. After importing data, you should verify and possibly format it to customize its fields.

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

CS 250 SQL and SQLite. Benjamin Dicken

Chapter 13 : Informatics Practices. Class XI ( As per CBSE Board) SQL Commands. New Syllabus Visit : python.mykvs.in for regular updates

ITI Introduction to Computing II

System Pages (Setup Admins Only)

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

CS448 Designing and Implementing a Mini Relational DBMS

SQL DATA DEFINITION LANGUAGE

DB Creation with SQL DDL

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

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

Insertions, Deletions, and Updates

A Flat file database. Problems with a flat file database (data redundancy)

Transcription:

Chapter 6 Introduction to SQL 6.1 What is a SQL? When would I use it? SQL stands for Structured Query Language. It is a language used mainly for talking to database servers. It s main feature divisions are broken into data description, data access, and user privilege. It is used as the front end language for many applications such as mysql, postgresql, and oracle. You would use a database, and subsequently SQL whenever you have a large dataset that needs to be accessed easily very frequently or even simultaneously by multiple clients. Examples of when you would use a database could consist of generating reports, data storage, and more recently as backends to websites. SQL is the standard for relational databases. A relational database stores information in tables made of many rows and columns where a row represents a record and a column represents an attribute. Databases typically provide ways to add, delete, and search records in a table. Typically one column of a table will be considered a primary key. This primary key should be unique to each record and allows for faster searches on this column. Example of a Relational Database Students Table FName LName StudentID College Year Justin Ennen 83746387 Science Senior Dan Bass 83635563 Management Junior Michael Chazoule 83554752 Mathematics Junior Above you can see an example of a relational database which holds data about various students at a college, their names, year of school, etc in a table called Students. This will provide a good example for choosing a primary key. At first you might think it would be a good idea to choose the name of the student as the primary key, however there can be multiple students with similar or even the same name. In this case, it is best by far to choose the student ID as the primary key, since every student has their own unique ID that belongs only to them.

6.2 SQL Language The first thing to take a look at is the various commonly used data types available to us in SQL. These are outlined in the table below: Variable CHARACTER(n) INTEGER(n), SMALLINT, BIGINT Description string with a fixed length of n integers with various precision, precision n, 5, and 10 respectively BOOLEAN true or false valued variable FLOAT decimal numerical value, with a mantissa precision of 16 TIME stores hours minutes and seconds In addition to the variable types, you should know the various operations you can perform. We will first look at the operations that allow you to edit within a table, and then later briefly look at the operations that allow you to navigate the whole database. The main operations or statements you will use within a table that we are concerned with are SELECT, INSERT, UPDATE, and DELETE. Statement Description Example SELECT* UPDATE* DELETE* INSERT INTO used to retrieve records from the database used to change existing records removes a record or records from a table used to add an entry to a table SELECT column, column2 FROM table UPDATE table SET column = value, column2 = value2, etc DELETE FROM table INSERT INTO table (column1, column2, column3, etc) VALUES (value1, value2, value3, etc); *It is important to note that these statements do NOT require a where clause to check values, if there is no where clause, then the operation will be applied to the entire table As well as each of these operations within a table, there are operations that allow us to manage multiple tables within a database or even multiple databases. These operations are each fairly straight forward and obvious.

Statement SHOW DATABASES; SHOW TABLES IN database; USE database; Description lists all accessible databases lists all accessible tables in database makes database the currently active database, operations will assume this database as the basis for other statements DESCRIBE table; CREATE TABLE tablename ( column1 variabletype, column2 variabletype, column3 variabletype, etc ); 6.1 Creating an Example Database lists all attributes of table creates a table with the name tablename and creates columns or attributes to hold each of the specified variables with the given names column1, column2, etc. We will next go through the steps of creating the example database about the students we have shown previously. You are encouraged to follow along with these steps to create the database as well. If you would like to do this, one of the easiest, but certainly not the only, SQL programs to set up is MySQL, an open source database application provided by Oracle. You may use any of the MySQL Community Server applications to follow along or use another program of your own choosing as SQL is fairly standardized across all platforms and database applications. The very first thing we need to do is to create our table. We want it to have columns for the first name, last name, student ID, college, and what their current year is at the university. We also want to specify the student ID as the primary key. We also want to make sure that there is always a supplied name for the student, that is the students name cannot be NULL, or empty. To do these two things we will use constraints, which are descriptors added after the variable type. After we have created the table, we want to make sure there were no mistakes, so we will ask to have our newly created table described to us after we have created it. Therefore our first command will look like this: mysql> CREATE TABLE Students

( FName CHARACTER(100) NOT NULL, LName CHARACTER(100) NOT NULL, StudentID INTEGER PRIMARY KEY, College CHARACTER(100), Year CHARACTER(100) ); mysql> DESCRIBE Students; Field Type Null Key Default Extra FName char(100) YES NULL LName char(100) YES NULL StudentID int(11) YES NULL College char(100) YES NULL Year char(100) YES NULL 5 rows in set (0.07 sec) After running the describe command, you should now see a table describing the fields of your newly created table saying which values can be null, and which fields are considered any type of key. We have now successfully created our first table! Now we just have to populate it with data. We will do the first together, and leave the last two up to you as exercises. Our first student, Justin Ennen, is in the college of science, has a student ID of 83746387 and is a senior. Therefore, our insert statement will look like the following: mysql> INSERT INTO Students VALUES ( Justin, Ennen, 83746387, Science, Senior ); mysql> SELECT * FROM Students; + + + + FName LName StudentID College Year + + + + Justin Ennen 83746387 Science Senior + + + + 1 row in set (0.00 sec) Below our insert statement you can see we used a select statement, with an asterisk as the column name. The simplest way to think about this is think of the asterisk as meaning everything. So we are saying select everything, so that we can see the changes we made to the table by adding our new row. For the sake of the rest of the example we will assume you have added the other two entries to the table. We have just received a request from the registrar at our university for the student ID of Dan Bass. This information is thankfully already in our database, all we have to do is retrieve it from the table! We can do this with a command like the following:

mysql> SELECT StudentID FROM Students WHERE FName LIKE Dan AND LName LIKE Bass ; StudentID 83635563 1 row in set (0.06 sec) If we typed this correctly, and the entry was in our database, this should have returned the student ID we needed. This operation used few new things. It used the LIKE operator which is similar to using the = operator for numbers, but with strings, and it used the AND keyword to allow us to use two criteria for the select statement. You can apply these same concepts to the update, and delete statements as well. Chapter Summary Questions and Exercises 1. Create a statement to insert James into our student database using the INSERT statement. His ID is 6857938 and he is a sophomore. He is in the same college as you are. 2. Use what you have learned about forming SQL statements to now delete James from the database using the DELETE statement. This will work very similarly to the SELECT statement. 3. Now try using the UPDATE statement to change Dan s Student ID to the number 1000000. 4. Take the time to find an SQL connector for your favorite language online. A connector is the library you will include that allows you to easily make connections to and send statements to your SQL database from inside one of your programs.