Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "MySql In Action Step by step method to create your own database."— Presentation transcript:

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

2 CSE 498MySql In Action2/44 What is next? Having convinced that MySql is a strong tool to develop databases, you are wondering if you can use it to develop your own business. Having convinced that MySql is a strong tool to develop databases, you are wondering if you 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 thinking to create a small database to keep track what kind of films that you have, how long the films are checked out, and how much money that a customer has to pay for renting a film. –Since you already know that MySql is easy to use, you will use it for developing your own database.

3 CSE 498MySql In Action3/44 Database Structure for Video Rental I After spending some time, you come up with the following structure of the database that you need for your small video rental. After spending some time, you come up with the following structure of the database that you need for your small video rental. –Customers 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) ) –Rentals movieID ( int ) => Primary KeymovieID ( int ) => Primary Key title ( VarChar(20) )title ( VarChar(20) ) copyNum ( int )copyNum ( int )

4 CSE 498MySql In Action4/44 Database Structure for Video Rental II –CheckOut 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 )

5 CSE 498MySql In Action5/44 Connecting To MySql Until this point, you are ready to create tables to implement your database. Until this point, you are ready to create tables to implement your database. The first step that you have to do is to connect to MySql by using your favorite tool, such as Telnet or x-window. The first step that you have to do is to connect to MySql by using your favorite tool, such as Telnet or x-window. I assume that you already contacted the database administrator to create a blank database which is called “video_rental”. I assume that you already contacted the database administrator to create a blank database which is called “video_rental”. On the prompt, type in mysql -p video_rental On the prompt, type in mysql -p video_rental –‘-p’ option is used to tell MySql that you will use password to connect to your database.

6 CSE 498MySql In Action6/44 Connecting To MySql (Cont….) After you logon to MySql, you should get similar result as the following: After you logon to MySql, you should get similar result as the following: Note: Note: –I use x-window to logon to MySql –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.

7 CSE 498MySql In Action7/44 Creating Customers table The next step is to create tables on the video_rental database. The next step is to create tables on the video_rental database. The First table that you will create is Customers table. The First table that you will create is Customers table. To create that table, you need to use create table command. To create that table, you need to use create table command. In this case, you should use In this case, you should use create table Customers (customerID int unsigned auto_increment primary key, firstName varchar(20) not null default ‘N/A’, lastName varchar(20) not null default ‘N/A’, address varchar(30) not null default ‘N/A’); create table Customers (customerID int unsigned auto_increment primary key, firstName varchar(20) not null default ‘N/A’, lastName varchar(20) not null default ‘N/A’, address varchar(30) not null default ‘N/A’);

8 CSE 498MySql In Action8/44 Creating Customers table (Cont….) If you want to know and check the table that you just created, you can use show command. If you want to know and check the table that you just created, you can use show command. –I used show columns from Customers;

9 CSE 498MySql In Action9/44 Creating Customers table (Cont….) Note: Note: –customerID is a primary key. I declared it as an unsigned integer with auto increment so that I can always get the unique number. –For the rest of the table, I declared them as variable characters with a default value N/A. –In all columns, I will not allow them to have null as value.

10 CSE 498MySql In Action10/44 Creating Rentals table The second table that you should create is Rentals table. The second table that you should create is Rentals table. Following the syntax from the previous table creation, you should come up with the following command: Following the syntax from the previous table creation, you should come up with the following command: create table Rentals (movieID int unsigned auto_increment primary key, title varchar(20) not null default ‘N/A’, copyNum int unsigned not null); create table Rentals (movieID int unsigned auto_increment primary key, title varchar(20) not null default ‘N/A’, copyNum int unsigned not null);

11 CSE 498MySql In Action11/44 Creating Rentals table (Cont….) Note: Note: –movieID is a primary key. I declared it as an auto-incremented unsigned integer. –Title is a variable character with default value ‘N/A’. –copyNum is an integer with 0 as its default value.

12 CSE 498MySql In Action12/44 Creating CheckOut table CheckOut table is the last table that you need to create. CheckOut table is the last table that you need to create. Following the syntax from the previous slides, the command will be as the following: Following the syntax from the previous slides, the command will be as the following: create table CheckOut(entryID int unsigned auto_increment primary key, customerID int unsigned not null, movieID int unsigned not null, checkOutDate datetime not null default 'N/A', duration int unsigned not null, cost double unsigned not null); create table CheckOut(entryID int unsigned auto_increment primary key, customerID int unsigned not null, movieID int unsigned not null, checkOutDate datetime not null default 'N/A', duration int unsigned not null, cost double unsigned not null); If you succeeded in creating the table, you will get the same result as the one shown on the next page. If you succeeded in creating the table, you will get the same result as the one shown on the next page.

13 CSE 498MySql In Action13/44 Creating CheckOut table (Cont….) Note: Note: –entryID is used as a primary key, instead of customerID and movieID. MySql does not allow more than two primary keys on the same table. –The rest of the table should be clear from the above screenshot.

14 CSE 498MySql In Action14/44 Checking Your Database The quick way to check all tables that you have created is by using the show tables command. The quick way to check all tables that you have created is by using the show tables command. Congratulation…. You have successfully created three tables in the video_rental database. Congratulation…. You have successfully created three tables in the video_rental database.

15 CSE 498MySql In Action15/44 Inserting data into the tables After you have created tables in video_rental database, it’s time for you to input the data. After you have created tables in video_rental database, it’s time for you to input the data. Suppose that you have invested some money to buy five copies of “StarWars” and you want to store this information into your Rentals table. Suppose that you have invested some money to buy five copies of “StarWars” and you want to store this information into your Rentals table. How do you do that??? How do you do that???

16 CSE 498MySql In Action16/44 Inserting data into the tables (Cont.) Use insert command. Use insert command. The command should look like the following: The command should look like the following: –insert into Rentals (title, copyNum) values ('StarWars', 5); Since the movieID is auto-increment integer, I do not have to explicitly give a value. It is done automatically for you. Since the movieID is auto-increment integer, I do not have to explicitly give a value. It is done automatically for you. If the insertion is successful, you will have the similar result as the one on the next slide. If the insertion is successful, you will have the similar result as the one on the next slide.

17 CSE 498MySql In Action17/44 Inserting data into the tables (Cont.) To see the entry in the table, you can use “select * from Rentals;”. To see the entry in the table, you can use “select * from Rentals;”. –* means that you want to see all available columns in the table Remember to use ‘ or “ at the beginning and at the end of the string values. Remember to use ‘ or “ at the beginning and at the end of the string values.

18 CSE 498MySql In Action18/44 All entries are already inserted Congratulation…. Congratulation…. To shorten your time, all the data insertions are already done for you. To shorten your time, all the data insertions are already done for you. Customers table

19 CSE 498MySql In Action19/44 All entries are already inserted (Cont.) Rentals table CheckOut table

20 CSE 498MySql In Action20/44 Deleting data from a table You can delete an entry from a table by using delete command. You can delete an entry from a table by using delete command. Suppose that you inserted a wrong data in CheckOut table as shown at the last row of the following table. Suppose that you inserted a wrong data in CheckOut table as shown at the last row of the following table. –There is no customer with ID# 4 and there is not movie with ID# 4 as well.

21 CSE 498MySql In Action21/44 Deleting data from a table (Cont.) I used the following query to delete the last row: I used the following query to delete the last row: –delete from CheckOut where entryID = 4; –You can use different condition on where clause, such as customerID = 4.

22 CSE 498MySql In Action22/44 Deleting a table from a database In addition to record deletion command, MySql also provides a command to delete a table from a database. In addition to record deletion command, MySql also provides a command to delete a table from a database. Use drop table command to delete a table. Use drop table command to delete a table. Suppose that you want to delete a table whose name is service, you can use the following command: Suppose that you want to delete a table whose name is service, you can use the following command: –drop table service;

23 CSE 498MySql In Action23/44 Deleting more than one table You can also use the drop table command to delete more than one table at the same time by cascading the table names. You can also use the drop table command to delete more than one table at the same time by cascading the table names. Suppose that in addition to service, you also want to delete tables pay and time. You can delete them by using: Suppose that in addition to service, you also want to delete tables pay and time. You can delete them by using: –drop table service, pay, time;

24 CSE 498MySql In Action24/44 Select-Form-Where query Like almost all SQL query, MySql uses Select, From, and Where syntax to retrieve some information form the database. Like almost all SQL query, MySql uses Select, From, and Where syntax to retrieve some information form the database. The Form clause is usually used to indicate which relation(s) to which the query refers. The Form clause is usually used to indicate which relation(s) to which the query refers. The Where clause is consist of the condition in which tuples must satisfy. The Where clause is consist of the condition in which tuples must satisfy. The Select clause is consist of attribute(s) of tuples that match the condition(s). The Select clause is consist of attribute(s) of tuples that match the condition(s).

25 CSE 498MySql In Action25/44 Select-Form-Where query (Cont.) Suppose that you want to retrieve the information about your customer whose last name is Doe. Suppose that you want to retrieve the information about your customer whose last name is Doe. The proper query will be The proper query will be select * from Customers where lastName = ‘Doe’; Note: * means that you want to retrieve all information about the customer whose last name is Doe.

26 CSE 498MySql In Action26/44 Select-Form-Where query (Cont.) The result of the query is shown below. The result of the query is shown below.


Download ppt "MySql In Action Step by step method to create your own database."

Similar presentations


Ads by Google