universitas bina darma 2013 data definition language (ddl)

22
UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)

Upload: jacob-harrison

Post on 19-Jan-2018

225 views

Category:

Documents


0 download

DESCRIPTION

1. Create Database 2. Delete Database a. Create database databasename; example “ create database akademik; “ b. Drop database databasename; example “ Drop database akademik” c. To open / use the database “use namadatabase;” example “ use akademik; ”

TRANSCRIPT

Page 1: UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)

UNIVERSITAS BINA DARMA2013

DATA DEFINITION LANGUAGE(DDL)

Page 2: UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)

1. Create Database2. Delete Database3. Creating Tables4. Deleting Tables5. Seeing Table Structure6. See Tables Have Been Created7. Defining Null / Not Nulll8. Defining Default Values9. Defining a Primary Key10. Deleting a Primary Key Table11. Adding a New Column In Table12. Changing the Data Type and Column Width in Table13. Change Column Name14. Delete Column In Table15. Mendefeinisikan Foreign Key Table16. Deleting a Foreign Key Table

Page 3: UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)

1. Create Database 2. Delete Databasea. Create database databasename;example “ create database akademik; “b. Drop database databasename;example “ Drop database akademik”c. To open / use the database“use namadatabase;”example “ use akademik; ”

Page 4: UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)

3. Membuat Tabel

Page 5: UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)
Page 6: UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)

4. Deleting Tables

Drop Table tablename ;

example “Drop Table mahasiswa;”

Page 7: UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)

5. Seeing Table Structure

the Command Structure“ Describe “example describe mahasiswa;

Page 8: UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)

6. See Table A has been madeUsing Commands“ show tables “example show tables;

Page 9: UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)

To menduplicate table structure to new table

To mencuplicate table structure to a new table that can use the following command:Create table karyawan_baru like employees;

Example :

Mysql > create table mahasiswa_baru like mahasiswa;

Then it will create an additional table with the names of fruit karyawan_baru where the resulting structure together with the employee table

Page 10: UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)

7. Defining Null / Not Null

Not that the column with Null column type should not be vacated, and the column with Null column type can be left blank

Syntax :CREATE TABLE namatabel(Field1 TipeData1 NOT NULL,Field2 TipeData2);

Page 11: UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)

8. Defining Default Values

The default value is the value given by the system automatically to a column when there is the addition of new lines, while the value of the field is not filled by user. Syntax :CREATE TABLE namatabel(Field1 TipeData1,Field2 TipeData2 DEFAULT nilai);value is the default value of the column.

Page 12: UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)

9. Defining a Primary Key

There are three ways to define the primary key. The following is a syntax to define the primary key for Field1.

1. CREATE TABLE namatabel(Field1 TipeData1 NOT NULL PRIMARY KEY,Field2 TipeData2);

Page 13: UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)

2. CREATE TABLE tablename(Field1 TipeData1,Field2 TipeData2,PRIMARY KEY(Field1));

3. ALTER TABLE tablename ADD CONSTRAINT constraintname PRIMARY KEY (columnname);example :create table jenisfilm with column type data type char (6), price int data type by defining a null note values and primary key for the column type and default value for the price column :

or

Page 14: UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)

or

Page 15: UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)

10. Deleting a Primary KeyDeleting Primary KeyMethod 1: If the primary key is made by using the alter table:ALTER TABLE DROP CONSTRAINT namatabel namaconstraint;Method 2: If the primary key is created through create table:tablename ALTER TABLE DROP PRIMARY KEY; Here is the command used to remove the primary key in the table jenisfilm :

Page 16: UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)

11. Adding a New Column

ALTER TABLE namatabel ADD fieldbaru tipe;namatabel is the name of tables to be added [by] his field. Fieldbaru is the name of column to be added, type is data type of column to be added. Following govern to add description column with data type of varchar(25) :

Page 17: UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)

12. Altering Type Data and Wide of Column At Tables.

ALTER TABLE namatabel MODIFY COLUMN field tipenamatabel is the name of tables to be added [by] his field. Fieldbaru is the name of column to be added, type is data type of column to be added. Following govern to add description column with data type of varchar(25) :

Page 18: UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)

13. Altering the Name of Column.

ALTER TABLE namatabel CHANGE COLUMN namabarukolom namalamakolom of him; namatabel is the name of tables to be altered by the name of his column, namalamakolom is column to by him, namabarukolom is new name of column, of him is data type of column. Following govern to alter the name of description column become ket. :

Page 19: UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)

14. Vanishing the Name of Column.

Syntax :ALTER TABLE namatabel DROP COLUMN namakolom;Following govern to vanish column of ket at tables of jenisfilm. :

Page 20: UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)

15. Defining Foreign Key.

To define key foreign, hence have to be ensured that [by] referred attribute and tables ( tables of mains of key foreign) have been defined beforehand. Syntax :1. CREATE TABLE namatabel(Field1 TipeData1,Field2 TipeData2,FOREIGN KEY (Field2) REFERENCES namatabelinduk(namakolominduk)ON UPDATE CASCADEON DELETE NO ACTION)

Page 21: UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)

2. ALTER TABLE namatabel ADD CONSTRAINT namaconstraint FOREIGN KEY (namakolom) REFERENCES namatabelinduk (namakolominduk) ON UPDATE CASCADE ON DELETE NO ACTION;

or

Page 22: UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)

16. Vanishing Foreign Key.

ALTER TABLE namatabel DROP FOREIGN KEY namaconstraint; Following govern to vanish key foreign at film tables. :