mysql in action step by step method to create your own database

26
MySql In Action MySql In Action Step by step method to Step by step method to create your own database create your own database

Upload: cory-antony-obrien

Post on 24-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MySql In Action Step by step method to create your own database

MySql In ActionMySql In Action

Step by step method to create Step by step method to create your own databaseyour own database

Page 2: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 2/44

What is next?What is next?

Having convinced that MySql is a strong tool to Having convinced that MySql is a strong tool to develop databases, you are wondering if you develop databases, you are wondering if you can use it to develop your own business.can use it to develop your own business.

This is the story.This is the story.– You want to open a small video rental in Seattle. You are You want to open a small video rental in Seattle. You are

thinking to create a small database to keep track what thinking to create a small database to keep track what kind of films that you have, how long the films are checked kind of films that you have, how long the films are checked out, and how much money that a customer has to pay for out, and how much money that a customer has to pay for renting a film .renting a film .

– Since you already know that MySql is easy to use, you will Since you already know that MySql is easy to use, you will use it for developing your own database.use it for developing your own database.

Page 3: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 3/44

Database Structure for Video Rental IDatabase Structure for Video Rental I

After spending some time, you come up with the After spending some time, you come up with the following structure of the database that you following structure of the database that you need for your small video rental.need for your small video rental.

– CustomersCustomers• customerID ( int ) => Primary KeycustomerID ( int ) => Primary Key• firstName ( VarChar(20) )firstName ( VarChar(20) )• lastName ( VarChar(20) )lastName ( VarChar(20) )• address ( VarChar(20) )address ( VarChar(20) )

– RentalsRentals• movieID ( int ) => Primary KeymovieID ( int ) => Primary Key• title ( VarChar(20) )title ( VarChar(20) )• copyNum ( int )copyNum ( int )

Page 4: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 4/44

Database Structure for Video Rental IIDatabase Structure for Video Rental II

– CheckOutCheckOut• entryID ( int ) => Primary KeyentryID ( int ) => Primary Key• customerID ( int )customerID ( int )• movieID ( int )movieID ( int )• checkOutDate ( date/time )checkOutDate ( date/time )• duration ( int )duration ( int )• cost ( double )cost ( double )

Page 5: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 5/44

Connecting To MySqlConnecting To MySql

Until this point, you are ready to create tables Until this point, you are ready to create tables to implement your database.to implement your database.

The first step that you have to do is to connect The first step that you have to do is to connect to MySql by using your favorite tool, such as to MySql by using your favorite tool, such as Telnet or x-window.Telnet or x-window.

I assume that you already contacted the I assume that you already contacted the database administrator to create a blank database administrator to create a blank database which is called “video_rental”.database which is called “video_rental”.

On the prompt, type in On the prompt, type in mysql -p video_rentalmysql -p video_rental– ‘‘-p’ option is used to tell MySql that you will use password -p’ option is used to tell MySql that you will use password

to connect to your database.to connect to your database.

Page 6: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 6/44

Connecting To MySql (Cont….)Connecting To MySql (Cont….)

After you logon to MySql, you should get After you logon to MySql, you should get similar result as the following:similar result as the following:

Note:Note:– I use x-window to logon to MySqlI use x-window to logon to MySql– On the prompt, it displays “ffaizal@cochise”On the prompt, it displays “ffaizal@cochise”

• cochise is the name of the server that I use, andcochise is the name of the server that I use, and• ffaizal is my login name in that server. ffaizal is my login name in that server.

– You should get similar prompt when you logon to MySql.You should get similar prompt when you logon to MySql.

Page 7: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 7/44

Creating Customers tableCreating Customers table

The next step is to create tables on the The next step is to create tables on the video_rental database.video_rental database.

The First table that you will create is The First table that you will create is Customers Customers table.table.

To create that table, you need to use To create that table, you need to use create create tabletable command. command.

In this case, you should useIn this case, you should use create table Customers (customerID int unsigned create table Customers (customerID int unsigned

auto_increment primary key, firstName varchar(20) not auto_increment primary key, firstName varchar(20) not null default ‘N/A’, lastName varchar(20) not null default null default ‘N/A’, lastName varchar(20) not null default ‘N/A’, address varchar(30) not null default ‘N/A’);‘N/A’, address varchar(30) not null default ‘N/A’);

Page 8: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 8/44

Creating Customers table (Cont….)Creating Customers table (Cont….)

If you want to know and check the table that If you want to know and check the table that you just created, you can use you just created, you can use showshow command. command.– I used I used show columns from Customers;show columns from Customers;

Page 9: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 9/44

Creating Customers table (Cont….)Creating Customers table (Cont….)

Note:Note:– customerID is a primary key. I declared it as an unsigned customerID is a primary key. I declared it as an unsigned

integer with auto increment so that I can always get the integer with auto increment so that I can always get the unique number.unique number.

– For the rest of the table, I declared them as variable For the rest of the table, I declared them as variable characters with a default value N/A.characters with a default value N/A.

– In all columns, I will not allow them to have null as value.In all columns, I will not allow them to have null as value.

Page 10: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 10/44

Creating Rentals tableCreating Rentals table

The second table that you should create is The second table that you should create is Rentals table.Rentals table.

Following the syntax from the previous table Following the syntax from the previous table creation, you should come up with the creation, you should come up with the following command:following command: create table Rentals (movieID int unsigned auto_increment create table Rentals (movieID int unsigned auto_increment

primary key, title varchar(20) not null default ‘N/A’, primary key, title varchar(20) not null default ‘N/A’, copyNum int unsigned not null);copyNum int unsigned not null);

Page 11: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 11/44

Creating Rentals table (Cont….)Creating Rentals table (Cont….)

Note:Note:– movieID is a primary key. I declared it as an auto-movieID is a primary key. I declared it as an auto-

incremented unsigned integer.incremented unsigned integer.– Title is a variable character with default value ‘N/A’.Title is a variable character with default value ‘N/A’.– copyNum is an integer with 0 as its default value. copyNum is an integer with 0 as its default value.

Page 12: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 12/44

Creating CheckOut tableCreating CheckOut table

CheckOut table is the last table that you need CheckOut table is the last table that you need to create.to create.

Following the syntax from the previous slides, Following the syntax from the previous slides, the command will be as the following:the command will be as the following: create table CheckOut(entryID int unsigned create table CheckOut(entryID int unsigned

auto_increment primary key, customerID int unsigned not auto_increment primary key, customerID int unsigned not null, movieID int unsigned not null, checkOutDate datetime null, movieID int unsigned not null, checkOutDate datetime not null default 'N/A', duration int unsigned not null, cost not null default 'N/A', duration int unsigned not null, cost double unsigned not null);double unsigned not null);

If you succeeded in creating the table, you will If you succeeded in creating the table, you will get the same result as the one shown on the get the same result as the one shown on the next page.next page.

Page 13: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 13/44

Creating CheckOut table (Cont….)Creating CheckOut table (Cont….)

Note:Note:– entryID is used as a primary key, instead of customerID entryID is used as a primary key, instead of customerID

and movieID. MySql does not allow more than two primary and movieID. MySql does not allow more than two primary keys on the same table.keys on the same table.

– The rest of the table should be clear from the above The rest of the table should be clear from the above screenshot. screenshot.

Page 14: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 14/44

Checking Your DatabaseChecking Your Database

The quick way to check all tables that you have The quick way to check all tables that you have created is by using the created is by using the show tablesshow tables command. command.

Congratulation…. You have successfully created Congratulation…. You have successfully created three tables in the video_rental database.three tables in the video_rental database.

Page 15: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 15/44

Inserting data into the tablesInserting data into the tables

After you have created tables in video_rental After you have created tables in video_rental database, it’s time for you to input the data.database, it’s time for you to input the data.

Suppose that you have invested some money Suppose that you have invested some money to buy five copies of “StarWars” and you want to buy five copies of “StarWars” and you want to store this information into your Rentals to store this information into your Rentals table.table.

How do you do that??? How do you do that???

Page 16: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 16/44

Inserting data into the tables (Cont.)Inserting data into the tables (Cont.)

Use Use insertinsert command. command.

The command should look like the following:The command should look like the following:– insert into Rentals (title, copyNum) values ('StarWars', 5);insert into Rentals (title, copyNum) values ('StarWars', 5);

Since the movieID is auto-increment integer, I do Since the movieID is auto-increment integer, I do not have to explicitly give a value. It is done not have to explicitly give a value. It is done automatically for you.automatically for you.

If the insertion is successful, you will have the If the insertion is successful, you will have the similar result as the one on the next slide.similar result as the one on the next slide.

Page 17: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 17/44

Inserting data into the tables (Cont.)Inserting data into the tables (Cont.)

To see the entry in the table, you can use “select To see the entry in the table, you can use “select * from Rentals;”.* from Rentals;”.– * means that you want to see all available columns in the * means that you want to see all available columns in the

tabletable

Remember to use ‘ or “ at the beginning and at Remember to use ‘ or “ at the beginning and at the end of the string values.the end of the string values.

Page 18: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 18/44

All entries are already insertedAll entries are already inserted

Congratulation…. Congratulation….

To shorten your time, all the data insertions To shorten your time, all the data insertions are already done for you. are already done for you.

Customers tableCustomers table

Page 19: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 19/44

All entries are already inserted (Cont.)All entries are already inserted (Cont.)

Rentals tableRentals table

CheckOut tableCheckOut table

Page 20: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 20/44

Deleting data from a tableDeleting data from a table

You can delete an entry from a table by using You can delete an entry from a table by using deletedelete command. command.

Suppose that you inserted a wrong data in Suppose that you inserted a wrong data in CheckOut table as shown at the last row of the CheckOut table as shown at the last row of the following table.following table.

– There is no customer with ID# 4 and there is not movie with There is no customer with ID# 4 and there is not movie with ID# 4 as well.ID# 4 as well.

Page 21: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 21/44

Deleting data from a table (Cont.)Deleting data from a table (Cont.)

I used the following query to delete the last I used the following query to delete the last row:row:– delete from CheckOut where entryID = 4;delete from CheckOut where entryID = 4;– You can use different condition on You can use different condition on wherewhere clause, such as clause, such as

customerID = 4.customerID = 4.

Page 22: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 22/44

Deleting a table from a databaseDeleting a table from a database

In addition to record deletion command, MySql In addition to record deletion command, MySql also provides a command to delete a table also provides a command to delete a table from a database.from a database.

Use Use drop tabledrop table command to delete a table. command to delete a table.

Suppose that you want to delete a table whose Suppose that you want to delete a table whose name is service, you can use the following name is service, you can use the following command:command:– drop table service;drop table service;

Page 23: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 23/44

Deleting more than one tableDeleting more than one table

You can also use the drop table command to You can also use the drop table command to delete more than one table at the same time delete more than one table at the same time by cascading the table names.by cascading the table names.

Suppose that in addition to Suppose that in addition to serviceservice, you also , you also want to delete tables want to delete tables paypay and and timetime. You can . You can delete them by using:delete them by using:– drop table service, pay, time;drop table service, pay, time;

Page 24: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 24/44

Select-Form-Where querySelect-Form-Where query

Like almost all SQL query, MySql uses Like almost all SQL query, MySql uses SelectSelect, , FromFrom, and , and WhereWhere syntax to retrieve some syntax to retrieve some information form the database. information form the database.

The The FormForm clause is usually used to indicate clause is usually used to indicate which relation(s) to which the query refers.which relation(s) to which the query refers.

The The Where Where clause is consist of the condition in clause is consist of the condition in which tuples must satisfy.which tuples must satisfy.

The The Select Select clause is consist of attribute(s) of clause is consist of attribute(s) of tuples that match the condition(s).tuples that match the condition(s).

Page 25: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 25/44

Select-Form-Where query (Cont.)Select-Form-Where query (Cont.)

Suppose that you want to retrieve the Suppose that you want to retrieve the information about your customer whose last information about your customer whose last name is Doe.name is Doe.

The proper query will beThe proper query will beselect *select *

from Customersfrom Customers

where lastName = ‘Doe’;where lastName = ‘Doe’;

Note: * means that you want to retrieve all information Note: * means that you want to retrieve all information about the customer whose last name is Doe. about the customer whose last name is Doe.

Page 26: MySql In Action Step by step method to create your own database

CSE 498 MySql In Action 26/44

Select-Form-Where query (Cont.)Select-Form-Where query (Cont.)

The result of the query is shown below.The result of the query is shown below.