Learning MySql - Creating Database

7/14/10

Today we will learn how to create database in MySql. First i will give you an example of ERD. You can learn about ERD in previous lesson. I will make the database called 'inventory'.
ERD Inventory
ERD Inventory

So there are 6 tables in Database 'Inventory'. What's the meaning of this ERD? Okay it's some example of database in minimarket. There are Goods who contain data of goods and have relation with goods_type and proc_detail. Procurement have relation with proc_detail, which have relation directly to goods. The point is this is an example of database in minimarket, of course, in easy and simple way. The real ERD and Database of minimarket/supermarket is a lot complex than this.

Now step how to create database :
  1. Run your xampp, and start Apache and MySql.
  2. Go into your browser ant type : localhost/phpmyadmin
  3. MySql Page
    MySql Page
  4. Look into create database form, and write inventory on it, then click create
  5. Table Page in MySql
    Table Page in MySql
  6. Then look for  tab called structure, click on it, and create table Goods_Type and fill the number of fileds with 2. It's because we have 2 attributes in Goods Type (Look at ERD above)
  7. On the Field Form, fill the form based on ERD that we have created. You can make it same with picture below, the click save.
    Field Form in MySql
    Field Form in MySql
    Well you can see the red section, it's called the data type. We have id_type that have data_type INT. INT is integer. If you forget about it, you can read about overview of mysql. Then you can see the blue section. You choose the Primary on id_type based on ERD, and what is auto_increment? Auto_increment is a functionality that will make your Primary Key automatically generate, so we shouldn't have to create it manually. If you still confuse, dont worry, we will learn this in insert function.
  8. Then if you succedd create it, the page will show.
    Table in MySql
    Table in MySql
    You have succesfully created it, the red and blue section is the proof. Now we continue to create another tables. 
To create another tables, the step is the same, we go directly to step 5 oke?
Field Form in MySql
Field Form in MySql
So based on ERD again, make sure you create 5 fields. If you didn't do it, check out red section there, and add the number of field that you want. The other tables is the same, you can create it by your own ok?
Now next step is how to make the 2 tables above become a relationship table?

Go back into your inventory page, and look for SQL table, and write this :
alter table goods add foreign key(id_type) references goods_type (id_type) on delete restrict on update restrict;

SQL text form in MySql
SQL text form in MySql
Well in that picture, the syntaks is wrong, so don't follow the syntax on picture ok?

So that's all for creating database, the rest of table you can create it by urself. Thanks for reading!

No comments:

 

Tags