Minggu, 22 Mei 2011

contoh membuat database dan tabel pada sql server

create database db080010197

on (name= N'db080010197_data',
filename = N'd:\k081\db080010197.mdf')
log on (name= N'db080010197_log',
filename = N'd:\k081\db080010197.ldf')

use db080010197

create table departemen
(
    Departmentid varchar (5) primary key,
    Departmentnama varchar (55),
    Departmenthead varchar (25)
   
)


insert into departemen values ('0001','dep_perindustrian','')
insert into departemen values ('0002','dep_kepariwisataan','kuncoro')
insert into departemen values ('0003','dep_keuangan','sukarjana')
insert into departemen values ('0004','dep_perhutanan','supriono')
insert into departemen values ('0005','dep_kelautan','mega')


select *from departemen

create table employees
(
    employeeid  varchar (5) primary key,
    employeename varchar (35),
    employeee_gender char (2),
    dateOfBirth datetime,
    hiredDate datetime,
    Departmentid varchar (5),
    stateid  varchar (5)

)

drop table employees
insert into employees values ('001101','putu_denix','L','january 24 1088','march 26 2010','0001','0011')
insert into employees values ('001102','gede_pasek','L','april 28 1988','march 23 2010','0002','0012')
insert into employees values ('001103','benong','L','june 12 1987','march 18 2010','0003','0013')
insert into employees values ('001104','ahmad_dani','L','july 22 1979','march 20 2010','0004','0013')
insert into employees values ('001105','luna_maya','W','april 26 1983','march 22 2010','0005','0012')

select * from employees

alter table employees
add departmentId  varchar (5)
insert into  employees where values departemenid('0001')

alter table employees
add stateId varchar (5)

alter table employees
add constraint fkdepartments foreign key (departments)
references departments(departmentid)on update cascade

alter table employees
add constraint fkstates foreign key (states)
references states(stateid)on update cascade

create table states
(
    stateid  varchar (5) primary key,
    statename varchar (6)
   
)

insert into states values ('0011','honor')
insert into states values ('0012','tetap')
insert into states values ('0013','tetap')

select * from states

alter table departements
add stateid  varchar (5)


alter table departements
add constraint fkstates foreign key (stateid)
references states (stateid)on update cascade

Tidak ada komentar:

Posting Komentar