How to get a unique match pair from many tables? - sql-server

I want to create a unique pair with one mentor and one mentee. I only want to have a mentor in only one pair and a mentee to belong to only one pair.
These are the tables that I have now involved in the pair:
create table member(
MemberID int not null primary key Identity (1,1),
MemberFName varchar(255),
MemberLName varchar(255),
MemberStreet varchar(255),
MemberCity varchar(255),
MemberState varchar(20),
MemberZip varchar(10),
MemberPhone bigint,
MemberEmail varchar(255),
MemberDOB date,
MemberGender char(1),
MemberChildren int,
MemberStartDate date,
CountryID int not null foreign key references country
) -- records all member information
create table mentor(
MentorID int not null primary key Identity (1,1),
MemberID int not null foreign key(MemberID) references member,
MentorOnlineSynch char(1),
MentorOnlineAsynch char(1),
MentorPhoneSkype char(1),
MentorFace char(1)
) -- mentor mentoring preferences
create table mentee(
MenteeID int not null primary key Identity (1,1),
MemberID int not null foreign key(MemberID) references member,
MenteeOnlineSynch char(1),
MenteeOnlineAsynch char(1),
MenteePhoneSkype char(1),
MenteeFace char(1)
) -- mentee mentoring preferences
create table pair(
PairID int not null primary key Identity (1,1),
MentorID int foreign key(MentorID) references mentor,
MenteeID int foreign key(MenteeID) references mentee,
PairStartDate date,
PairEndDate date
) -- pair details which include mentor and mentee id's and the date they were paired
This is what creates Pairs now; however, it allows mentors and mentees to be in multiple pairs:
insert into Pair (mentorid, menteeid, pair.pairstartdate)
select mentor.mentorid, mentee.menteeid, getdate()
from mentor, mentee
left join pair on pair.menteeid = mentee.menteeid
where mentor.menteeonlinesynch = 'Y'
and mentee.mentoronlinesynch ='Y'
and mentor.menteeonlinesynch = mentee.mentoronlinesynch
and mentor.memberid !=m entee.memberid
Any ideas on how to fix it to allow a mentor to be in only one pair and a mentee to be in only one pair?

Related

There is already an object named '...' in the database. Where Can I find the request?

I have made a new request and create a new table and this error message happened.
There is already an object named 'Employe' in the database.
How can I fix this and can anyone tell me where I can find this request? Thank you
It's a database about a market there is client order.
CREATE DATABASE TP1DB
CREATE TABLE Employe(
matricule INT IDENTITY(1,1),
nom VARCHAR(20),
prenom VARCHAR(20),
poste VARCHAR(20),
date_embauche DATE,
date_naissance DATE,
pays VARCHAR(20),
ville VARCHAR(20),
PRIMARY KEY(matricule),
)
INSERT INTO Employe
VALUES ('Paré', 'Éric','Vendeur', '23-12-2020', '01-12-1985', 'Montréal', 'Canada');
CREATE TABLE Client(
numero_client INT IDENTITY(1,1),
nom VARCHAR(20),
pays VARCHAR(20),
ville VARCHAR(20),
PRIMARY KEY(numero_client)
)
CREATE TABLE Commande(
no_commande INT IDENTITY(1000,1),
date_envoi DATE,
date_commande DATE,
ville_livraison VARCHAR(20),
pays_livraison VARCHAR(20),
code_postal_livraison VARCHAR(20),
a_livrer_avant DATE,
matricule INT,
numero_client INT,
PRIMARY KEY(no_commande),
FOREIGN KEY (matricule) REFERENCES Employe(matricule),
FOREIGN KEY (numero_client) REFERENCES Client(numero_client)
)
CREATE TABLE Categorie(
no_categorie INT IDENTITY(100,100),
nom_categorie VARCHAR(20),
PRIMARY KEY(no_categorie)
)
INSERT INTO Categorie
VALUES ('Tablette');
CREATE TABLE Fournisseur(
no_fournisseur INT IDENTITY(1,1),
nom_fournisseur VARCHAR(20),
pays VARCHAR(20),
ville VARCHAR(20),
no_commande INT,
PRIMARY KEY(no_fournisseur)
)
INSERT INTO Fournisseur
VALUES ('Samsung', 'États-Unis', 'Boston');
CREATE TABLE Produit(
no_produit INT IDENTITY(1,1),
nom_produit VARCHAR(20),
prix_unitaire MONEY,
unites_en_stock INT,
no_fournisseur INT,
no_categorie INT,
PRIMARY KEY(no_produit),
FOREIGN KEY (no_fournisseur) REFERENCES Fournisseur(no_fournisseur),
FOREIGN KEY (no_categorie) REFERENCES Categorie(no_categorie)
)
INSERT INTO Produit
VALUES ('Galaxy tab2', 249, 9, 1),
('Sumsung 15', 399, 0, 1 );
The Employe table already exists in your database. You can use the Select * From Employe query to see what it contains. If it's a useless table, you can delete it and re-run your code to create it. If you want to delete it, you can use DROP TABLE Employe
If you are using SQL Server Management Studio, you can use the Object Explorer window tree to see the contents of a database

i need Database table creating

i created my tables and I'm stuck at the last one,
here is the tables that been created correctly
CREATE TABLE Staff (
Staff_ID INT NOT NULL PRIMARY KEY,
First_Name VARCHAR(50),
Last_Name VARCHAR(50),
Username VARCHAR(10),
Password VARCHAR(10),
Address VARCHAR(30)
)
CREATE TABLE Category (
Category_ID INT NOT NULL PRIMARY KEY,
Name VARCHAR(30)
)
CREATE TABLE Author (
Author_ID INT NOT NULL PRIMARY KEY,
First_Name VARCHAR(50),
Last_Name VARCHAR(50),
Birth_Place VARCHAR(30),
Birth_Date DATE
)
CREATE TABLE Publisher (
Publisher_ID INT NOT NULL PRIMARY KEY,
Name VARCHAR(50)
)
and this is the one I'm getting an error :
CREATE TABLE Book (
Book_ID INT NOT NULL PRIMARY KEY,
Title VARCHAR(50),
Edition INT(30),
Year_Published INT(4),
FOREIGN KEY (Publisher_ID) REFERENCES Publisher(Publisher_ID),
FOREIGN KEY (Author_ID) REFERENCES Author(Author_ID),
FOREIGN KEY (Category_ID) REFERENCES Category(Category_ID)
)
the error says:
"ORA-00907: missing right parenthesis"
INT can not have a scale associated with it so YEAR_PUBLISHED and EDITION are incorrect definitions.
I believe that, generally, you would be better off sticking to NUMBER for numeric datatypes, eg NUMBER(4), NUMBER(30).
In the database the INT datatype is simply a sub-type of NUMBER so you aren't gaining anything by using it:
type NUMBER is NUMBER_BASE;
subtype INTEGER is NUMBER(38,0);
subtype INT is INTEGER;
If you want to see the definitions for the various 'other' numeric datatypes take a look at the SYS.STANDARD package.
The INT data type does not have a precision.
You also need to define the Publisher_ID, Author_ID and Category_ID columns.
It is good practice to name your constraints.
A PRIMARY KEY column is both NOT NULL and UNIQUE so you do not need to include a second NOT NULL constraint.
Like this:
CREATE TABLE Book (
Book_ID INT CONSTRAINT Book__Book_id__PK PRIMARY KEY,
Title VARCHAR(50),
Edition INT,
Year_Published INT,
Publisher_ID INT CONSTRAINT Book__Publisher_ID__FK REFERENCES Publisher(Publisher_ID),
Author_ID INT CONSTRAINT Book__Author_id__FK REFERENCES Author(Author_ID),
Category_ID INT CONSTRAINT Book__category_ID__FK REFERENCES Category(Category_ID)
);

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');

foreign key references to a table problem here

create database Exer4
use Exer4
create table customer (
cus_code int,
constraint PK_customer primary key (cus_code),
cus_Lname varchar(20),
cus_Fname varchar (30),
cus_intial varchar (2),
cus_areacode int,
cus_phone int,
cus_balance float
)
create table charter (
char_trip int,
constraint PK_charter primary key (char_trip),
char_date date,
ac_number varchar(5),
foreign key(ac_number) references aircraft,
char_destination varchar(5),
char_distance float,
char_hours_flown float,
char_hours_wait float,
char_fuel_gallons float,
cus_code int,
foreign key(cus_code) references customer
)
create table aircraft(
ac_number varchar(5),
constraint PK_aircraft primary key(ac_number),
mod_code varchar(10),
foreign key(mod_code) references model,
ac_itaf varchar(10),
ac_tiel varchar(10),
ac_tier varchar(10),
)
create table model(
mod_code int,
constraint PK_model primary key(mod_code),
mod_manufacturer varchar(10),
mod_name varchar(10),
mod_seats int,
mod_chg_mile int,
)
create table crew (
char_trip int,
emp_num int,
constraint PK_crew primary key (char_trip,emp_num),
foreign key (char_trip) references charter,
foreign key(emp_num) references employee,
crew_job varchar(10)
)
create table rating (
rtg_code varchar(5),
rtg_name varchar (30),
constraint PK_rating primary key (rtg_code)
)
create table employee (
emp_num int,
constraint PK_employee primary key (emp_num),
emp_title varchar (4),
emp_lname varchar (20),
emp_fname varchar (30),
emp_initial varchar (2),
emp_dob date,
emp_hire_date date,
)
create table pilot (
emp_num int,
pl_license varchar (3),
pl_ratings varchar (30),
pl_med_type int,
pl_med_date date,
pl_pt135_date date,
constraint PK_pilot primary key (emp_num)
)
why is that when i reference to "aircraft" it said invalid table??
what is wrong with my code??
You have to specify which field to reference:
foreign key (mod_code) references model (mod_code),
The foreign key constraint consists of 2 parts:
ALTER TABLE aircraft
ADD CONSTRAINT fk_aircraft_model
FOREIGN KEY (mod_code) -- here you specify the field(s) to reference from in the aircraft table
REFERENCES model (mod_code) -- here you specify the field(s) to reference to in the model table
You are creating the tables in the wrong order. charter references aircraft but create table aircraft doesn't appear until later.

Database schema for a school

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.

Resources