Monday, 11 March 2013

Bahasa Basis Data

Posted by Wawan On 09:20 | No comments


Bahasa Basisdata
l  Definisi:
                        Bahasa yang dipakai untuk berkomunikasi dengan DBMS
l  Bentuk :
                        Structure Query Language (SQL)
l  Macam :
l  Data Definition Language (DDL)
l  Membuat/Menghapus Database
l  Membuat/Mengubah/Menghapus Tabel
l  Mendefinisikan Constraint (Primary key, Foreign Key, dll)
l  Data Manipulation Language (DML)
l  Menambah/Mengubah/Menghapus data
l  Menampilkan Data
Membuat database
l  Membuat otoritas membuat database
l  Buka Interprise Maneger
l  Klik Users pada Database Master
l  Klik Kanan – New Users
l  Pilih login Name : userxxx
l  Check semua list pada Permit in database role
l  Buka Query Analyzer dengan user Anda (userxxxx)
l  Tuliskan Script untuk membuat database
l  Jalankan dengan menekan tombol F5

Script Membuat database
CREATE DATABASE SBDxxxx
ON PRIMARY
( NAME = sbdxxxx,
 FILENAME = 'D:\Data\xxxx\sbd.mdf',
   SIZE = 1,
   MAXSIZE = 10,
   FILEGROWTH = 2 )
LOG ON
( NAME = ‘sbdxxxxlog',
   FILENAME = 'D:\Data\xxxx\sbdlog.ldf',
   SIZE = 1MB,
   MAXSIZE = 10MB,
   FILEGROWTH = 1MB );

Script Menghapus database
DROP DATABASE PBD;

Meng aktifkan database
USE PBD;






Membuat Tabel
l  Syntax
                       
                        CREATE TABLE MAHASISWA(
                                                THANG INTEGER,
                                                NIM INTEGER,
                                                NAMA VARCHAR (30),
                                                ALAMAT VARCHAR (80));
Mendefinisikan Not nul
CREATE TABLE MAHASISWA(
                                                THANG INTEGER not null,
                                                NIM INTEGER not null,
                                                NAMA VARCHAR (30) not null,
                                                ALAMAT VARCHAR (80));
Mendefinisikan Default
CREATE TABLE MAHASISWA(
                                                THANG INTEGER not null,
                                                NIM INTEGER not null,
                                                NAMA VARCHAR (30) not null,
                                                ALAMAT VARCHAR (80) default 'YOGYA');
Mendefinisikan Auto Increment
l  MEMBUAT
CREATE TABLE CONTOH(
                        IDCONTOH INTEGER IDENTITY (0,1) PRIMARY KEY,
                        KODE CHAR(2) NOT NULL,
                        NOMOR INTEGER NOT NULL);
l  MERESET
                        DBCC CHECKIDENT (CONTOH, RESEED, 5)

Mendefinisikan Primary Key
1.        DROP  TABLE MAHASISWA;
CREATE TABLE MAHASISWA(
                                                THANG INTEGER not null,
                                                NIM INTEGER not null primary key,
                                                NAMA VARCHAR (30) not null,
                                                ALAMAT VARCHAR (80) default 'YOGYA');

2.        DROP  TABLE MAHASISWA;
CREATE TABLE MAHASISWA(
                                                THANG INTEGER not null,
                                                NIM INTEGER not null,
                                                NAMA VARCHAR (30) not null,
                                                ALAMAT VARCHAR (80) default 'YOGYA',
PRIMARY KEY (NIM));

3.        DROP  TABLE MAHASISWA;
CREATE TABLE MAHASISWA(
                                                THANG INTEGER not null,
                                                NIM INTEGER not null,
                                                NAMA VARCHAR (30) not null,
                                                ALAMAT VARCHAR (80) default 'YOGYA');
ALTER TABLE MAHASISWA ADD CONSTRAINT PK_MHS PRIMARY KEY (NIM);                



Foreignt Key
CREATE TABEL MENGAJAR(
                        NIP CHAR(10) NOT NULL
                                               REFERENCES DOSEN (NIP)
                                                                        ON UPDATE CASCADE
                                                                        ON DELETE NO ACTION,
                        KODE CHAR(10) NOT NULL
                                               REFERENCES MATAKULIAH (KODE)
                                                                        ON UPDATE CASCADE
                                                                        ON DELETE NO ACTION);

Foreign Key 2
l  ALTER TABLE MENGAJAR
                        ADD CONSTRAINT  FK_MENGAJAR1
                                                FOREIGN KEY (NIP)
                                               REFERENCES DOSEN (NIP)
                                                                        ON UPDATE CASCADE
                                                                        ON DELETE NO ACTION;
l  ALTER TABLE MENGAJAR
                        ADD CONSTRAINT
                                               FK_MENGAJAR2 FOREIGN KEY (KODE)
                                               REFERENCES MATAKULIAH (KODE)
                                                                        ON UPDATE CASCADE
                                                                        ON DELETE NO ACTION;

Aturan
l  Tabel induk harus dibuat terlebih dahulu
l  Tipe field harus sama dengan tipe file yang dirujuk
Menghapus Constraint
l  ALTER TABLE MENGAJAR DROP CONSTRAINT FK_MENGAJAR2;
Mengubah Tabel
l  MENAMBAH KOLOM
                        ALTER TABLE DOSEN
                                                ADD JABATAN VARCHAR (30);
l  MENGHAPUS KOLOM
                        ALTER TABLE DOSEN DROP JABATAN;
l  MENGUBAH TIPE KOLOM
                        ALTER TABLE DOSEN ALTER COLUMN NAMA VARCHAR (40);

Membuat Diagram
l  Dari Interprise Manager
l  Pada Database anda  - Diagrams
l  Klik Kanan – New Database Diagrams
l  Next
l  Pilih Tabel yang akan dilihat dalam diagram
l  Tekan Add>
l  Next - Finish












ARTIKEL TERKAIT:

0 komentar:

Post a Comment

Blog Archive

Powered by Blogger.

Contributors

Followers