Can i use composite primary key here? - sql

Below are my two tables
Student(rno int primary key,name varchar(20))
Fees(id int identity,name varchar(10), amount decimal(10,2), pdate date,
rno int foreign key references Student(rno))
In Fees table [id name rno] gives unique.
So can i create composite primary key on table Fees? or need to add one more table to normalize?
Most of the search on table Fees is based on column rno. So it would be good if i have index on rno.

You can Create Composite Primary Key if you want to be unique set of (id,rno) by below code
create table my_table (
column_a integer not null,
column_b integer not null,
column_c varchar(50),
primary key (column_a, column_b)
);
once you create composite primary key constraint you won't able to insert duplicate combination of rno and id

create one more table of FeesMaster where you entered the which type of fees and its name, if any due, then the price or other details.
This will help in future expansion.
and give reference the FeesMaster id to Fees as FeesID. Always give primary key to auto-increment if it is numeric field.
So your table like this.
Student(rno int primary key with auto-increment,name varchar(20))
FeesMaster(feesid int primary key with auto-increment,name varchar(20), price int, dueprice int) --last 2 is optional or give other detail which work as a master detail
--use this table as a transaction table of both above master table, the logic is
Fees(
id int identity,
rno int foreign key references Student(rno),
feesid id foreign key references FeesMaster(FeesID),
amount decimal(10,2),
pdate date)
Updated :-
Fees
id is primary-key of Fees table which are auto-increment
rno is refernce-key of Student table's rno column
feesid is refernce-key of FeesMaster table's feesid column
So in insert gives the feesid of fees master based on fees name. and to get data, the query looklike
select s.name as studentname, fm.name as feesname
from student s
inner join Fees f on f.rno = s.rno
inner join FeesMaster fm on f.feesid = fm.feesid

Related

Is there a way to relationship between two table which table one has 3 unique keys and table two has 2 unique keys in SQL Server?

I want to create relation between two tables, here's my table structure
Table1:
ID INT PrimaryKey AUTO_INCREMENT,
CountryCode INT UNIQUE,
Division VARCHAR(4) UNIQUE,
SubDivision VARCHAR(10) UNIQUE
Table2:
ID INT PrimaryKey AUTO_INCREMENT,
CountryCode INT UNIQUE,
Division VARCHAR(4) UNIQUE,
ReportNo VARCHAR(10) UNIQUE
Table1:
ID |CountryCode |Division |SubDivision
-------+------------------+--------------+-----------
1 |IDN |A |A-1
2 |IDN |B |B-1
3 |IDN |B |B-2
Table2
ID |CountryCode |Division |ReportNo
-------+------------------+--------------+-----------
1 |IDN |A |Re001
2 |IDN |A |Re002
3 |IDN |B |Re003
I want to create a relationship between those two tables which table2 (CountryCode, Division) refers to table1 (CountryCode, Division).
So when I want to delete in table1 with CountryCode = IDN and Division = A, SQL will prompt error when table2 contains CountryCode = IDN and Division = A.
I had tried to create a relationship between those two tables, but SQL Server always throws this error:
The column in table 'table1' do not match an existing primary key or unique constraint
Can anyone guide me how can I create a relationship between those two tables?
Thanks in advance
You can't do it this way. SQL Server requires a unique constraint (or primary key constraint) on the target of a foreign key - and you have duplicates in the source table.
For this to work, you would need to have a separate table that references all possible (CountryCode, Division) combinations. Actually, your whole schema should be normalized into something like:
-- "top" table that stores the countries
create table countries (
country_id int primary key
name varchar(100)
);
-- the table that stores the divisions
create table divisions (
division_id int primary key,
country_id int references countries(country_id),
name varchar(100)
);
-- the table that stores the subdivisions
-- this corresponds to "table1" in your question
create table subdivisions (
subdivision_id int primary key,
division_id int references divisions(division_id),
name varchar(100)
);
-- the table that stores the reports
-- this corresponds to "table2" in your question
create table reports (
report_id int primary key,
division_id references divisions(division_id),
name varchar(100)
);
You can make the primary keys automatic by using identity columns (which is the SQL Server equivalent for MySQL's AUTO_INCREMENT).
As an example, here is how you would generate the current output that you are showing for the subdivisions:
select
sd.id,
c.name country,
d.name division,
sd.name subdivision
from subdivisions sd
inner join divisions d on d.division_id = sd.division_id
inner join countries c on c.country_id = d.country_id
As GMB has answered you cannot do it in this way because of the duplicates. GMB's answer is the best way to solving your problem. If for some reason you cannot follow his advice then maybe my answer would help.
You could use a composite primary key on the columns CountryCode, Division, SubDivision. Then add subdivision to Table2. And then reference this primary key in the foreignkey restraint. (notice that my example throws an error on purpose to show that the value cannot be deleted)
DROP TABLE IF EXISTS Table2;
DROP TABLE IF EXISTS Table1;
CREATE TABLE Table1
(ID INT IDENTITY(1,1)
, CountryCode CHAR(3)
, Division VARCHAR(4)
, SubDivision VARCHAR(10)
, CONSTRAINT PK_Table1 PRIMARY KEY(CountryCode, Division, SubDivision)
)
INSERT INTO Table1(CountryCode, Division, SubDivision)
VALUES ('IDN', 'A', 'A-1')
, ('IDN', 'B', 'B-1')
, ('IDN', 'B', 'B-2');
CREATE TABLE Table2
(ID INT IDENTITY(1,1) PRIMARY KEY
, CountryCode CHAR(3)
, Division VARCHAR(4)
, SubDivision VARCHAR(10)
, ReportNo VARCHAR(10)
, CONSTRAINT FK_CountryDivision FOREIGN KEY(CountryCode, Division, SubDivision) REFERENCES Table1(CountryCode, Division, SubDivision)
);
INSERT INTO Table2(CountryCode, Division, SubDivision, ReportNo)
VALUES ('IDN', 'A', 'A-1', 'Re001')
, ('IDN', 'B', 'B-1', 'Re002')
, ('IDN', 'B', 'B-2', 'Re003');
DELETE FROM Table1
WHERE Division = 'A';
Of course this change could add a whole set of new problems for instance when a report is for the whole division what should the subdivision value then be.
ps. i had to change up the example tables a bit because the values did not match, ie string values into an int column.
SQL fiddle
thank you for the all greats answer.
But in my design I can't doing this, because I has been wrong at the first time.
And the greats answer was from Mr. #10676716 #GMB.
-- "top" table that stores the countries
create table countries (
country_id int primary key
name varchar(100)
);
-- the table that stores the divisions
create table divisions (
division_id int primary key,
country_id int references countries(country_id),
name varchar(100)
);
-- the table that stores the subdivisions
-- this corresponds to "table1" in your question
create table subdivisions (
subdivision_id int primary key,
division_id int references divisions(division_id),
name varchar(100)
);
-- the table that stores the reports
-- this corresponds to "table2" in your question
create table reports (
report_id int primary key,
division_id references divisions(division_id),
name varchar(100)
);
Thanks again Mr. GMB.

SQL Query that displays the number of athletes that have competed in at least ten different places(locations)

I'm building a query based on the fact that they have competed in ten or more places. Note that it does not matter how many sports or competitions they competed in, just how many places they have competed in.
CREATE TABLE Gender (
gender CHAR(1),
description VARCHAR(10),
PRIMARY KEY (gender));
CREATE TABLE People (
ID INT,
name VARCHAR(50),
gender CHAR(1),
height FLOAT,
PRIMARY KEY (ID),
FOREIGN KEY (gender) REFERENCES Gender (gender));
CREATE TABLE Sports (
ID INT,
name VARCHAR(50),
record FLOAT,
PRIMARY KEY (ID),
UNIQUE (name));
CREATE TABLE Competitions (
ID INT,
place VARCHAR(50),
held DATE,
PRIMARY KEY (ID));
CREATE TABLE Results (
peopleID INT NOT NULL,
competitionID INT NOT NULL,
sportID INT NOT NULL,
result FLOAT,
PRIMARY KEY (peopleID, competitionID, sportID),
FOREIGN KEY (peopleID) REFERENCES People (ID),
FOREIGN KEY (competitionID) REFERENCES Competitions (ID),
FOREIGN KEY (sportID) REFERENCES Sports (ID));
If anyone can help me with this it would be much appreciated!
As commented by Brad, you can use a simple aggregated query that JOINs table People with Results, with a HAVING BY clause to filter on the number of competitions each person participated to. It seems like you don’t need to bring in any other table to achieve your goal.
SELECT
p.ID,
p.Name
FROM
People p
Results r ON r.peopleID = p.ID
GROUP BY
p.ID,
p.Name
HAVING COUNT(*) >= 10

SQL Query Error: Invalid Column Name

I am new to SQL and I have this problem, I can't seem to search on google.
This is my school work so please be patient with my coding.
Now the problem is i am doing a select query and SQL does not seem to recognize the column I need
here is an attached picture, it just says invalid column but it is clear that the column exist
Dropbox link to the picture
here is the complete code
create database Hairsalon
use Hairsalon
create table Employee
(
Employee_ID int primary key,
Name varchar(20),
Birthday date,
Gender char(1)
);
create table Inventory
(
Inventory_ID int primary key,
Name varchar(30),
Stock int
)
create table Record
(
Transaction_Number int primary key,
Transaction_Date date,
Transaction_Time time
);
alter table Record
drop column Transaction_Date
alter table Record
drop column Transaction_Time
alter table Record
add Transaction_DT varchar(30)
alter table Record
add Transaction_Description varchar(255)
Create table Inventory_Record
(
Transaction_Number int primary key foreign key references Record(Transaction_Number),
Inventory_ID int foreign key references Inventory(Inventory_ID),
);
create table Customer_Record
(
Transaction_Number int foreign key references Record(Transaction_Number),
Customer_ID int primary key,
Service_Description varchar(255),
Pay money
);
create table Customer
(
Customer_ID int foreign key references Customer_Record(Customer_ID),
Name varchar(20),
Payable money,
Session_DT varchar(20)
);
create table Employee_Record
(
Transaction_Number int primary key foreign key references Record(Transaction_Number),
Employee_ID int foreign key references Employee(Employee_ID),
Time_In time,
Time_Out time,
Record_Date date,
Customer_Count int
);
create table Salon
(
Customer_ID int foreign key references Customer_Record(Customer_ID),
Employee_ID int foreign key references Employee(Employee_ID),
Inventory_ID int foreign key references Inventory(Inventory_ID),
Transaction_Number int foreign key references Record(Transaction_Number)
);
alter table Customer
add Session_DT varchar(20)
alter table Customer
drop column Customer_ID
alter table Customer
add Customer_ID int foreign key references Customer_Record(Customer_ID)
alter table Customer_Record
add Employee_ID int foreign key references Employee(Employee_ID)
alter table Employee
add [Type] varchar(15)
alter table Employee
drop column Birthday
alter table Employee
drop column Birthday
alter table Employee_Record
drop column Time_In
alter table Employee_Record
drop column Time_Out
alter table Employee_Record
drop column Record_Date
alter table Employee_Record
add Time_In varchar(20)
alter table Employee_Record
add Time_Out varchar(20)
alter table Employee_Record
add Record_Date varchar(20)
alter table Employee_Record
drop column Customer_Count
insert into Employee
values(1,'Test Employee','M','Cashier','bday')
insert into Employee
values(-1,'Null','N','Null','Null')
insert into Employee values(1,'test1','M','HairDresser','9/8/2014')
INSERT INTO Record values(2,'')
INSERT INTO Customer_Record values(1,1,'asd',123.00,null)
INSERT INTO Customer values(1,'test1',0,'9/8/2014 8:16 AM')
SELECT * FROM Record
select * from Customer
SELECT * FROM Customer_Record
SELECT * FROM Employee
SELECT DISTINCT Customer.Name as 'Customer Name', Employee.Name as 'Attendee'
FROM Customer, Customer_Record, Employee_Record, Employee
WHERE ( Customer_Record.Transaction_Number = Employee_Record.Transaction_Number
AND Employee_Record.Employee_ID = Employee.Employee_ID
AND Customer.Customer_ID = Customer_Record.Customer_ID
) OR ( Customer_Record.Transaction_Number = Employee_Record.Transaction_Number
AND Customer_Record.Employee_ID = -1
AND Customer.Customer_ID = Customer_Record.Customer_ID
)
insert into Employee_Record values(9,1,'10:00','10:99','date')
select * from Record
select * from Customer
select * from Employee
select * from Employee_Record
select * from Customer_Record
select count(Customer_ID) from Customer
select count(Transaction_Number) from Record
insert into Customer
values(1,'test',120,DATEFROMPARTS(32,12,31))
INSERT INTO Customer_Record values(1,1,'Long Haired Customer: ',0, null)
DELETE FROM Customer WHERE Customer_ID = 1
DELETE FROM Customer
DELETE FROM Customer_Record
DELETE FROM Record
DELETE FROM Employee
DELETE FROM Employee_Record
DELETE FROM Inventory
DELETE FROM Inventory_Record
commit
According to the DDL you have presented, it seems quite obvious that Customer_Record has no Employee_ID.
You may have mixed up Customer_Record with Employee_Record or Employee_ID with Customer_ID, but something is wrong.
EDIT I had missed that you alter the table in your script to add the missing columns. In that case, Intellisense (the auto-completion system) does not take it into account unless you manually reload it (using Ctrl + Shift + R). Your query is correct and the errors will disappear once Intellisense is up to date.

How to organize tables to stare statistic of two entities?

For now I have only two tables
CREATE TABLE IF NOT EXISTS company (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR (250) NOT NULL
);
CREATE TABLE IF NOT EXISTS employee (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR (250),
company_id INT,
FOREIGN KEY (company_id) REFERENCES Company (id)
);
I need to create one or two tables to store employees and companies statistic. For employee statistic I need to remember all previous companies of this employee and of cause hire dates and resign dates. For company statistic I need to remember all resigned employees. What is the best way to organize DB structure in my case?
Since you have many-to-many relationship, you need an aggregate table company_employee that will have combined primary key, so you need:
CREATE TABLE IF NOT EXISTS company (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR (250) NOT NULL
);
CREATE TABLE IF NOT EXISTS employee (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR (250)
);
CREATE TABLE IF NOT EXISTS company_employee (
company_id INT NOT NULL,
employee_id INT NOT NULL,
hire_date DATE,
resign_date DATE,
FOREIGN KEY (company_id) REFERENCES Company (id),
FOREIGN KEY (employee_id) REFERENCES Employee (id)
);
So, if you want anything from aggregate table, just use JOIN on key of appropriate table.

Fact table not populating with data

I'm trying to create and populate three dimensions and a fact tables with data. The dimensions work just fine but i'm unable to get any data into my fact table. If anyone will be so kind to point out what i'm doing wrong - i will be grateful.
Thank you all very much.
create table Customer
(
CUID integer identity(1,1) primary key,
Name varchar (25)
)
insert into Customer select distinct customer from [Data]
create table Agent
(
AID integer identity(1,1) primary key,
Agent varchar (25)
)
insert into Agent select distinct Seller from [Data]
create table Time
(
TID integer identity(1,1) primary key,
Week varchar (25),
Month varchar (25),
Year int
)
insert into Time
(Week,Month,Year)
select distinct Day,Month,Year from [Data]
Create Table Fact
(
CUID integer,
AID integer,
TID integer,
Sale money,
constraint pk_Fact primary key (CUID, AID, TID),
constraint fk1_Fact foreign key (CUID)
references Customer (CUID),
constraint fk2_Fact foreign key (AID)
references Agent (AID),
constraint fk3_Fact foreign key (TID)
references Time (TID),
)
insert into Fact
(CUID, AID, TID, Sale)
SELECT CUID, AID, TID, Sale
FROM Customer,
Agent,
Time,
[Data]
You need to process the cubes. Open it in a Visual Studio 2008 and process it. After that the data will be visible. Did you see the Dimension's mapping with the measures are correct?