CS 250 SQL and SQLite. Benjamin Dicken

Similar documents
CS 250 Databases and DBMSs. Benjamin Dicken

CS2300: File Structures and Introduction to Database Systems

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

INFORMATION TECHNOLOGY NOTES

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

user specifies what is wanted, not how to find it

NCSS: Databases and SQL

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

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

Database Management Systems,

CPS 510 Data Base I. There are 3 forms of database descriptions the ANSI/SPARK, 1975 and so on

CGS 3066: Spring 2017 SQL Reference

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Today's Party. Example Database. Faloutsos/Pavlo CMU /615

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

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

CPS510 Database System Design Primitive SYSTEM STRUCTURE

SQL Commands & Mongo DB New Syllabus

Chapter-14 SQL COMMANDS

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

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

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Basic SQL. Basic SQL. Basic SQL

Lecture 1. Monday, August 25, 2014

Unit 7: Database Development (Basic)

SQL DATA DEFINITION LANGUAGE

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

Simple Quesries in SQL & Table Creation and Data Manipulation

SQL: Data Definition Language

SQL Constraints and Triggers. Week 11

Databases and MySQL: The Basics

CSE 344: Section 1 Git Setup for HW Introduction to SQLite

Unit 1 - Chapter 4,5

Operating systems fundamentals - B07

Database Systems Fundamentals

CS W Introduction to Databases Spring Computer Science Department Columbia University

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

CS 2316 Homework 9b GT Thrift Shop Due: Wednesday, April 20 th, before 11:55 PM Out of 100 points. Premise

CSE 344: Section 1 Git Setup for HW Introduction to SQLite. September 28, 2017

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

Data Base Lab. The Microsoft SQL Server Management Studio Part-3- By :Eng.Alaa I.Haniy.

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

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

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #4: SQL---Part 2

Introduction to SQL Part 2 by Michael Hahsler Based on slides for CS145 Introduction to Databases (Stanford)

Exact Numeric Data Types

CSE 414 Introduction to Data Management. Section 2: Introduction to SQLite

COMP 430 Intro. to Database Systems

Introduction to Databases, Fall 2005 IT University of Copenhagen. Lecture 2: Relations and SQL. September 5, Lecturer: Rasmus Pagh

Storing and Indexing Expressions in Database Systems

The Mother of All Query Languages: SQL in Modern Times

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

DS Introduction to SQL Part 2 Multi-table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Lecture 8. Database vs. Files SQL (I) Introduction to SQL database management systems (DBMS)

PART I SQLAlchemy Core

EE221 Databases Practicals Manual

Question Bank IT(402) Unit Database 1. What is database? Ans: A database is a collection of information that is organized so that it can be easily

Carnegie Mellon University in Qatar Spring Problem Set 4. Out: April 01, 2018

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

CSC 453 Database Technologies. Tanu Malik DePaul University

Index. Bitmap Heap Scan, 156 Bitmap Index Scan, 156. Rahul Batra 2018 R. Batra, SQL Primer,

CS108 Lecture 19: The Python DBAPI

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

CS 327E Lecture 2. Shirley Cohen. January 27, 2016

Integrity constraints, relationships. CS634 Lecture 2

MySQL Creating a Database Lecture 3

SQL DATA DEFINITION LANGUAGE

Unit 11: Database Systems CS 101, Fall 2018

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Oracle Create Table Foreign Key On Delete No

1) Introduction to SQL

SQL Constraints and Triggers

Fundamentals: Expressions and Assignment

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

SQL DATA DEFINITION LANGUAGE

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

SQL Simple Queries. Chapter 3.1 V3.01. Napier University

Traffic violations revisited

CSE 414 Midterm. April 28, Name: Question Points Score Total 101. Do not open the test until instructed to do so.

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Not-So-Mini-Lecture 6. Modules & Scripts

Downloaded from

Databases 1. Defining Tables, Constraints

In this lab, you will learn more about selection statements. You will get familiar to

ASSIGNMENT NO Computer System with Open Source Operating System. 2. Mysql

Summary Modifications. Database Construction (and Usage) Explicit attribute lists. Insertions with queries. Quiz. Default values

CSC Web Programming. Introduction to SQL

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

Declarative Programming

Full file at

Using a DBMS. Shan-Hung Wu & DataLab CS, NTHU

Lecture 2: Introduction to SQL

CSE 344 APRIL 16 TH SEMI-STRUCTURED DATA

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

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

SQL: The Query Language Part 1. Relational Query Languages

DB2 MOCK TEST DB2 MOCK TEST I

MTAT Introduction to Databases

Review the syntax of. Create a new Schema CREATE TABLE INSERT SELECT UPDATE DELETE

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

Transcription:

CS 250 SQL and SQLite Benjamin Dicken

SQL SQL (Structured Query Language) is a programming language designed for managing schema and data held in a relational database management system (RDBMS)

SQLite SQLite is a relational database management system written in the C programming language. In contrast to many other database management systems, SQLite is not a client server database engine. Rather, it is embedded into the end program. We use SQL to insert, remove, and access the data in a SQLite database Specifically, we are using SQLite version 3 Installation instructions: tutorialspoint.com/sqlite/sqlite_installation.htm

SQLite SQLite is a C library that acts as a lightweight DBMS doesn t require a separate server process and allows accessing the database Uses a standard variant of the SQL query language Some applications use SQLite for internal data storage Since so lightweight, often prototype an application using SQLite and then port the code to a larger database such as PostgreSQL or Oracle

Learning SQL with SQLite

SQLite To start the SQLite 3 from bash: $ sqlite3 ex1 This starts up the sqlite3 program If a SQL database file with the name ex1 exists in the current directory, this will user that database If not, will create a new database named ex1

SQLite At this points, we are connected to the DB using the SQLite DBMS From here, the user must use the SQL programming language to Create tables Add data Remove data Query the tables Create relationships

SQL - create tables The first SQL command we will learn about is CREATE Use the CREATE command to create a new table The format is CREATE TABLE table_name( column1 datatype, column2 datatype,... columnn datatype );

SQL - create tables Each attribute (column) has a TYPE. SQLite supports many, many types, but we will focus on only a few in this class INT - integer numbers (like python int) FLOAT - floating-point numbers (like python float) TEXT - sequence of characters (like python str) BOOLEAN - true (1) or false (0) (like python bool)

SQL - create tables CREATE TABLE character( name TEXT, cid INT, pid INT); CREATE TABLE person( name TEXT, pid INT); CREATE TABLE movie( title TEXT, mid INT);

SQL - create tables CREATE TABLE character( name TEXT, description TEXT, is_good BOOLEAN, appearances INT, cid INT);

SQL - create tables SQL CREATE resources: sqlite.org/lang_createtable.html tutorialspoint.com/sqlite/sqlite_create_table.htm

SQL - inserting The next SQL command we will learn about is INSERT Use the INSERT command to add rows to an existing table The format is INSERT INTO TABLE_NAME (column1, column2, columnn) VALUES (value1, value2, valuen);

SQL - inserting INSERT INTO character (name, cid, pid) VALUES ( Thor, 1, 4); INSERT INTO character (name, cid, pid) VALUES ( Batman, 2, 7); INSERT INTO character (name, cid, pid) VALUES (Superman, 3, 1);

SQL - inserting INSERT INTO character (name, description, is_good, appearances, cid) VALUES ( Batman, Rich Hero Dude, 1, 137, 2); INSERT INTO character VALUES ( Batman, Rich Hero Dude, 1, 137, 2);

SQL - inserting SQL INSERT resources: sqlite.org/lang_insert.html tutorialspoint.com/sqlite/sqlite_insert_query.htm

SQL - deleting The next SQL command we will learn about is DELETE Use the DELETE command to delete row(s) from an existing table The format is DELETE FROM table_name WHERE condition; Will delete all rows that match the condition

SQL - deleting DELETE FROM character where cid = 1; DELETE FROM movie where title = Batman Begins ; DELETE FROM person where name = Chris Nolan ;

SQL - deleting SQL DELETE resources: sqlite.org/lang_delete.html tutorialspoint.com/sqlite/sqlite_delete_query.htm

SQL - select The next SQL command we will learn about is SELECT Use the SELECT command to extract information from tables in the DB The format is SELECT column1, column2,... columnn FROM table_name; Will return each of the specified columns from table_name

SQL - select SELECT name FROM person; SELECT cid, name FROM character; SELECT * FROM movie;

SQL - select The SELECT comment has an optional WHERE clause A condition is specified after the WHERE keyword Only rows in which the condition holds true will be returned SELECT column1, column2,... columnn FROM table_name WHERE condition;

SQL - select Multiple WHERE clauses can be specified with AND / OR SELECT column1, column2,... columnn FROM table_name WHERE condition1 AND condition2 AND conditionn; SELECT column1, column2,... columnn FROM table_name WHERE condition1 OR condition2 OR conditionn;

SQL - select SELECT name FROM person WHERE cid = 1; SELECT * FROM character WHERE description <> A Rich Dude OR name == Thor ; SELECT * FROM character WHERE description = something AND cid > 3;

SQL - select SQL SELECT resources: https://www.tutorialspoint.com/sqlite/sqlite_select_query.htm https://sqlite.org/lang_select.html

SQL - full example $ sqlite3 heromovies SQLite version 3.14.0 2016-07-26 15:17:14 Enter ".help" for usage hints. sqlite>

SQL - full example sqlite> CREATE TABLE character(name TEXT, description TEXT, is_good BOOLEAN, appearances INT, cid INT); sqlite> sqlite> INSERT INTO character (name, description, is_good, appearances, cid) VALUES ('Batman', 'Bat-like super hero', 1, 1284, 1); sqlite> sqlite> INSERT INTO character (name, description, is_good, appearances, cid) VALUES ('Thor', 'Hero from another planet', 1, 572, 2); sqlite> sqlite> INSERT INTO character (name, description, is_good, appearances, cid) VALUES ('Superman', 'Hero from another planet', 1, 1752, 3);

SQL - full example sqlite> SELECT * FROM character; Batman Bat-like super hero 1 1284 1 Thor Hero from another planet 1 572 2 Superman Hero from another planet 1 1752 3 sqlite>

SQL - full example sqlite> SELECT description, name, is_good FROM character; Bat-like super hero Batman 1 Hero from another planet Thor 1 Hero from another planet Superman 1 sqlite>

SQL - full example sqlite> SELECT name, description FROM character WHERE description = 'Hero from another planet'; Thor Hero from another planet Superman Hero from another planet sqlite> sqlite> SELECT name, description FROM character WHERE description <> 'Hero from another planet'; Batman Bat-like super hero sqlite>

SQL - full example sqlite> SELECT cid, name, description FROM character WHERE cid = 1 OR cid = 2; 1 Batman Bat-like super hero 2 Thor Hero from another planet sqlite> sqlite> SELECT cid, name, description FROM character WHERE cid = 1 AND cid = 2; sqlite>

SQL - full example $ sqlite3 ex1 SQLite version 3.8.5 2014-05-29 12:36:14 Enter ".help" for usage hints. sqlite> create table tbl1(one varchar(10), two smallint); sqlite> insert into tbl1 values('hello!',10); sqlite> insert into tbl1 values('goodbye', 20); sqlite> select * from tbl1; hello! 10 goodbye 20 sqlite>

SQL - specifying relationships We can create multiple tables in a single database cid, mid, and pid are supposed to reference IDs from other tables But there is nothing keeping us from putting invalid IDs in there! CREATE TABLE character( name TEXT, cid INT, pid INT); CREATE TABLE person( name TEXT, pid INT); CREATE TABLE movie( title TEXT, mid INT);

SQL - specifying relationships We should specify a PRIMARY KEY for each table A primary key specifies which column uniquely identifies each row Often this is an integer ID, as it is in this case CREATE TABLE character( name TEXT, cid INT PRIMARY KEY, pid INT); CREATE TABLE person( name TEXT, pid INT PRIMARY KEY); CREATE TABLE movie( title TEXT, mid INT PRIMARY KEY);

SQL - specifying relationships Relationships should be specified with a FOREIGN KEY Create a FOREIGN KEY from the ID in one table to a primary key in another table CREATE TABLE character( name TEXT, cid INT PRIMARY KEY, pid INT, FOREIGN KEY(pid) REFERENCES person(pid)); CREATE TABLE person( name TEXT, pid INT PRIMARY KEY); CREATE TABLE movie( title TEXT, mid INT PRIMARY KEY);

SQL - specifying relationships Use this command to make sure that FOREIGN KEY restrictions are enabled PRAGMA foreign_keys = ON;

In-class Exercise (picking up from previous lecture) Model Ford Motor Company s manufacturing database Ford manufactures cars at multiple locations in the US Flat Rock Assembly Plant 1, Michigan Chicago Assembly, Illinois Dearborn Truck, Michigan Kansas City Assembly, Missouri Ford manufactures many types of cars F-150, Mustang, Focus, Explorer, Flex,... Ford sells to many customers Dealerships, Companies, Gov t, Individuals,...

In-class Exercise (picking up from previous lecture) Tables: Facility Item Purchase Customer Relationships to model Facility <-> Item Item <-> Purchase Purchase <-> Customer

In-class Exercise (picking up from previous lecture) Define the schema with CREATE statements Make sure to specify the FOREIGN KEYS Insert a few rows into each table with INSERT

SQL and SQLite Reading Materials www.w3schools.com/sql www.tutorialspoint.com/sql