DBMS: SQL Server 2000
Statement Transact-SQL (DDL)
Create table : pembentukan tabel
Use northwind
create table
masterbarang
(kodebarang int not null,
jenisbarang varchar(20) not null,
merk varchar(10) not null)
Alter Table : modifikasi tabel
Use northwind
Alter table
masterbarang
Add
jumlah char
Alter table
masterbarang
Drop column
jumlah
Alter table
masterbarang
Drop Table : menghapus tabel
Use northwind
Insert : menambahkan baris pd
tabel
Insert into (nama tabel) (nama kolom) values nilai masukan
Use northwind
Insert into
territories
values
(98101,‟seatle‟,2)
Insert into
masterbarang
values
Update : mengubah data dlm tabel
Update nama tabel set nilai yg akan diubah where kondisi
Use northwind
Update
territories
set
territorydescription
= „downtown
seattle‟ where territoryid =98104
Delete : menghapus baris dr tabel
Delete from nama tabel where kondisi
Use northwind
Delete from
territories
where
territoryid = 98104
Delete from
masterbarang
where
Comment
Primary Key
Sebuah tabel seharusnya mempunyai sebuah field atau kombinasi beberapa field yg nilainya scr unik mengidentifikasi sebuah record dlm tabel. Field unik tsb dikenal sbg primary key. Jika menambahkan constraint primary key
pada kolom, SQL Server akan mengecek data dlm kolom yang terlibat. Pengecekannya
meliputi :
Perhatian mengenai Primary Key:
Constraint primary key tidak dapat dihapus
jika diacu oleh tabel lain dengan constraint foreign key
Hanya dapat mendefinisikan 1 buah
constraint primary key dgn 1 kolom atau lebih kolom pd 1 tabel.
Primary Key
Utk 1 kolom :
Create table nama tabel
(nama kolom1 tipe data not null primary key, Nama kolom2 tipe data )
Utk kombinasi 2 kolom atau lebih: Create table nama tabel
(
nama kolom1 tipe data not null, nama kolom2 tipe data not null, Nama kolom3 tipe data,
Contoh:
Create table masterbarang (
kodebarang char(4) not null primary key, Namabarang char(10) not null default
„unknown‟ )
Contoh kombinasi:
Create table bukupengarang (
kodebuku char(4) not null,
Kodepengarang char(4) not null
Primary key (kodebuku, kodepengarang) )
Alter table nama tabel
Add constraint namaconstraint PRIMARY KEY (nama kolom dlm tabel)
Contoh:
Alter table masterbarang
Add constraint kodebarangpk primary key (kodebarang)
Alter table pengarang
Add constraint pengarangpk primary key (kodepengarang)
Insert into masterbarang (
jenisbarang ,
Unique key
Dipakai untuk menjamin tidak ada duplikasi pada
kolom yg tidak membangun primary key.
Konstrain unique dpt diacu oleh konstrain foreign
key.
Jika sebuah filed atau kombinasi beberapa field
bukan primary key dan diharuskan unik.Beberapa konstrain unique dpt didefinisikan dlm sebuah tabel, tetapi pendefinisian primary key hanya boleh ada 1
Jika sebuah field boleh null. Konstrain unique dpt
didefinisikan pd field yg boleh null, tetapi konstrain primary key tidak dapat didefinisikan pd field yg
Foreign Key
Merupakan kolom atau kombinasi beberapa
kolom yg dipakai utk menghubungkan sebuah tabel dgn tabel lainnya.
Sebuah tabel dapat memiliki beberapa
Utk 1 kolom :
Create table nama tabel (
nama kolom1 tipe data not null primary key, Nama kolom2 tipe data,
Nama kolom3 tipe data references nama tabel yg diacu (nama kolom primary key yg diacu) )
Utk kombinasi 2 kolom atau lebih: Create table nama tabel
(
nama kolom1 tipe data not null primary key, nama kolom2 tipe data,
Nama kolom3 tipe data, Nama kolom4 tipe data,
Constraint namaconstraint1 Foreign key (nama kolom4) references nama tabel acuan (namakolom primary key acuan)
Constraint namaconstraint2 Foreign key (nama kolom2) references nama tabel acuan (namakolom primary key acuan)
Contoh:
Create table dosen (
kodedosen char(4) not null primary key, Namadosen char(10),
Kodemk char(4) references tabelkuliah (idmatakuliah)
Create table pembimbingskripsi (
npm char(10) not null primary key, Kodedosen char(4) not null,
Judulskripsi char(30),
Constraint ta1_fk foreign key (npm) references tabelmhs (idmhs),
Constraint ta2_fk foreign key (kodedosen) references tabeldosen (iddosen)
Statement Transact-SQL (DML)
Select : menampilkan data dlm
baris/kolom dari tabel
Select nama kolom from nama
Use northwind
Select
customerid, companyname,
contactname
from
customers
where
(customerid =„alfki‟ or
customerid=„anatr‟)
order by
Menampilkan data dari tabel Author dari
database pubs.
Select country,region from
Distinct key word utk menghilangkan
duplikasi dlm baris hasil.
Jika ada 10 pegawai yg tinggal di kota yg
sama maka saat dipergunakan perintah
Select country from employees maka akan
tampil nama country sebanyak 10x. Jika ingin menampilkan 1 data dari duplikasi data tsb
maka :
Use northwind
Top n key word menyatakan hasil yg
diberikan tidak semua, tetapi hanya n baris pertama dr hasil.
Ditampilkan 5 data karyawan pertama dari
tabel employees. Use northwind
Select top 5 employeeid, firstname from employees
Mengubah Judul Kolom
Utk memudahkan suatu pembacaan dari
suatu hasil perhitungan yg tdk mempunyai judul kolom
Select judulkolom = namakolom from namatabel
Use northwind
Select orderid, unitprice, quantity,
Menampilkan literal (bayar) yg ditampilkan dlm
kolom mandiri (penulisan literal dlm tanda petik tunggal)
Use northwind
Select orderid, „bayar‟, „nilai
Function
Built-in function
Built-in function
telah didefinisikan dlm
Transact-SQL
Terdiri dari
Aggregate, mengoperasikan sejumlah
nilai dengan memberikan hasil nilai
tunggal. Contohnya
AVG,SUM,MIN,MAX,COUNT.
Aggregate biasanya disertakan dlm
Group by clause dari statement
SELECT
Select quantity from “order details”
Select sum(quantity) from “order details” Select min(quantity) from “order details” Select max(quantity) from “order details” Select avg(quantity) from “order details”
Scalar, mengoperasikan nilai tunggal
dengan memberikan hasil nilai
tunggal. Contohnya : MONTH
Contoh select getdate() ;
menghasilkan tanggal dan waktu
sekarang
Use northwind
select bulan=
datename
(mm,hiredate),
tahun=
datepart
(yy,hiredate)from
hiredate adalah nama kolom pd
tabel employees; menghasilkan
nama bulan dan nilai tahun
select
datediff
(
yy
,hiredate,
'2000'
)
as „jangka waktu‟ from employees
menghasilkan nilai jangka waktu
dari thn pd kolom hiredate hingga
thn 2000
Operator
Operator Kisaran (Between atau
Not between , pengambilan
Use northwind
Select employeeid, lastname, firstname, hiredate from employees where hiredate between „1/1/93‟ and ‟12/31/93‟
Use pubs
select city,phone,au_fname from authors where phone between '400„ and '410'
select au_fname,city from authors where city
Operator List, pemilihan nilai yg sesuai
dengan nilai yg ada atau tertera dlm list.
Contoh : select au_fname,city from authors where city in ('salt lake city','vacaville')
Operator String, pemilihan baris yg sesuai
dengan karakter yg digunakan.
Contoh:
nama yg diawali dengan huruf „S‟
use pubsselect au_lname from authors where au_lname like 's%'
nama yg diawali dengan huruf „A‟ dan huruf
kedua dgn k atau n atau b dan huruf ketiga boleh apa saja
Use pubs
select au_fname from authors where au_fname like 'A[knb]%'
nama yg diawali dengan huruf „S‟ dan huruf
kedua tidak boleh k atau n use pubs
select au_fname from authors where au_fname like „S[^kn]%'
Operator Logika,
Use pubs
select au_fname, city from authors where
au_fname='abraham' or not city='oakland'
select au_fname, city from authors where
select au_fname, city from authors where
au_fname='ann' and city ='palo alto' Use northwind
Select employeeid, lastname, firstname, from employees where employeeid in (3,5,8)
Klausa Order By
Klausa Order by, pengurutan
hasil sesuai dengan kolom yg
ditentukan. Contoh: select
au_fname,au_lname from authors
order by au_lname desc
Type data
Kategori Binary :
Binary, varbinary, image,
Kategori Character:
Char,varchar,text
Kategori Date and time
Kategori Decimal
Decimal,numeric
Kategori Floating Point
Float, real
Kategori Integer
Bigint, int, smallint, tinyint
Monetary
Special
Bit, cursor, timestamp,
uniqueidentifier, SQL_variant,
table