Intro to Database Commands

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

CSC Web Programming. Introduction to SQL

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

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Unit 1 - Chapter 4,5

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

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Basic SQL. Basic SQL. Basic SQL

SQL Structured Query Language Introduction

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

Deepak Bhinde PGT Comp. Sc.

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

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

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

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

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

SQL Data Definition: Table Creation

Exact Numeric Data Types

SQL Data Definition Language

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

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

CSIE30600 Database Systems Basic SQL 2. Outline

BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E)

BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E)

Chapter 3 Introduction to relational databases and MySQL

SQL Introduction. CS 377: Database Systems

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

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

Slides by: Ms. Shree Jaswal

Provider: MySQLAB Web page:

EDB Postgres Hadoop Data Adapter Guide

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

ECE 650 Systems Programming & Engineering. Spring 2018

Chapter 9: Working with MySQL

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

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

SQL: Data Definition Language

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

SQL. Rodrigo García Carmona Universidad San Pablo-CEU Escuela Politécnica Superior

MTAT Introduction to Databases

Reminders - IMPORTANT:

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

Database and MySQL Temasek Polytechnic

Databases and SQL programming overview

Applied Databases. Sebastian Maneth. Lecture 7 Simple SQL Queries. University of Edinburgh - February 1 st, 2016

How to use SQL to create a database

User's Guide c-treeace SQL Explorer

Structured Query Language (SQL)

Implementing Table Operations Using Structured Query Language (SQL) Using Multiple Operations. SQL: Structured Query Language

user specifies what is wanted, not how to find it

tablename ORDER BY column ASC tablename ORDER BY column DESC sortingorder, } The WHERE and ORDER BY clauses can be combined in one

SQL DATA DEFINITION LANGUAGE

Applied Databases. Sebastian Maneth. Lecture 7 Simple SQL Queries. University of Edinburgh - February 6 st, 2017

SQL Commands & Mongo DB New Syllabus

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

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

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

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

SQL (Structured Query Language)

MySQL by Examples for Beginners

Setup of PostgreSQL, pgadmin and importing data. CS3200 Database design (sp18 s2) Version 2/9/2018

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

After completing this unit, you should be able to: Define the terms

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

IBM Database Conversion Workbench 3.5

Chapter-14 SQL COMMANDS

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

Part II Relational Databases Data as Tables

30. Structured Query Language (SQL)

Principles of Data Management

Parameterizing an iway Data Quality Server SQL Statement From an Input File or the Command Line

SQL Part 2. Kathleen Durant PhD Northeastern University CS3200 Lesson 6

EXAMINATIONS 2009 END-YEAR. COMP302 / SWEN304 Database Systems / Database System Engineering. Appendix

SQL BASICS WITH THE SMALLBANKDB STEFANO GRAZIOLI & MIKE MORRIS

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

Lecture 3 SQL. Shuigeng Zhou. September 23, 2008 School of Computer Science Fudan University

Chapter 3: Introduction to SQL

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

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

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

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

The SQL database language Parts of the SQL language

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

SURVEYOR/400. Users Guide. Copyright , LINOMA SOFTWARE LINOMA SOFTWARE is a division of LINOMA GROUP, Inc.

Lecture 5. Monday, September 15, 2014

SQL DATA DEFINITION LANGUAGE

SQL DATA DEFINITION LANGUAGE

QQ Group

Table Joins and Indexes in SQL

Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations Views Modification of the Database Data Definition

Management Reports Centre. User Guide. Emmanuel Amekuedi

SQL stands for Structured Query Language. SQL lets you access and manipulate databases

Lab # 4 Hands-On. DDL and DML Advance SQL Statements Institute of Computer Science, University of Tartu, Estonia

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

SQL. Lecture 4 SQL. Basic Structure. The select Clause. The select Clause (Cont.) The select Clause (Cont.) Basic Structure.

DATABASE TECHNOLOGY. Spring An introduction to database systems

Model Question Paper. Credits: 4 Marks: 140

Basic SQL. Dr Paolo Guagliardo. University of Edinburgh. Fall 2016

Transcription:

Intro to Database Commands

SQL (Structured Query Language) Allows you to create and delete tables Four basic commands select insert delete update You can use clauses to narrow/format your result sets or the records to modify

Creating Tables in a Database The command is as follows: CREATE TABLE movies( movie_num INTEGER, title CHAR(35) NOT NULL, actor INTEGER, year INTEGER ); This will create a table named movies with four columns, one of which will not support empty values (title) To delete this table the syntax is: DROP TABLE movies;

SQL Data Types Numeric Integers of various ranges: INTEGER (or INT), SMALLINT Real numbers of various precision: FLOAT, REAL, DOUBLE PRECISION, or the preferred: NUMERIC(p, s) where p = precision and s = scale (the number before and after the decimal) Character Strings Fixed length n: CHAR(n) or CHARACTER(n) Variable length of maximum n: VARCHAR(n) or CHAR VARYING(n) (default n =1) Date/Time - Date: contains only date info - N.B. PostGreSQL on the opentech server takes the Date as a string with the format of YYYY-MM-DD (e.g. 1989-10-23 ) - Time: contains only time info - Timestamp: contains both date and time information For detailed list see: http://www.postgresql.org/docs/7.3/interactive/datatype.html#datatype-float

SQL Data Qualifiers When you set up (i.e. CREATE) your tables you can set conditions for the fields in your records: PRIMARY KEY Makes the field in the record required and has the extra condition that the field must be unique in the table (only one record can have a certain value for the field) A DB table can only have one PRIMARY KEY NOT NULL Makes the field in the record mandatory (i.e. you cannot create records that do not have the field present) Different from PRIMARY KEY as multiple records can have the same value

SELECT SQL Statements SELECT statements work on existing records in a database Example: SELECT * FROM movies; For readability, should be more specific SELECT movie_num, title, actor, year FROM movies;

SELECT Statement Qualifiers You can narrow down your results by using various qualifiers WHERE column_name LOGIC_OPERATOR value The logic operators are the same as programming: <>, >=, =, <=, <, > N.B. single equal sign is the logic comparator And <> to check inequaliy (not the!=)

SELECT Statement Sorting You can sort your select result set with the ORDER BY clause ORDER BY column_name directional_qualifier The directional qualifier can be: ASC for ascending (default) DESC for descending

SELECT Statement Aliases For joining multiple tables in a single query it is sometimes easier (clearer) to give table names aliases: E.g.: SELECT movies.title, movies.year, actors.name FROM movies, actors WHERE movies.actor = actors.id ORDER BY movies.year ASC Could be: SELECT m.title, m.year, a.name FROM movies m, actors a WHERE m.actor = a.id ORDER BY m.year ASC In this case, a and m become aliases for the tables actors and movies respectively

Insert SQL Statements INSERT statements create records in a database Example: INSERT INTO movies VALUES(21, 'Casino Royale', 'Daniel Craig', 2006); Should be more specific INSERT INTO movies (movie_num, title, actor, year) VALUES(21, 'Casino Royale', 'Daniel Craig', 2006); N.B. strings are delimited by single quotes ('), not double quotes

Update SQL Statements UPDATE statements modify existing records in a database, and uses same clauses as SELECT statements Example: UPDATE movies SET year = 2003 WHERE title = 'Die Another Day'; Be aware you can update multiple records with one UPDATE command (if not careful)

DELETE SQL Statements DELETE statements remove existing records in a database, and uses same clauses as SELECT statements Example: DELETE FROM movies WHERE title = 'Die Another Day' Be aware you can DELETE multiple records with one DELETE command (if not careful) e.g. DELETE FROM movies; Removes all records from the movies table (but does not remove the table, you must use a DROP statement to delete)

SQL Comments SQL scripts support comments in two formats: Single line comments start with -- (two hyphens) -- this is a single line SQL comment Multi-line comments are the same as c-style: /* */ This is a multi-line SQL comment

Creating a New PostGreSQL DB For this course (WEBD2201) you will not need to create your own database (it is created for you) From the command line the command following command was executed: createdb userid_db Where userid is your user id (i.e. pufferd) When the database was successfully created the system displays a CREATE DATABASE message

Creating a New PostGreSQL DB Now that a database exists for you, and you have been given ownership, when you log onto the server with PuTTy, if you type: psql userid_db You will be prompted for your password After entering it, the system will take you into your database, where you can perform SQL commands (prompt will be => instead of the # or $ OS prompt) To change your password at the sql prompt => type: ALTER USER your_user_id WITH ENCRYPTED PASSWORD 'your_new_password';

Removing a Database If you create a database (misnamed or unwanted), to remove it type: dropdb nogood_db where nogood_db is the unused/unwanted database NOTE: do NOT execute this command on your lastnamefirstinitial_db database, you are able to remove your db, but your user does not have permission to create a new one

Running SQL Script from the command line For large scale applications, it is advantageous to set up build scripts that drop/create databases as required Easier to implement changes than doing everything manually At the command prompt type: psql d userid_db f script_file.sql Where userid_db is the database to be modified and script_file.sql is the SQL file with the commands to be executed (must be co-located in the current directory, or else fully qualify the address)

PostGreSQL Meta-commands There are several commands that are defined for PostGreSQL that allow users some short-cuts to administer databases or runs scripts For a complete list see: http://www.postgresql.org/docs/9.2/static/app-psql.html Some common and useful ones that can be executed, at the PostGreSQL => prompt type: \q will quit or exit the database, takes you back to the OS \d will dump (quickly preview) the database s content \d table_name will dump a specific table s info \i db_script.sql allows you to run a script from outside the db prompt NOTE: this will default go to the directory the user was in when they connected to the database, to use a file from a different directory the file path must be fully qualified: \i /var/www/users/webd2201/user_id/sql/db_script.sql