Вы находитесь на странице: 1из 3

/*membuat database baru*/ (di tentukan letak data dan ukuran databasenya)

create database latihan on (name=latihan_dat, filename='D:\riswanto_IK446\latihan_SQL\latihan_dat.mdf', size=10mb, maxsize=15mb, filegrowth=20%) log on (name=latihan_log, filename='D:\riswanto_IK446\latihan_SQL\latihan_log.ldf', size=10mb, maxsize=15mb, filegrowth=20%)

(settingan default) create database inventory use inventory /*membuat tabel*/ create table customer (customer_id varchar(10) constraint kdcus primary key,cusomer_name varchar(30), address nvarchar (35),city varchar(35),postal_code int,phone nvarchar(16)) select * from customer /*memasukkan data dalam tabel*/ insert into customer values ('cs-001','radit','pondok indah','jakarta','10000','085712345678') insert into customer values ('cs-002','prisa','perumnas 1','tangerang','15000','085245617895') insert into customer values ('cs-003','Naruto','pondok sejahtera','Tangerang','15000','021-92586547') insert into customer values ('cs-004','doraemon','kutabumi','Tangerang','15000','021-55856595') insert into customer values ('cs-005','sponge bob','kentucky','United Kingdom','15000','085544875485') /*menampilkan data berdasarkan kriteria "and"*/ select customer_id,cusomer_name,city,address address='kutabumi'

from

customer

where

city='tangerang'

and

/*menampilkan data berdasarkan urutan (ascending/descending)*/ select customer_id,cusomer_name,city from customer where city='tangerang' order by cusomer_name asc /*menampilkan data berdasarkan huruf depan atau huruf belakang*/ select customer_id,cusomer_name,city from customer where cusomer_name like '%b' create table product (product_id nvarchar(20) constraint kdprod primary key ,product_name varchar (25), unit_price money,unit_instock int) select * from product insert into product values ('a-001','keyboard+mouse',25000,50) insert into product values ('a-002','monitor',700000,25) insert into product values ('a-003','processor',1000000,45) insert into product values ('a-004','flash disk',85000,100) insert into product values ('a-005','cassing',500000,40)

select product_id,product_name,unit_instock,unit_price,unit_instock*unit_price as total from product /*menampilkan data dengan kriteria "antara"*/ select product_id,product_name,unit_instock,unit_price,unit_instock*unit_price as total from product where unit_instock >=10 and unit_instock <=40 create table pesanan (order_id nvarchar(10) constraint kdord primary key, customer_id varchar(10),order_date datetime) select*from pesanan insert into pesanan values ('OR-001','cs005','2011-06-15') insert into pesanan values ('OR-002','cs-002','2011-06-23') insert into pesanan values ('OR-003','cs-004','12 feb 2011') insert into pesanan values ('OR-004','cs-004','12/12/2011') insert into pesanan values ('OR-005','cs-002','12/09/2011') /*menghapus data dalam tabel*/ delete from pesanan where order_id='OR-001' or order_id='OR-002'

select customer_id, cusomer_name,city from customer where customer_id in (select customer_id from pesanan where year (order_date)=2011 and month (order_date)=2) select order_id,customer_id,cusomer_name,city, order_date from pesanan where customer_id='cs-004' select distinct city from customer select customer_id,cusomer_name,city from customer order by cusomer_name select sum(unit_instock) as total, avg(unit_instock)as rata_rata, min(unit_instock)as terendah, max(unit_instock)as tertingi from product /*mengupdate isi table*/
update (nama tabel) set (nama colom=yang akan diganti) where (kriteria)

contoh:
update pelanggan set kota='tangerang' where namapelanggan='spongebob' (note: dalam 1 row bisa lebih dari 1 yang di update)

/*menampilkan isi table berelasi dengan perintah join*/


select penjualan.[nomorfaktur], penjualan.[pelangganid],detail_penjualan.[barangid],detail_penjualan.[jumlah_ barang], detail_penjualan.[jumlah_uang] from penjualan join detail_penjualan on penjualan.[nomorfaktur]=detail_penjualan.[nomorfaktur]

/*membuat view permanen*/


create view v1tabel ([nama_barang],[harga]) as select [nama_barang],[harga] from barang where username='riswan' select * from v1tabel

/*mengupdate view*/
alter view v1tabel as select [nama_barang],[harga],[username] from barang where username='dyra' create view vtampil as select penjualan.[tgl_faktur],detail_penjualan.[barangid],detail_penjualan.[jumlah_u ang] from penjualan,detail_penjualan where penjualan.[nomorfaktur]=detail_penjualan.[nomorfaktur]

Вам также может понравиться