trysql slides

152

Upload: javier-alejandro

Post on 16-Dec-2015

11 views

Category:

Documents


1 download

DESCRIPTION

Diapositivas MySQL

TRANSCRIPT

  • Who is this course designed for?This course will teach the basics of how to read, write, and program in the database language known as SQL.

  • Try SQLSection 1: Understanding Databases

  • The StoryWhere to begin?

    We want to see a movie at Gatsby Theaters, but were only interested in seeing a comedy.

    How can we retrieve a listing of all movies shown at Gatsby Theaters?

  • Consulting a newspaperFinding a list of movies

    Gatsby Theaters Shows Current Movies !Metropolis Sci-fi 153 minutes Nosferatu Horror 94 minutes The Kid Comedy 68 minutes The Gold RushAdventure 95 minutes

  • Reviewing a websiteFinding a list of showtimes

    Gatsby Theaters Shows Current Movies !Metropolis Sci-fi 153 minutes Nosferatu Horror 94 minutes The Kid Comedy 68 minutes The Gold RushAdventure 95 minutes

    Movie listings from the web Movie listings from a newspaper

  • Why is the website updating?Its updating because its pulling this information from a database.

    Requesting the latest showtimes from a website

    Databases collect and organize data to allow for easy retrieval.

  • Databases can be used by lots of devicesInformation from a database can be used by many dierent services.

  • Gatsby Theatersdatabase name

    movie info showtime info

    concessions info promotions info

    Here is the Gatsby Theaters database.

    What does this database look like?

  • Gatsby Theaters

    Movies

    PromotionsConcessions

    Showtimes

    showtime info

    promotions info

    this database contains 4 tables

    movie info

    concessions info

    Introducing database tablesThe data is organized and stored into tables, which hold the data so it is available to be retrieved.

  • Gatsby Theaters

    Movies

    PromotionsConcessions

    Showtimes

    showtime info

    promotions info

    movie info

    concessions info

    Retrieving movie informationSo, in order to look up information on movies, we would need to go into the ________________ database and look inside the ____________ table.

  • Gatsby Theaters

    Gatsby Theaters Movies

    Movies

    PromotionsConcessions

    Showtimes

    showtime info

    promotions info

    movie info

    concessions info

    So, in order to look up information on movies, we would need to go into the ________________ database and look inside the ____________ table.

    Retrieving movie information

  • Movies

    What is inside of a table?Inside the Movies table is information about the movies in a cell, or a eld of data.

    Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    Tables are also organized into rows

    id title genre duration

    Rows

    A row represents an entire data record within each table.

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    Tables have columns with descriptive names

    id title genre durationColumnsEach column has a name describing the kind of data found in that column.

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    Often tables will have a unique identier

    id title genre durationUnique identifier

    Sometimes the unique identifier is labeled as the primary key.

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    The film title being searched for is Nosferatu.

    First step: Moving through the columnsThe columns are the rst place to look inside of the table.

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    First step: Moving through the columns

    id title genre duration

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    The title column contains the matching record.

    But wait, the next step

    First step: Moving through the columns

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    Second step: Moving through the rows

    id title genre duration

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    Reaching the nal destination for the data

    id title genre duration

    The destination of Nosferatu has now been reached!

    Most databases have fewer columns than rows, making it easier to search columns rst.

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    The data search path

    id title genre duration

    If the entire table search path is visualized, it would look something like this:

  • Try SQLSection 2: Introducing SQL

  • How do we speak to the database?SQL is a programming language that allows interaction with databases.

    SQL Code

    Result

  • Using SQL to work with the database

    SQL Code

    Result

    First, SQL code is written.

    SQL is written as statements that ask the database to perform many things.

  • The database receives the SQL code and runs it.

    SQL Code

    Result

    Using SQL to work with the databaseSQL is written as statements that ask the database to perform many things.

  • The data is shown at its final destination.

    SQL Code

    Result

    Using SQL to work with the databaseSQL is written as statements that ask the database to perform many things.

  • Back to the original exampleWhat would the browser do to get showtimes?

    Server

    A DatabaseSQL Code

    Request

    Sends SQL

    Result

    Web Page

  • Now, lets see how to use SQLHere are some things SQL will help solve.

    Retrieve a list of all movies with id, title, genre, and duration of the movie. Retrieve just the title of the movie with id of 2. Retrieve all movies with the title of The Kid.

    Retrieve a list of just the titles of all movies shown at the Gatsby Theaters.

    So now, look back at the first issue

    But as always, one answer can lead to more questions

  • Retrieving data from our database

    The column or columns to be searched

    The table the data is in

    SELECT Recipe

    All SQL statements must end in a semicolonSELECT _______FROM _________;

    A SELECT statement is needed to get movie information.

  • Using our SELECT recipeRetrieving a list of all movie titles using the SELECT statement

    Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    By choosing to select a single column, the result will contain only this columns data from all the rows.

    Table Movies

    SELECT titleFROM movies;

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    SELECT titleFROM movies;

    The database will return these values.

    Metropolis

    Nosferatu

    The Kid

    The Gold Rush

    Using our SELECT recipeRetrieving a list of all movie titles using the SELECT statement

  • Even more SQL queriesRetrieving a list of all movies showing the id, title, genre, and duration

    SELECT title, genreFROM movies;

    Returns both title and genre

    SELECT id, title, genre, durationFROM movies;

    Returns id, title, genre and duration

    SELECT *FROM movies;

    Returns all columns

    Returns same result

    The asterisk means to retrieve ALL matching results.

  • Filtering table data

    WHERE Recipe

    Filters out other information and will return only the match that is requested

    SELECT _____FROM _______WHERE ______;

    The WHERE clause can be used to lter out data and match the requested data.

    The match case for the WHERE clause

  • Adding WHERE clause to return smaller set of dataRetrieving the movie title with id of 2.

    Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    SELECTFROMWHERE

    Filtering the Movies by id of 2.title

    moviesid = 2;

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    SELECT titleFROM moviesWHERE id = 2;

    Before retrieving any data, all columns with a name of title are selected.

    Adding WHERE clause to return smaller set of data

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    SELECT titleFROM moviesWHERE id = 2;

    Is this a correct row?

    Adding WHERE clause to return smaller set of data

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    SELECT titleFROM moviesWHERE id = 2;

    This is a correct row.

    Adding WHERE clause to return smaller set of data

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    SELECT titleFROM moviesWHERE id = 2;

    The last two rows are also searched for matching conditions, but there are none.

    Adding WHERE clause to return smaller set of data

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    SELECT titleFROM moviesWHERE id = 2;

    Since the column title is the only column

    that is queried, thats the only data returned.Nosferatu

    Adding WHERE clause to return smaller set of data

  • Using the WHERE clause to return a string of dataRetrieving all movies with the title of The Kid.

    Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    SELECTFROM Filtering all movies by title of The Kid.WHERE

    *moviestitle = 'The Kid';

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    SELECT *FROM movies

    Since data for all movies is searched, all records are at first considered for the result.

    WHERE title = 'The Kid';

    Using the WHERE clause to return a string of data

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    SELECT *FROM moviesWHERE title = 'The Kid';

    However, if focus is placed on the column title as the first filter

    Using the WHERE clause to return a string of data

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    SELECT *FROM moviesWHERE title = 'The Kid';

    These are used for selecting whole string character values.

    Single quotation marks are used around a string value.Using the WHERE clause to return a string of data

  • Try SQLSection 3: Guiding Data Criteria

  • What if we want to retrieve ordered data?Retrieve the title of the movies sorted by duration.

    We want to retrieve all titles of the movies, sorted by duration, and return these values for all of the films in the table.

    How can data be retrieved in a specially ordered way?

  • Sorting data

    ORDER BY Recipe

    Sorts data by columns based on how it is to be viewed

    SELECT _____FROM _______ORDER BY ______;

    The ORDER BY clause can be used to sort data in a specic way.

    The column to be ordered

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    Retrieving data using the ORDER BY clause

    id title genre duration

    SELECTFROMORDER BY

    The title column is where we are pulling

    data from. Results can be displayed in ascending or descending order.

    Retrieve the title of the movies sorted by duration.

    titlemovies

    duration;

  • id title genre duration

    SELECT titleFROM moviesORDER BY duration;

    Also, as a reminder, sorting the title column focuses on which

    duration is lowest.

    Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    Retrieving data using the ORDER BY clause

  • The Kid Comedy 683

    Nosferatu Horror 942

    The Gold Rush Adventure 954

    Metropolis Sci-Fi 1531

    Sorting using ORDER BY in ascending order

    id title genre duration

    SELECT titleFROM moviesORDER BY duration;

    The duration is growing as more results are

    added to the query.

    The result set defaults to displaying data in ASC order, which is known as ascending.

  • Metropolis Sci-Fi 1531

    The Gold Rush Adventure 954

    Nosferatu Horror 942

    The Kid Comedy 683

    id title genre duration

    SELECT titleFROM moviesORDER BY duration DESC;

    This is how descending (DESC) order can be

    returned from the result set, shown as the duration gets lower.

    Sorting using ORDER BY in descending orderThe result set displays data in DESC order, which is known as descending.

  • Filtering data using comparison operatorsFind lms that have a duration of more than 100 minutes.

    We want to retrieve all films that have a duration of more than 100 minutes. How can we use SQL to find the results?

    How do we use current data to compare against itself to find the desired results?

  • Comparing data using comparison operators

    Comparison Operator RecipeUsed to signify that all items to the left are greater than what is to the right

    SELECT _____FROM _______WHERE ______ > ______;

    Comparison operators can also be used to specify data results.

    Multiple comparison fields are used to compare records in the query.

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    Using comparison operators

    id title genre duration

    SELECT *FROM moviesWHERE duration = 100;

    The query retrieves no records that provide a true comparison.

    Find lms that have a duration equal to 100 minutes.

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    Using comparison operators

    id title genre duration

    SELECT *FROM moviesWHERE duration > 100;

    Since the duration time for Metropolis is compared to all movie durations, only one record is retrieved.

    Using the greater-than symbol.

  • More comparison operators in SQLHere are more comparison operators that can be used to lter multiple conditions.

    SELECT *FROM moviesWHERE duration < 100;

    SELECT *FROM moviesWHERE duration >= 94;

    SELECT *FROM moviesWHERE duration

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    Using the NOT EQUAL comparison operator

    id title genre duration

    SELECT *FROM moviesWHERE genre 'Horror';

    The Not Equal To sign ( or !=) is used to compare all

    records that do not have a genre of Horror, as shown.

    Find lms that do not have a genre of Horror.

  • Matching records with multiple conditions

    We want to retrieve all records that have both an id of 1 and have the genre of Comedy. How would we do this with SQL?

    How do we retrieve the data with specific matching conditions?

    Retrieve records that have an id of 1 and genre of Comedy.

  • Showing matching criteria using AND operator

    AND Recipe

    Uses multiple criteria to display matching records in a table

    SELECT _____FROM _______WHERE ______AND ______;

    Using the AND operator to lter multiple conditions.

    Multiple matching criteria to display matching records in the query

  • Using the AND operator to select precise data

    Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    SELECT titleFROM moviesWHERE id = 1AND genre = 'Comedy';

    All conditions of the SELECT statement must

    be met. If not, the search, or query, will return no records, as shown.

    Retrieve records that have an id of 1 and genre of Comedy.

  • Metropolis Comedy 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    SELECT titleFROM moviesWHERE id = 1AND genre = 'Comedy';

    However, if the genre of Metropolis were

    changed to Comedy, a better display of the AND operator concepts is shown.

    Using the AND operator to select precise data

  • id title genre duration

    SELECT titleFROM moviesWHERE id = 1AND genre = 'Comedy';

    The WHERE and AND conditions must both be

    true.

    Metropolis Comedy 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    Using the AND operator to select precise data

  • Metropolis Comedy 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    SELECT titleFROM moviesWHERE id = 1AND genre = 'Comedy';

    Even though two records both contain Comedy, only one has an id of 1.

    Using the AND operator to select precise data

  • Matching records with dierent conditionsRetrieve records that have an id of 1 or genre of Comedy.

    We want to retrieve all records that have either an id of 1 or have the genre of Comedy. What conditions must be met to complete this with SQL?

    How do we retrieve the data with specific matching conditions?

  • Showing matching criteria using OR operator

    OR Recipe

    The OR operator is included to help cover

    more range with our SELECT statement.

    SELECT _____FROM _______WHERE ______OR ______;

    The OR operator can be used to lter multiple conditions.

    Multiple matching criteria to display matching records in the query

  • Using the OR criteria condition

    id title genre duration

    SELECT titleFROM moviesWHERE id = 1OR genre = 'Comedy';

    Either the WHERE condition or the OR

    condition can be true.

    Metropolis Comedy 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    Retrieve records that have an id of 1 or genre of Comedy.

  • Metropolis Comedy 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    SELECT titleFROM moviesWHERE id = 1OR genre = 'Comedy';

    Both records hold true since the result set can contain at least an id of 1 or a genre of Comedy.

    Using the OR criteria condition

  • Try SQLSection 1: Adding Data

  • Handling data using SQLUsing SQL to direct the data to our database

    We want to add a new movie called The Circus to the Movies table. It s a Comedy with a duration of 71 minutes.

    Okay, now we can access the data. But how do we add data to the current table?

  • Adding data to the tableTo add movie information, we need to use an INSERT statement.

    The column or columns we want to add to

    The table the data is in

    INSERT Recipe

    INSERT INTO ______ (_______)VALUES (____________);

    The actual values or data to add, separated by commas

  • Adding data using SQL

    Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    The task is to add a new movie called The Circus to the table movies. It is a Comedy with a duration of 71 minutes.

    Using the INSERT INTO statement to add data to the Movies table

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    VALUES (5, 'The Circus', 'Comedy', 71);

    The INSERT INTO statement signals that a new row will be added.

    Adding data using SQL

    INSERT INTO movies (id, title, genre, duration)

  • VALUES (5, 'The Circus', 'Comedy', 71);

    Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    Here, the movies table specifies into which table the new data will be added.

    Adding data using SQL

    INSERT INTO movies (id, title, genre, duration)

  • VALUES (5, 'The Circus', 'Comedy', 71);

    Specifying which columns to insert data into

    Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre durationColumn names are specied to tell our statement where to insert the values.

    INSERT INTO movies (id, title, genre, duration)

  • The VALUES keyword inside the INSERT statement

    Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    The VALUES keyword signifies where to begin placing the values to be entered.

    VALUES (5, 'The Circus', 'Comedy', 71);INSERT INTO movies (id, title, genre, duration)

  • The actual data to be added to the table

    Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    and here are the VALUES. Remember the quotes from earlier?

    VALUES (5, 'The Circus', 'Comedy', 71);INSERT INTO movies (id, title, genre, duration)

  • The result of the INSERT statement

    Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    The Circus Comedy 715

    So, lets execute the INSERT INTO statement and see that there are

    now 5 rows.

    VALUES (5, 'The Circus', 'Comedy', 71);INSERT INTO movies (id, title, genre, duration)

  • A dierent way to insert data into the table

    The same goal could have been accomplished without having the columns, only because data is being inserted into all the columns.

    Same

    VALUES (5, 'The Circus', 'Comedy', 71);

    This is a quicker way to write code.

    INSERT INTO movies VALUES ( );5, 'The Circus', 'Comedy', 71

    INSERT INTO movies (id, title, genre, duration)

  • We dont have to insert into every column

    INSERT INTO movies (id, title)VALUES (5, 'The Circus');

    The Circus5

    INSERT INTO movies

    The Fly 806

    Notice the id was set, even though it wasnt explicitly done. Why is this?

    (title, duration)('The Fly',80);VALUES

  • Understanding primary keys

    Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    The Circus Comedy 715

    The Fly 806

    SQL can have a primary key for a table, which is unique.

    This field will never be blank or empty.

    INSERT INTO movies (title, duration)('The Fly',80);VALUES

  • Understanding auto-increment

    Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    The Circus Comedy 715

    The Fly 806

    INSERT INTO movies VALUES ( );

    SQL can automatically increment the primary key for a table for new rows.

    This is now a new row with an id of 6.

    (title, duration)'The Fly',80

  • Understanding NULL / empty elds

    Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    The Circus Comedy 715

    The Fly 806

    INSERT INTO movies VALUES ( );

    SQL can also add data where there is no data for a eld.

    A NULL value is used to represent a field with missing data.

    A NULL value is used as a placeholder for unknown data.

    (title, duration)'The Fly',80

  • Try SQLSection 2: Changing Current Data

  • Changing the data using SQLUsing SQL to change the data in the database

    We want to change the genre for the film The Circus from a Comedy to a Romance.

    Great we can now add new data to the database. But how do we update existing data?

  • Updating data in the tableTo change the movie data, an UPDATE statement is needed.

    The table the data is in

    UPDATE Recipe

    The WHERE condition to help pinpoint where the change will take place

    The value needed to change the column to

    UPDATE ______SET ______ = ______(WHERE clause)

    This sets the column to be changed, followed by the column name

  • Changing current data using SQLUsing the UPDATE statement to change data in the Movies table

    Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    The Circus Comedy 715

    So how can the genre of The Circus be changed?

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    UPDATE moviesSET genre = 'Romance'

    The Circus Comedy 715

    WHERE id = 5;

    Updating existing dataUse the UPDATE statement to change an existing row.

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    UPDATE moviesSET genre = 'Romance'

    The Circus Comedy 715

    State the table movies to pinpoint where this UPDATE will be made.

    WHERE id = 5;

    Updating the Movies table

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    UPDATE moviesSET genre = 'Romance'

    The Circus Comedy 715

    Lets use the SET keyword to set the column genre to a value of Romance

    WHERE id = 5;

    The SET keyword in our UPDATE statement

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    UPDATE moviesSET genre = 'Romance'

    The Circus Comedy 715

    WHERE the id is 5.

    WHERE id = 5;

    Using WHERE to specify the UPDATE statement

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    The Circus Romance 715

    The UPDATE statement successfully changed the genre from Comedy

    to Romance for the movie The Circus.

    The record has been successfully changed

    UPDATE moviesSET genre = 'Romance'WHERE id = 5;

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    The Circus Comedy 705

    Separate changes by adding a comma and add each entry to the end of the line.

    Changing multiple elds at once

    UPDATE moviesSET genre = WHERE id = 5;

    Making multiple changes to an existing row

    'Comedy', duration = 70

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Romance 683

    The Gold Rush Adventure 954

    id title genre duration

    The Circus Romance 705

    To accomplish this, simply add an OR clause to the WHERE statement.

    Changing multiple rows at once

    UPDATE moviesSET genre = WHERE id =

    Making multiple changes to dierent rows

    'Romance'3 OR id = 5;

  • Try SQLSection 3: Removing Data

  • Removing data using SQLUsing SQL to remove data from the database

    We want to remove the film The Circus from our database.

    We can add or update data in the database. But how do we remove existing data?

  • Removing data in a tableUse a DELETE statement to remove movie data

    The table that contains the data to be removedDELETE Recipe

    The WHERE condition to help pinpoint where the change will take place

    DELETE FROM _____ (WHERE clause);

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    The Circus Romance 715

    How can a movie be removed from a table using SQL? We DELETE it, of course!

    Deleting data using SQLUsing the DELETE statement to remove a record from the Movies table

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    DELETE FROM movies WHERE id = 5;

    The Circus Romance 715

    The DELETE statement removes rows from the table based on which

    conditions are specified by the WHERE clause.

    Removing a record from the Movies table

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    DELETE FROM movies WHERE id = 5;

    The Circus Romance 715

    The FROM keyword specifies the exact table we will perform this

    command on

    Deleting data

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    DELETE FROM movies WHERE id = 5;

    The Circus Romance 715

    and the row is specified with the WHERE clause.

    Using WHERE to specify data removal

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    What would have happened if the WHERE clause was not included?

    Removing the row from our table

    DELETE FROM movies WHERE id = 5;

    The row has been removed completely from the Movies table.

  • Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    id title genre duration

    What would have happened if the WHERE clause was not included?

    Removing the row from our table

    DELETE FROM movies

    The row has been removed completely from the Movies table.

    ;

  • id title genre duration

    Lucky for us that we didnt do this, right?

    Deleting all data from a table

    DELETE FROM movies

    Metropolis Sci-Fi 1531

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    Completely removing all data from a table

    ;

  • Metropolis Sci-Fi 1531

    id title genre duration

    This statement would remove all records that have a duration of less than 100.

    Removing multiple rows from a table

    DELETE FROM movies

    Nosferatu Horror 942

    The Kid Comedy 683

    The Gold Rush Adventure 954

    Including a less-than comparison operator in the WHERE statement

    WHERE duration < 100;

  • Try SQLSection 1: Creating and Removing Databases

    and Tables

  • Using SQL to create new databases Using SQL to create a new database

    We want to add a new theater to the list of databases.

    We can retrieve and manipulate our data. How do we create a completely new database?

  • Gatsby Theaters

    Movies

    PromotionsConcessions

    Showtimes

    showtime info

    promotions info

    movie info

    concessions info

    Gatsby Theaters DatabaseRemember the rst database?

    We need to create similar tables in a new database.

  • Creating a new theater databaseFinding out what is needed to create a completely new database

    Chaplin TheatersFirst, lets start out with an empty database.

  • What goes inside of the new database

    Chaplin Theaters

    This is the way the new database will look, but there arent any tables.

  • The name of the database

    The statement to create the database

    CREATE DATABASE Recipe

    CREATE DATABASE __________;.

    Creating databases using SQLSQL can be used to create and maintain databases and their tables

  • Empty database created no tables yet!

    Using the CREATE DATABASE statement

    CREATE DATABASE ;

    Chaplin Theaters

    Chaplin Theaters

  • Using SQL to remove databases Using SQL to remove a database

    We want to see what it would take to remove a database from the list of databases.

    What if we realized we do not need a new theater. How would we remove the database?

  • The name of the database

    The statement to remove the database

    DROP DATABASE Recipe

    DROP DATABASE __________;.

    Dropping databases using SQLSQL can be used to remove entire databases and their tables

  • Chaplin Theaters

    Using the DROP DATABASE statement

    DROP DATABASE ;

    Be warned: Once this command is run, the database and ALL its data will no longer be available!

    Chaplin Theaters

  • The new (empty) database would be completely removed from the server!

    DROP DATABASE ;

    Using the DROP DATABASE statement

    Chaplin Theaters

  • Using SQL to create new tables Using SQL to create a new table

    We want to create a Movies table inside the Chaplin Theaters database.

    How can we create a completely new table inside of the new database?

  • The name of the tableCREATE TABLE Recipe

    CREATE TABLE ________(

    column_name3

    );

    column_name2column_name1

    Set of parentheses describing the columns of the table

    Using SQL to create new tables

  • CREATE TABLE Recipe

    CREATE TABLE(

    column_name3 datatype,

    );

    column_name2 datatype,column_name1 datatype,

    Normally a description of the data contained in that column

    Describes what kind of value a column can contain

    Using SQL to create new tables

  • Chaplin Theaters databaseAdding a new table to the new database

    Chaplin Theaters

    Movies

    showtime info

    promotions info

    movie info

    concessions info

    This is the first table to be added to the database.

  • Choosing how the columns will look

    How can the identity of the columns and their data types be created in the database?

    Chaplin Theaters data for movies

  • Explanation of creating columns in a table4 separate columns of data

    numbers characters characters numbers

  • Explanation of creating columns in a table

  • First column of data

    numbers

    The rst grows incrementally as more rows are added.

  • Chaplin Theaters

    Creating the rst column

    CREATE TABLE movies(

    );

    int,id column_name column_name column_name

    datatype,datatype,datatype

    This column will hold numbers. Therefore, it will use the data type of INT.

  • Looking at the next column

    characters

    This is where the names of the lms are given, which can be letters and/or numbers.

  • More column formation

    (

    );

    varchar(50),datatype,datatype

    int,

    Chaplin Theaters

    CREATE TABLE movies

    id title column_name column_name

    This column will hold letters or numbers, so it will use the data type of VARCHAR.

  • Creating columns in a table

    characters

    This column is where the type of lm is given, which are letters.

  • Creating columns from dierent data

    (

    );

    varchar(15),datatype

    varchar(50),int,id

    title genre column_name

    CREATE TABLE movies

    Chaplin TheatersThis column will hold letters or numbers, so it will also use the data type of VARCHAR.

  • Last column

    numbers

    This column appears to hold only numbers.

  • Adding the last column to the table

    (

    );intvarchar(100),varchar(20),int,id

    title genre duration

    CREATE TABLE movies

    Chaplin TheatersThis column will hold numbers, so it will use the data type of INT.

  • Gatsby Theaters

    The new movies table has been created in the Chaplin Theaters database.

    Adding a new table to the database

    (

    );

    varchar(100),varchar(20),int,

    Movies

    id title genre duration

    CREATE TABLE movies

    int

  • Movies

    So whats inside of the new table?We would see an empty table about the Movies.

    Looks like its time to add, or INSERT, some data

    into the newly created table, which we learned how to do earlier.

    id title genre duration

  • Using SQL to remove a table Removing a table using SQL

    We want to know how to remove the Movies table from the Chaplin Theaters database.

    How do we remove an existing table from the database?

  • The name of the table to be removed

    The statement to remove a table

    DROP TABLE Recipe

    DROP TABLE __________;

    Removing tables using SQLUsing SQL to remove a table from a database

  • DROP TABLE movies;

    Deleting tables

    To remove the movies table from the Chaplin Theaters database, this is what would happen.

    Chaplin Theaters

    Movies

  • Chaplin Theaters

    DROP TABLE movies; It would no longer be available to use by the Chaplin Theaters database.

    Deleting tables

  • Try SQLSection 2: Manipulating Tables

  • Using SQL to change table structure Using SQL to alter a column inside of a table

    We want to add a new column called Ratings to the Movies table.

    How can we add, change, or even drop a column in an existing table?

  • Using the ALTER TABLE command

    The table to make changes to

    The name of the column

    ALTER TABLE ADD Recipe

    The data type of the new column

    ALTER TABLE _______ADD COLUMN _________ ______;.

    The ALTER TABLE command is used to add, modify, and remove columns in a table.

    The ADD clause allows a column to be added to a table

  • Adding a column to a table

    Lets ADD the column of ratings to the movies table.

    It should be able to store numbers from 1 to 10 with the int data type.

    ALTER TABLE ADD COLUMN ;.

    Movies

    Don Juan Romance 1101

    Peter Pan Adventure 1052

    The Lost World Fantasy 1063

    Robin Hood Adventure 1434

    id title genre duration

    moviesratings int

  • Movies

    id title genre duration ratings

    Remember how to UPDATE data?ALTER TABLE ADD COLUMN ;

    Don Juan Romance 1101

    Peter Pan Adventure 1052

    The Lost World Fantasy 1063

    Robin Hood Adventure 1434

    Adding a column to a table

    moviesratings int

  • Adding some data to the ratings column

    UPDATE moviesSET ratings = 8WHERE title = 'Don Juan';

    UPDATE moviesSET ratings = 9WHERE title = 'Peter Pan';

    UPDATE moviesSET ratings = 7WHERE title = 'The Lost World';

    UPDATE moviesSET ratings = 7WHERE title = 'Robin Hood';

    Again, the WHERE clause is used to help pinpoint the places where data can be added.

  • New column now has dataMovies

    id title genre duration8

    9

    7

    7

    ratings

    The new column now has data that can be used in the database.

    Don Juan Romance 1101

    Peter Pan Adventure 1052

    The Lost World Fantasy 1063

    Robin Hood Adventure 1434

  • Removing a column from a table

    If the column is no longer needed, it can be removed with the DROP clause.

    Movies

    id title genre duration ratings

    ALTER TABLE moviesDROP COLUMN ratings;

    Don Juan Romance 1101

    Peter Pan Adventure 1052

    The Lost World Fantasy 1063

    Robin Hood Adventure 1434

    8

    9

    7

    7

  • Removing a column from a table

    The column has been removed from the table.

    ALTER TABLE moviesDROP COLUMN ratings;

    Movies

    Don Juan Romance 1101

    Peter Pan Adventure 1052

    The Lost World Fantasy 1063

    Robin Hood Adventure 1434

    id title genre duration