Uploaded by dhkcbf

DATABASE DDL and DML

advertisement
Student Management System
Create Database StudentManagement
STUDENT TABLE
create table student
(student_id int not null primary key,
name varchar(25),
age int,
gender varchar(1),
class varchar(2),
secton varchar(1)
)
alter table student
add foreign key (class, section) references class_section(class, section)
STUDENT_SUBJECT TABLE
Create table Student_Subject
(Student_id int not null,
sub varchar(3)
primary key (student_id, subject)
)
alter table student_subject
add foreign key (student_id) references student(student_id)
alter table student_subject
add foreign key (sub) references subjects(sub)
SUBJECTS TABLE
CREATE TABLE SUBJECTS
(SUB VARCHAR(3) PRIMARY KEY,
CREDIT_HOURS INT)
CLASS_SECTION TABLE
CREATE TABLE CLASS_SECTION
(CLASS VARCHAR(3),
SECTION VARCHAR(1),
CLASS_TEACHER VARCHAR(25)
PRIMARY KEY (CLASS, SECTION))
Alter Commands
alter table student_subject
drop column sub
alter table student_subject
add sub varchar(3) not null
ALTER TABLE Student_Subject
ADD PRIMARY KEY (Student_id, sub)
INSERT, UPDATE, DELETE AND SELECT COMMANDS
insert into student (student_id, name, age, class, section)
values (1002, 'Karim', 22, 'XI', 'B')
insert into class_section (class, section, class_teacher)
values ('XI', 'B', 'Amin')
select * from student
select student_id, name from student
update student
set age = 32, name = 'Aleem'
where student_id = 1002
delete from student where student_id = 1002
select student_id, name, CLASS_TEACHER, student.class
from student INNER JOIN CLASS_SECTION
ON student.class= CLASS_SECTION.CLASS AND
student.section = CLASS_SECTION.SECTION
CRICKET
Example 1
--create table players
--(playerid int primary key,
--pname varchar(20))
--create table team_player
--(teamid int,
--playerid int
--primary key (teamid, playerid))
--alter table team_player
--add foreign key (teamid) references team(teamid)
--alter table team_player
--add foreign key (playerid) references players(playerid)
Example 2
--insert into team (teamid, country) values (1005, 'ENGLAND')
--INSERT INTO PLAYERS (PLAYERID, PNAME) VALUES (110, 'TAILYOR')
--insert into team_player (teamid, playerid) values (1004, 108)
Example 3
--select * from players
--select * from team
--select * from team_player
SELECT Country, pname FROM
Team INNER JOIN Team_Player
ON Team.Teamid = Team_player.Teamid
INNER JOIN Players
ON Players.PlayerID = Team_player. Playerid
--update team_player
--set playerid = 107
--where playerid = 108 and teamid = 1004
delete from team_player
where playerid=107 and teamid = 1004
Example 4
--drop table team_player
--alter table players
--add teamid int
--alter table players
--add foreign key (teamid) references team(teamid)
--select country, count(*)
--FROM team INNER JOIN players
--ON team.teamid = players.teamid
--group by country
Example 5
SELECT TEAM_ID, AVG(PLAYER_AGE) AVERAGE_AGE, COUNT(*) TOTAL
MIN(PLAYER_AGE) mini, SUM(PLAYER_AGE) TOTALAGE
FROM PLAYERS GROUP BY TEAM_ID
, MAX(PLAYER_AGE) MAXI,
Download