Tutorial 2: sql

create table with referential integrity

create table barang

CREATE TABLE barang (
kd_brg varchar(4) NOT NULL,
nm_brg varchar(25) NOT NULL,
harga_beli double NOT NULL,
harga_jual double NOT NULL,
stok double NOT NULL,
PRIMARY KEY (kd_brg))
ENGINE=MYISAM;

create table pembelian_detil

CREATE TABLE pembelian_detil (
no_beli varchar(4) NOT NULL,
kd_brg varchar(4) NOT NULL,
harga_beli double NOT NULL,
jumlah double NOT NULL,
PRIMARY KEY (no_beli, kd_brg))
ENGINE=MYISAM;

add referential integrity

ALTER TABLE pembelian_detil
ADD CONSTRAINT FOREIGN KEY (kd_brg)
REFERENCES barang (kd_brg);

create table penjualan_detil

CREATE TABLE penjualan_detil (
no_jual varchar(4) NOT NULL,
kd_brg varchar(4) NOT NULL,
harga_jual double NOT NULL,
jumlah double NOT NULL,
PRIMARY KEY (no_jual, kd_brg))
ENGINE=MYISAM;

add referential integrity

ALTER TABLE penjualan_detil
ADD CONSTRAINT FOREIGN KEY (kd_brg)
REFERENCES barang (kd_brg);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s