Sie sind auf Seite 1von 3

Tugas-2: Pengantar SQL

1. Tuliskan query untuk membuat tabel berikut!

2. Tuliskan query untuk membuat constraint untuk masing-masing tabel tersebut!


Tabel Customer: Primary Key(1), Unique(2), Check(2)
Tabel Address: Primary Key(2), Default(1)
Tabel AddressType:Primary Key(1)
3. Tuliskan query untuk merelasikan ketiga tabel tersebut!

Keterangan
1. Tugas 2 dikumpulkan di minggu ke-7 perkuliahan.
2. Bobot tugas 1 sebesar 8% dari 100% mata kuliah ini.
JAWAB
1. USE DATABASE;
CREATE TABLE CUSTOMER
(
CustomerID INT NOT NULL IDENTITY(1,1),
CustomerName varchar (25) NOT NULL,
CustomerGender char (1) not null,
CustomerPhone varchar (15) not null,
CustomerEmail varchar (30) not null
);

USE DATABASE;
CREATE TABLE ADDRES
(
CustomerID NOT NULL IDENTITY(1,1),
AddressType CHAR (1) NOT NULL ,
Address1 VARCHAR (50) IS NULL,
Address2 VARCHAR (20) NOT NULL,
City VARCHAR (20) NOT NULL,
STATE CHAR (2) NOT NULL,
PostalCode CHAR (5) NOT NULL
);
USE DATABASE;
CREATE ADDRESTYPE
(
AddressType CHAR (1) NOT NULL IDENTITY(1,1),
Address VARCHAR (10) NOT NULL
);

2. -TABLE CUSTOMER

USE DATABASE;
ALTER TABLE CUSTOMER
ADD CONSTRAINT PK_TABLE CUSTOMER_CustomerID
PRIMARY KEY (CustomerID);
ALTER TABLE CUSTOMER
ADD CONSTRAINT UQ_TABLE CUSTOMER_ CustomerPhone
UNIQUE (CustomerPhone),
ADD CONSTRAINT UQ_TABLE CUSTOMER_ CustomerEmail
UNIQUE (CustomerEmail);

ALTER TABLE CUSTOMER


ADD CONSTRAINT CK_ TABLE CUSTOMER_CustomerGender
CHECK (GENDER=’M’ OR GENDER=’F’);

-TABLE ADDRESS

USE DATABASE;
ALTER TABLE ADDRESS
ADD CONSTRAINT PK_ TABLE ADDRESS _CustomerID
PRIMARY KEY (CustomerID),
ADD CONSTRAINT PK_ TABLE ADDRESS_STATE
PRIMARY KEY (STATE);
ALTER TABLE ADDRESS
ADD CONSTRAINT DF_ TABLE ADDRESS_ AddressType
DEFAULT (‘ACTIVE’) FOR AddressType;

-TABLE ADDRESTYPE
ADD CONSTRAINT PK_ TABLE ADDRESS _ AddressType
PRIMARY KEY (AddressType);
3. SELECT TABLE CUSTOMER.CustomerID, TABLE ADDRES. CustomerID, TABLE ADDRES.AddressType, TABLE ADDRESTYPE.
AddressType
FROM TABLE CUSTOMER, TABLE ADDRES , TABLE ADDRESTYPE
WHERE TABLE CUSTOMER.CustomerID= TABLE ADDRES. CustomerID AND TABLE ADDRESTYPE.AddressType=, TABLE
ADDRES.AddressType