Tuesday, May 31, 2011

Database Tutorial

create table student(
id int,
name varchar(255),
marks int,
PRIMARY KEY(id)
);

create table lecturer(
id int primary key,
name varchar(255)
);

create table batch(
id int primary key,
description varchar(255)
);

One to One
alter table batch add column lectId int;

alter table batch add foreign key(lectId) references lecturer(id);

alter table lecturer add column batchId int;

alter table lecturer add foreign key(batchId) references batch(id);

One to Many

alter table student add column batchid int;

alter table student add foreign key(batchId) references batch(id);

Turn off Foreign Key

set foreign_key_checks=0;

Add data to batch

insert into batch
values (1,'.NET,1),(2,'Java',2),(3,'QA',3),(4,'PHP',4),(5,'Pega',5),(6,'Intern',6),(7,'C++',7);

insert into lecturer
values (1,'Madu',1),(2,'Shyam',2),(3,'Nimali',3),(4,'JJ',4),(5,'Sanjay',5),(6,'Mihiri',6),(7,'Keith',7);

insert into student(id,name,marks)
values (1,'Kalani',90),(2,'Nuresha',90),(3,'Sashikala',90),(4,'Nadeesha',90),(5,'Suresh',99),(6,'Lahiru',95),(7,'Sandi',80),(8,'Sheron',85),(9,'CJ',77),(10,'Achini',70),(11,'Chanks',65);

No comments: