Database schema for a school - sql-server

Here is the database query needed to create the database:
create table Area
(
AreaId int primary key identity(1,1),
Name nvarchar(64) not null
)
create table Level
(
LevelId int primary key identity(1,1),
Name nvarchar(32) not null,
Principle nvarchar(512) not null
)
create table Subject
(
SubjectId int primary key identity(1,1),
AreaId int foreign key references Area(AreaId),
LevelId int foreign key references Level(LevelId),
Name nvarchar(32) not null,
Abbreviation nvarchar(16)
)
create table StaffType
(
StaffTypeId int primary key identity(1,1),
Name nvarchar(64) not null
)
create table Staff
(
StaffId int primary key identity(1,1),
StaffTypeId int foreign key references StaffType(StaffTypeId),
Name nvarchar(128) not null,
LastNameFather nvarchar(256) not null,
LastNameMother nvarchar(256) not null,
DateOfBirth datetime,
PlaceOfBirth nvarchar(256),
Sex nvarchar(8) not null,
Carnet nvarchar(64),
Telephone nvarchar(64),
MobilePhone nvarchar(64),
Address nvarchar(256),
FatherName nvarchar(256),
MotherName nvarchar(256),
FatherContactNumber nvarchar(64),
MotherContactNumber nvarchar(64),
FatherPlaceOfWork nvarchar(64),
MotherPlaceOfWork nvarchar(64),
DateOfHiring datetime,
YearsOfService int,
Formation nvarchar(128)
)
create table Grade
(
GradeId int primary key identity(1,1),
Name nvarchar(32) not null,
LevelId int foreign key references Level(LevelId),
Observation nvarchar(256)
)
create table GradeInstance
(
GradeInstanceId int primary key identity(1,1),
StaffId int foreign key references Staff(StaffId),
GradeId int foreign key references Grade(GradeId),
Name nvarchar(32) not null,
Year datetime
)
create table Student
(
StudentId int primary key identity(1,1),
RUDE int,
Name nvarchar(64) not null,
LastNameFather nvarchar(256) not null,
LastNameMother nvarchar(256) not null,
DateOfBirth datetime not null,
PlaceOfBirth nvarchar(128),
Sex nvarchar(8),
Carnet nvarchar(32),
Telephone nvarchar(64),
MobilePhone nvarchar(64),
Address nvarchar(256),
FatherName nvarchar(512),
MotherName nvarchar(512),
FatherMobilePhone nvarchar(64),
MotherMobilePhone nvarchar(64),
FatherProfession nvarchar(64),
MotherProfession nvarchar(64),
FatherPlaceOfWork nvarchar(256),
MotherPlaceOfWork nvarchar(256),
Observations nvarchar(3000)
)
create table StudentInstance
(
StudentInstanceId int primary key identity(1,1),
GradeInstanceId int foreign key references GradeInstance(GradeInstanceId),
StudentId int foreign key references Student(StudentId)
)
create table StudentGradeReport
(
StudentGradeReportId int primary key identity(1,1),
StudentInstanceId int foreign key references StudentInstance(StudentInstanceId),
SubjectId int foreign key references Subject(SubjectId),
FirstTrimester int,
SecondTrimester int,
ThirdTrimester int,
FinalGrade int
)
If you find an attribute that should be null checked, please disregard it. I've gone over this with the client and they want certain things to left blank if so chosen by the end user.
My main concern when designing this database was how to associate a student with a grade here and now, yet keep a record for previous years and manage to see what grade he got back in 2009. See?
I think I've done a good job but you never know - the hivemind here will probably find a flaw and I'd love some feedback.
No longer using SQLite, but MSSQL.
I've tried to normalize the database as much as I could, but not religiously, just as much as it took to make it 'click'.
Before a Student, was directly related to a Classroom (Grade) instance. Now I create 'instances' of Students and assign them to a Classroom (Grade) instance. Each instance has a date saying which year it belongs to. This helps me keep a record of previous years without dirty hacks.

create table Area
(
AreaId int primary key identity(1,1),
Name nvarchar(64) not null
)
"Name" should be UNIQUE.
create table Level
(
LevelId int primary key identity(1,1),
Name nvarchar(32) not null,
Principle nvarchar(512) not null
)
"Name" should be UNIQUE.
create table Subject
(
SubjectId int primary key identity(1,1),
AreaId int foreign key references Area(AreaId),
LevelId int foreign key references Level(LevelId),
Name nvarchar(32) not null,
Abbreviation nvarchar(16)
)
Odds are good that one or more of these apply. Impossible to tell without representative sample data.
The set {AreaId, LevelId} should be
UNIQUE.
"Name" should be UNIQUE.
"Abbreviation" should be UNIQUE.
I'm running out of time.
create table StaffType
(
StaffTypeId int primary key identity(1,1),
Name nvarchar(64) not null
)
"Name" should be UNIQUE.
More later if I have time.

Related

How can I reference 2 different columns in 2 different tables simultaneously

This is an assignment and I have been given this diagram for reference - https://imgur.com/gallery/wRkArmQ
There are other tables that I will include but the issue seems to be with the tables section and time_slot. In the diagram, they reference each other but I don't know how to. Any comments are helpful! I am using Oracle btw, here is the code -
create table department (
dept_name varchar(15) primary key,
building varchar(10),
budget int
);
create table instructor (
id varchar(10) primary key,
name varchar(15) not null,
dept_name varchar(20) not null
references department(dept_name) on delete cascade,
salary int not null
);
create table course (
course_id varchar(10) primary key,
title varchar(10),
dept_name varchar(15)
references department(dept_name) on delete cascade,
credits int
);
create table classroom (
building varchar(10) not null,
room_no varchar(5) not null,
capacity int not null,
primary key (building, room_no)
);
create table section (
course_id varchar(10) not null
references course(course_id),
sec_id varchar(10) not null,
semester varchar(10) not null,
year varchar(10) not null,
building varchar(10) not null
references classroom(building),
room_no varchar(5) not null
references classroom(room_no),
time_slot_id varchar(5) not null
references time_slot(time_slot_id),
primary key (course_id, sec_id, semester, year)
);
create table time_slot (
time_slot_id varchar(5) not null
references section(time_slot_id),
day varchar(10) not null,
start_time date not null,
end_time date not null,
primary key (time_slot_id, day, start_time)
);
create table teaches (
id varchar(10) primary key
references instructor(id) on delete cascade,
course_id varchar(15) not null
references section(course_id),
sec_id varchar(5) not null
references section(sec_id),
semester varchar(15)
references section(semester),
year int not null
references section(year)
);
create table student (
id varchar(10) not null primary key,
name varchar(20) not null,
dept_name varchar(15) not null
references department(dept_name) on delete cascade,
tot_cred int not null
);
create table advisor (
s_id varchar(10) primary key
references student(id) on delete cascade,
i_id varchar(6)
references instructor(id) on delete cascade
);
create table prereq (
course_id varchar(10)
references course(course_id) on delete cascade,
prereq_id varchar(10)
references course(course_id) on delete cascade,
primary key (course_id, prereq_id)
);
create table takes (
id varchar(10) not null
references student(id) on delete cascade,
course_id varchar(10) not null
references section(course_id) on delete cascade,
sec_id varchar(10) not null
references section(sec_id) on delete cascade,
semester varchar(10) not null
references section(semester) on delete cascade,
year int not null
references section(year) on delete cascade,
grade varchar(10) not null,
primary key (id, course_id, sec_id, semester, year, grade)
);
This is the output -
Table created.
Table created.
Table created.
Table created.
ORA-02270: no matching unique or primary key for this column-list
ORA-00942: table or view does not exist
ORA-00942: table or view does not exist

How to create tables using below statement

Working on small ASP.Net MVC project, and got requirement from business analyst who gave me PDF file with below SQL staff to create in database.
Looking on how to achieve result from the below SQL question.
By the way should all be created using T-SQL
Student - Table
studentNo Identity column INT NOT NULL PK
lastName VARCHAR(30) NOT NULL
firstName VARCHAR(30) NOT NULL
gender CHAR NOT NULL
phoneNumber VARCHAR(20) NOT NULL
salaryAmount INT,
departmentID INT FK
Department - Table
departmentID Identity Column INT NOT NULL PK
name VARCHAR(40) NOT NULL
priceCentreID INT FK
PriceCentre - Table
priceCentreID
name VARCHAR(50)
accountID varchar(50)
As I understand you want help to create the sql code to generate the tables you provided?
Below is the code to create the tables and the relations that you are looking for.
code:
CREATE TABLE PriceCentre(
priceCenterID INT PRIMARY KEY,
name VARCHAR(50),
accountID VARCHAR(50)
);
GO
CREATE TABLE Department(
departmentID INT NOT NULL PRIMARY KEY,
name VARCHAR(40) NOT NULL,
priceCenterID int FOREIGN KEY REFERENCES PriceCentre (priceCenterID)
)
GO
CREATE TABLE Student(
studentNo INT NOT NULL PRIMARY KEY,
lastName VARCHAR(30) NOT NULL,
firstName VARCHAR(30) NOT NULL,
gender CHAR NOT NULL,
phoneNumber VARCHAR(20) NOT NULL,
salaryAmount INT,
departmentID INT FOREIGN KEY REFERENCES Department(departmentID)
);

Associative table is erroring

I have a school project where I need to create a database in SQL Server. I am currently having issues entering in my tables.
I have successfully entered in the first two tables, but the associative table is erroring, as are other tables following.
create table Home
(
HomeID INT Identity (1,1) Primary key,
Address1 varchar(250) not null,
City varchar(25) not null,
State1 varchar(25) not null,
Zipcode CHAR(5) not null,
SmokeDetectors INT,
Co2Detectors INT,
SecuritySystem CHAR(1),
TotalSqFt int,
Rooms INT,
AdditionalBuilding CHAR(1),
AssessedValue DEC(10,2),
YearAssessed char(4),
CompanyID int not null,
)
create table MortgageCompany
(
CompanyID INT Identity (1,1) Primary key,
CompanyName varchar(100) not null,
Address1 varchar(250),
City varchar(25),
State1 varchar(25),
Zipcode char(5),
HomeID int not null
)
create table HomeMortgageCompany
(
HomeID INT,
CompanyID INT,
foreign key CompanyID references MortgageCompany(CompanyID),
foreign key HomeID references Home(HomeID),
Primary key (HomeID,CustomerID)
)
create table Loan
(
LoanID INT Identity (1,1) Primary key,
LoanNumber int not null,
MortgageAmount dec(10,2),
CompanyID int
foreign key CompanyID references MortgageCompany(CompanyID)
)
create table PersonalProperty
(
PropertyID INT Identity (1,1) Primary key,
ItemName int,
Type1 varchar(150) not null,
Value1 Dec(10,2) not null,
HomeID int
foreign key HomeID references Home(HomeID)
)
create table Policy
(
PolicyID INT Identity (1,1) Primary key not null,
PolicyYear char(4),
PremiumCharged dec(10,2),
EffectiveDate date not null,
ExpirationDate date not null,
ReviewDateTime datetim,
Decision char(8),
HomeID int
foreign key HomeID references Home(HomeID)
)
create table Bank
(
AccountID int Identity (1,1) Primary Key,
BankName varchar(150) not null,
AccountNumber int not null,
RoutingNumber int not null,
Amount Dec(10,2),
PolicyID int not null
)
create table PolicyBank
(
PolicyID int,
AccountID INT,
foreign key PolicyID references Policy(PolicyID),
foreign key AccountID references Bank(AccountID),
Primary Key (PolicyID, AccountID)
)
Create table PolicyHolder
(
SSN char(9) Primary key not null,
FirstName varchar(50) not null,
LastName varchar(50) not null,
)
create table PolicyPolicyHolder
(
SSN char(9),
PolicyID INT,
foreign key PolicyID references Policy(PolicyID),
foreign key SSN references PolicyHolder(SSN),
Primary key (SSN, PolicyID)
)
create table HomePolicyHolder
(
SSN char(9),
HomeID Int,
foreign key HomeID references Home(HomeID),
foreign key SSN references PolicyHolder(SSN),
Primary key (SSN, HomeID)
)
create table Employment
(
SSN char(9) Primary Key,
EmployerFirstName varchar(50)not null,
EmployerLastName varchar(50) not null,
Address1 varchar(250),
City varchar(25),
State1 varchar(25),
Zipcode char(5),
StartDate date,
CurrentSalary dec(10,2) not null,
JobTitle varchar (75),
RecordExtractionDate date not null,
foreign key SSN references PolicyHolder(SSN)
)
Create table Document
(
DocumentID INT Identity (1,1) Primary key,
DocumentType varchar(50),
DateTimeSent datetime not null,
SSN char(9) not null,
PolicyID int,
foreign key PolicyID references Policy(PolicyID)
)
create table PolicyHolderDocument
(
DocumentID int,
SSN char(9),
foreign key DocumentID references Document(DocumentID),
foreign key SSN references PolicyHolder(SSN)
);
For the third table definition:
create table HomeMortgageCompany (
HomeID INT,
CompanyID INT,
foreign key CompanyID references MortgageCompany(CompanyID),
foreign key HomeID references Home(HomeID),
Primary key (HomeID,CustomerID)
)
Problems with this code:
the column name in the defina of the foreign key needs to be surrounded with parentheses
the primary key refers to column CustomerID, but there is no column in the table
Presumably, you meant:
create table HomeMortgageCompany (
HomeID INT,
CompanyID INT,
foreign key (CompanyID) references MortgageCompany(CompanyID),
foreign key (HomeID) references Home(HomeID),
Primary key (HomeID,CompanyID)
)

Using the last inserted record's ID as a foreign key

I am making a database which links together two tables via the primary key of the first table. The one with the primary key which links the two is created first but how do i make the second record get the ID of the record I just created?
create table Person
(
Person_ID int IDENTITY(100000,1) primary key,
First_Name varchar(20) not null,
Last_Name varchar(20) not null,
)
create table Employee
(
Employee_ID int identity(100000,1) primary key,
Person_ID int references Person(Person_ID),
Employee_Type varchar(10)
)
insert into Person(First_Name, Last_Name) values ('Michael', 'Chu');
insert into Employee(Person_ID, Employee_Type,) values (????????, 'Admin');
I've had a look at the 'last()' function but not really sure how to utilise that. Other then that, I have no idea. Can someone help me out or guide me in the right direction.
try this:
create table Person
(
Person_ID int IDENTITY(100000,1) primary key,
First_Name varchar(20) not null,
Last_Name varchar(20) not null,
)
create table Employee
(
Employee_ID int identity(100000,1) primary key,
Person_ID int references Person(Person_ID),
Employee_Type varchar(10)
)
DECLARE #myID AS INT
insert into Person(First_Name, Last_Name) values ('Michael', 'Chu');
SET #myID = ##IDENTITY
insert into Employee(Person_ID, Employee_Type,) values (#myID , 'Admin');

how to refer a computed column in MS SQL?

I have written a query as follows.
create table registration(
id int identity(1000,1) ,
first_name varchar(45) unique,
sur_name varchar(45),
address_line1 varchar(45),
address_line2 varchar(45),
state varchar(45),
city varchar(45),
email_id varchar(45),
contact_no varchar(45),
date_of_birth date,
apply_type varchar(45),
qualification varchar(45),
gender varchar(45),
password as CONVERT(VARCHAR(4),DATEPART(dd,GETDATE())) +
substring(CONVERT(VARCHAR(4),DATENAME(mm,GETDATE())),1,3) +
convert(varchar(4),FLOOR(RAND(CHECKSUM(NEWID()))*(999-100)+100)),
hint_question varchar(50),
hint_answer varchar(50),
user_id as substring(apply_type,1,4) + convert(char(45),id)
persisted primary key not null);
create table passport(
id int identity(1000,1),
Passport_Number as substring(Booklet_type,1,2) +
convert(varchar(100),id) persisted primary key not null,
user_id varchar(200) constraint fk_uid foreign key(user_id) references
registration(user_id) on delete cascade,
Type_of_Passport varchar(45),
Type_of_Service varchar(50),
Booklet_type varchar(50),
Address1 varchar(50),
Address2 varchar(50),
City varchar(50),
State varchar(50),
Country varchar(50)n
Pin int,
Number_of_Years int,
Date_Of_Application date,
Issue_Date date,
Amount int,
Reason_for_reissue varchar(50),
Expired_Date date);
But I'm getting the following error:
Column 'registration.user_id' is not the same length or scale as referencing column 'passport.user_id' in foreign key 'fk_uid'. Columns participating in a foreign key relationship must be defined with the same length and scale.
How to rectify this problem?
cast or convert the user_id in registration to varchar(200) ie same as the one in passport
user_id as CONVERT(VARCHAR(200),
substring(apply_type,1,4) +
convert(char(45),id) )
persisted primary key not null);

Resources