Learning MySql - DDL

7/13/10

Code of DDL
Code of DDL


Hello again, today we are gonna learn about DDL. Data Definition Language (DDL) is used to define the structure of database and table. The syntaxt contain four kind : CREATE, DROP, ALTER. The syntaks will be written in Sql tab, besides Structure tab, in MySql. Actually we can write the code in this section, but MySql has give us an easy way to create, delete, the database or table, but sometimes,  we still need to write the code and run it, because not all function of Sql is provided by MySql. We will learn it one by one.

  • CREATE
To make a new database, table, index, or procedure, for example :

Create Database test; //it's used to create new database called
create table supplyer(
`sup_id` integer(11) not null,
`sup_code` varchar(20) not null,
`sup_name` varchar(20) not null,
`sup_addr` varchar(20) not null,
`sup_phone` integer(12) not null,
primary key(`sup_id`)
);
// it will create table called supplyer

if you a little bit confuse, let's check last lesson about creating database. In that lesson, we created the table and database in GUI form, not in code.

  • DROP
It's used to delete an object from DBMS, in this case, MySql. We can drop table, database, or procedure, but we can only drop them if they are not used by another object. For example there are two tables, table school and table student and they have relation 1-n. In this case, we can't drop any of this table, but if we insist want to drop any of this table, we can use ALTER.

DROP Table supplyer;//it's the example code

DROP Table school;//to do this,you should drop any FK that connects with table school, you can look at ALTER section below

  • ALTER
It's used to change the properties inside of MySql. We can alter database and table. For example :

alter table goods add price INT; //it will add column price that has datatype INT in table goods

alter table student drop id_school; // it will drop column id_school which is FK from table student

You can either use the code like this to create, change, delete the table or database, or you can create it with GUI form that's provided by MySql. Good luck!

No comments:

 

Tags