How can I re-design with fewer tables? - sql

I have designed a database for online donation. I have not added foreign key relationships because first I want to achieve a simple design. It's currently 11 tables, and it seems already more complicated than necessary.
The first one is registration table. The other 8 are different item types which will be donated by donors or by receivers, and 1 will be for post table it means when the user or receiver wants donate they will post what they want to donate or what they want ask others to give them. The last one will be for comment, the comment might be on the post by donors or receivers.
Can any one can help me re-design this in a simpler way?
Create Table Registration
(
ID int NOT NULL primary key,
FirstName varchar(25) NOT NULL,
LastName varchar(25) NOT NULL,
Username varchar(20),
Gender varchar(10),
Home_Address varchar(100),
Office_Address varchar(100),
City varchar(25),
State varchar(25),
Zip varchar(25),
Contact_No int ,
Email varchar(25)
);
2 posts
CREATE TABLE Post
( p_id int constraint p_pk primary key,
Username varchar(25),
Status varchar(25),
image nvarchar(max),
date_time varchar(100)
);
3,
CREATE TABLE Books
(
ISBN nvarchar(200) constraint ISBN_pk primary key,
Username varchar(25),
Book_title varchar(25),
Authorname varchar(25),
Publicationdate varchar(25),
Purchasedate varchar(25),
Book_edition varchar(25)
);
4,
CREATE TABLE images
(I_id int constraint i_pk primary key,
Username varchar(25),
Title varchar(25),
url varchar(50),
description varchar(100)
);
5,
CREATE TABLE laptop
(
L_id int constraint L_pk primary key,
Username varchar(25),
Model varchar(25),
Speed varchar(25),
Ram varchar(25),
HD varchar(50),
Screen varchar(50)
);
6, other items
CREATE TABLE other_items
(O_id int constraint O_pk primary key,
Username varchar(25),
Item_title varchar(25),
Item_type varchar(25),
Item_description varchar(100),
Itempicture varchar(100)
);
7, recipient
CREATE TABLE Recipient
(R_id int constraint R_pk primary key,
Firstname varchar(25),
Lastname varchar(25),
National_id_no varchar(50),
Address varchar(100),
Contact_no varchar(100)
);
8
CREATE TABLE Shoesimages
(
s_id int constraint Rss_pk primary key,
Username varchar(25), standard varchar(25),
Gender varchar(25),
Colour varchar(25),
Description varchar(100)
);
9,
CREATE TABLE uniform
(U_id int constraint u_pk primary key,
Username varchar(25),
Standard varchar(25),
Gender varchar(25),
Colour varchar(25),
Description varchar(100)
);
10,
CREATE TABLE Research_paper
(
Rs_id int constraint Rs_pk primary key,
Username varchar(25),
title varchar(25),
authorname varchar(25),
year_of_publish varchar(25),
venu varchar(100)
);
11,
CREATE TABLE Comments
(
C_id int constraint C_pk primary key,
Username varchar2(25),
comment nvarchar(max),
date_time varchar2(100)
);

Related

ORA-00907: missing right patenthesis

I just can't see what needs to be changed in this code:
create table tblGirlScout (
GirlScout_ID varchar(10,0) not null,
GirlScoutFirstName varchar(25) not null,
GirlScoutLastName varchar(25),
GirlScoutAddress varchar(30),
GirlScoutCity varchar(20),
GirlScoutState char(2),
GirlScoutPostalCode varchar(9),
Constraint tblGirlScout_PK Primary Key(GirlScout_ID)
);
I believe you wanted to set GirlScout_ID column as a number, not a varchar. This is what causes the problem. Here is a fix
create table tblGirlScout (
GirlScout_ID number(10,0) not null,
GirlScoutFirstName varchar(25) not null,
GirlScoutLastName varchar(25),
GirlScoutAddress varchar(30),
GirlScoutCity varchar(20),
GirlScoutState char(2),
GirlScoutPostalCode varchar(9),
Constraint tblGirlScout_PK Primary Key(GirlScout_ID)
);

Arithmetic overflow error converting expression to data type int, How do I resolve this?

This is the code that created the table.
CREATE TABLE CUSTOMERS
(
Customer_ID INT NOT NULL,
CHECK(Customer_ID <= 11),
First_Name varchar(20) NOT NULL,
Last_Name varchar(30),
Home_Street varchar(30),
Home_City varchar(20),
Home_State varchar(2),
Home_Zip varchar(5),
PhoneNumber varchar(11) NOT NULL
);
ALTER TABLE CUSTOMERS
ADD CONSTRAINT PK_CUSTOMERS PRIMARY KEY(Customer_ID);
Then I try to insert data (using this code) into the table and that is where I get this error.
INSERT INTO dbo.CUSTOMERS(Customer_ID, First_Name, Last_Name, Home_Street, Home_City, Home_State, Home_Zip, PhoneNumber)
VALUES (11223344556, 'John', 'Doe', '1234 Hand Street', 'Wahiawa', 'HI', 96786, 2535551267);
What am I doing wrong and what can I do to fix this issue?
AS per my understanding you are checking length of Customer_ID <=11 so you should mention len(Customer_ID)<=11 it will work and you should alter datatype of Customer_ID int to bigint
CREATE TABLE CUSTOMERS
(
Customer_ID bigINT NOT NULL,
CHECK(len(Customer_ID)<=11),
First_Name varchar(20) NOT NULL,
Last_Name varchar(30),
Home_Street varchar(30),
Home_City varchar(20),
Home_State varchar(2),
Home_Zip varchar(5),
PhoneNumber varchar(11) NOT NULL
);
ALTER TABLE CUSTOMERS
ADD CONSTRAINT PK_CUSTOMERS
PRIMARY KEY(Customer_ID);
INSERT INTO dbo.CUSTOMERS(Customer_ID,First_Name,Last_Name,Home_Street,
Home_City,Home_State,Home_Zip,PhoneNumber)
VALUES(11223344556,'John','Doe','1234 Hand Street',
'Wahiawa','HI',96786,2535551267);
You need customer id to be bigint:
CREATE TABLE CUSTOMERS
(
Customer_ID BIGINT NOT NULL,
CHECK(Customer_ID<=11),
First_Name varchar(20) NOT NULL,
Last_Name varchar(30),
Home_Street varchar(30),
Home_City varchar(20),
Home_State varchar(2),
Home_Zip varchar(5),
PhoneNumber varchar(11) NOT NULL
);
ALTER TABLE CUSTOMERS
ADD CONSTRAINT PK_CUSTOMERS
PRIMARY KEY(Customer_ID);
The problem may be due to CHECK(Customer_ID<=11) since Customer_ID id integer datatype the server may check for integer validation and not length validation. Try to change the validation.

oracle SQL - ORA-009007 Missing right parenthesis

I have this sql
CREATE TABLE PATIENTS
(
patientID NUMBER NOT NULL,
healthInsID NUMBER,
fname VARCHAR(20) NOT NULL,
minit VARCHAR(15),
lname VARCHAR(30) NOT NULL,
gender CHAR(2),
email VARCHAR(40),
street VARCHAR(40),
postalCode NUMBER,
city VARCHAR(20),
country VARCHAR(20),
PRIMARY KEY (patientID),
FOREIGN KEY (healthInsID) REFERENCES HEALTH_INSURANCES
ON DELETE SET NULL ON UPDATE CASCADE
);
I have no idea why I keep getting this error. I have search a lot but still nothing that can solve it. Any ideas?
Thanks
You have to specify column from HEALTH_INSURANCES table in FOREIGN KEY caluse.
You should also remove ON UPDATE CASCADE. You can use a trigger on update instead of this clause.
CREATE TABLE PATIENTS
(
patientID NUMBER NOT NULL,
healthInsID NUMBER,
fname VARCHAR(20) NOT NULL,
minit VARCHAR(15),
lname VARCHAR(30) NOT NULL,
gender CHAR(2),
email VARCHAR(40),
street VARCHAR(40),
postalCode NUMBER,
city VARCHAR(20),
country VARCHAR(20),
PRIMARY KEY (patientID),
FOREIGN KEY (healthInsID) REFERENCES HEALTH_INSURANCES (healthInsID)
ON DELETE SET NULL
);
More information about constraints

Creating Entity Relationship Diagram

I created the ERD of my system and now I would like to create a SQL Code.
So should a SQL Code look like this?:
CREATE TABLE Student
(
StudentID INT NOT NULL IDENTITY PRIMARY KEY,
FirstName VARCHAR(255),
LastName VARCHAR(255),
ADDRESS VARCHAR(255),
PhoneNumber VARCHAR(255),
Email VARCHAR(255),
GroupID INT NOT NULL FOREIGN KEY
);
Your problem is with the FOREIGN KEY part of your query, you are not defining the foreign key there. If you now remove that, your query will work, but without a defined FK:
CREATE TABLE Student
(
StudentID INT NOT NULL IDENTITY PRIMARY KEY,
FirstName VARCHAR(255),
LastName VARCHAR(255),
ADDRESS VARCHAR(255),
PhoneNumber VARCHAR(255),
Email VARCHAR(255),
GroupID INT NOT NULL
);
If you want to create the foreign key, you need to do something like this (with the correct table and column):
CREATE TABLE Student
(
StudentID INT NOT NULL IDENTITY PRIMARY KEY,
FirstName VARCHAR(255),
LastName VARCHAR(255),
ADDRESS VARCHAR(255),
PhoneNumber VARCHAR(255),
Email VARCHAR(255),
GroupID INT NOT NULL REFERENCES Group(Group_ID)
);
Your "FOREIGN KEY" declaration is incomplete - you need to say which table/column the foreign key references.
If you just want to get the table built,
CREATE TABLE Student
(
StudentID INT NOT NULL IDENTITY PRIMARY KEY,
FirstName VARCHAR(255),
LastName VARCHAR(255),
ADDRESS VARCHAR(255),
PhoneNumber VARCHAR(255),
Email VARCHAR(255),
GroupID INT NOT NULL);
should work.

Foreign Key Used in Composite Primary Key

Is it possible to use a composite foreign key as a piece of a table's composite primary key?
For instance, let's say I have two tables:
CREATE TABLE DB.dbo.Partners
(
CONSTRAINT pk_Partners_Id
PRIMARY KEY (Name, City, State, Country, PostalCode),
Name VARCHAR(100) NOT NULL,
Address1 VARCHAR(100),
Address2 VARCHAR(100),
Address3 VARCHAR(100),
City VARCHAR(150) NOT NULL,
State CHAR(2) NOT NULL,
Country CHAR(2) NOT NULL,
PostalCode VARCHAR(16) NOT NULL,
Phone VARCHAR(20),
Fax VARCHAR(20),
Email VARCHAR(256)
)
... and then in a second table, I would like to reference the foreign key in the second table's primary key:
CREATE TABLE DB.dbo.PartnerContacts
(
CONSTRAINT pk_PartnerContacts_Id
PRIMARY KEY (fk_PartnerContacts_PartnerId, FirstName, LastName, PhoneNumber, Email),
CONSTRAINT fk_PartnerContacts_PartnerId
FOREIGN KEY REFERENCES Partners(Name, City, State, Country, PostalCode),
FirstName VARCHAR(75) NOT NULL,
MiddleName VARCHAR(75),
LastName VARCHAR(75) NOT NULL,
PhoneNumber VARCHAR(20) NOT NULL,
MobileNumber VARCHAR(20),
FaxNumber VARCHAR(20),
Email VARCHAR(256) NOT NULL,
MailTo VARCHAR(100),
Address1 VARCHAR(100),
Address2 VARCHAR(100),
Address3 VARCHAR(100),
City VARCHAR(150),
State CHAR(2),
Country CHAR(2),
PostalCode VARCHAR(16)
)
Is there any way that I can do that? Yes, it might be easier to just simply use IDENTITY columns in these tables but if I can define an actual relationship without an IDENTITY I would like to do that.
EDIT:
I wanted to provide the final, working SQL. Thanks to everyone who answered!
CREATE TABLE DB.dbo.Partners
(
CONSTRAINT pk_Partners_Id
PRIMARY KEY (Name, City, State, Country, PostalCode),
Id INT NOT NULL UNIQUE IDENTITY(1, 1),
Name VARCHAR(100) NOT NULL,
Address1 VARCHAR(100),
Address2 VARCHAR(100),
Address3 VARCHAR(100),
City VARCHAR(150) NOT NULL,
State CHAR(2) NOT NULL,
Country CHAR(2) NOT NULL,
PostalCode VARCHAR(16) NOT NULL,
Phone VARCHAR(20),
Fax VARCHAR(20),
Email VARCHAR(256)
)
CREATE TABLE DB.dbo.PartnerContacts
(
CONSTRAINT pk_PartnerContacts_Id
PRIMARY KEY
(PartnerId, FirstName, LastName, PhoneNumber, Email),
PartnerId INT NOT NULL CONSTRAINT fk_PartnerContacts_PartnerId FOREIGN KEY REFERENCES Partners(Id),
FirstName VARCHAR(75) NOT NULL,
MiddleName VARCHAR(75),
LastName VARCHAR(75) NOT NULL,
PhoneNumber VARCHAR(20) NOT NULL,
MobileNumber VARCHAR(20),
FaxNumber VARCHAR(20),
Email VARCHAR(256) NOT NULL,
MailTo VARCHAR(100),
Address1 VARCHAR(100),
Address2 VARCHAR(100),
Address3 VARCHAR(100),
City VARCHAR(150),
State CHAR(2),
Country CHAR(2),
PostalCode VARCHAR(16)
)
You probably need to specify the columns that are supposed to match.
CONSTRAINT fk_PartnerContacts_PartnerId
FOREIGN KEY (columns that correspond to referenced columns)
REFERENCES Partners (Name, City, State, Country, PostalCode),
So you need to provide the five column names whose values are supposed to match the values of {Name, City, State, Country, PostalCode} in the table "Partners". i'm pretty sure youcan't do that with your current structure. You won't be able to match "Name". I think you're looking for something along these lines.
CREATE TABLE DB.dbo.PartnerContacts (
-- Start with columns that identify "Partner".
partner_name VARCHAR(100) NOT NULL,
partner_city VARCHAR(150) NOT NULL,
partner_state CHAR(2) NOT NULL,
partner_country CHAR(2) NOT NULL,
partner_postcode VARCHAR(16) NOT NULL,
CONSTRAINT fk_PartnerContacts_PartnerId
FOREIGN KEY (partner_name, partner_city, partner_state, partner_country, partner_postcode)
REFERENCES Partners (Name, City, State, Country, PostalCode),
FirstName VARCHAR(75) NOT NULL,
MiddleName VARCHAR(75),
LastName VARCHAR(75) NOT NULL,
PhoneNumber VARCHAR(20) NOT NULL,
MobileNumber VARCHAR(20),
FaxNumber VARCHAR(20),
Email VARCHAR(256) NOT NULL,
MailTo VARCHAR(100),
Address1 VARCHAR(100),
Address2 VARCHAR(100),
Address3 VARCHAR(100),
City VARCHAR(150),
State CHAR(2),
Country CHAR(2),
PostalCode VARCHAR(16),
CONSTRAINT pk_PartnerContacts_Id
PRIMARY KEY (partner_name, partner_city, partner_state, partner_country, partner_postcode,
FirstName, LastName, PhoneNumber, Email)
);
Yes it is possible and is generally considered best DB design practice, but practically, an ID column is just easier to deal with. Think of join tables, their primary key is a composite of two foreign keys. There is no difference to using multiple foreign keys as part of a composite primary key.
Yes, that is definitely possible. We do have instances where we have a composite foreign key that is a part of the composite primary key of other table.
Let's simplify the use case little bit for the below example.
Say we have a table test1 having a composite primary key (A, B)
Now we can have a table say test2 having primary key (P, Q, R) where in (P,Q) of test2 references (A,B) of test1.
I ran the following script in the MySql database and it works just fine.
CREATE TABLE `test1` (
`A` INT NOT NULL,
`B` VARCHAR(2) NOT NULL,
`C` DATETIME NULL,
`D` VARCHAR(45) NULL,
PRIMARY KEY (`A`, `B`));
CREATE TABLE `test2` (
`P` INT NOT NULL,
`Q` VARCHAR(2) NOT NULL,
`R` INT NOT NULL,
`S` DATETIME NULL,
`T` VARCHAR(8) NULL,
PRIMARY KEY (`P`, `Q`, `R`),
INDEX `PQ_idx` (`P`,`Q` ASC),
CONSTRAINT `PQ`
FOREIGN KEY (`P`, `Q`)
REFERENCES `test1` (`A`,`B`)
ON DELETE CASCADE
ON UPDATE CASCADE);
In the above mentioned case, the database is expecting the combination of (A,B) to be unique and it is, being a primary key in test1 table.
But if you try to do something like following, the script would fail. The database would not let you create the test2 table.
CREATE TABLE `test2` (
`P` INT NOT NULL,
`Q` VARCHAR(2) NULL,
`R` DATETIME NULL,
`S` VARCHAR(8) NULL,
`T` VARCHAR(45) NULL,
INDEX `P_idx` (`P` ASC),
INDEX `Q_idx` (`Q` ASC),
CONSTRAINT `P`
FOREIGN KEY (`P`)
REFERENCES `test1` (`A`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `Q`
FOREIGN KEY (`Q`)
REFERENCES `test1` (`B`)
ON DELETE CASCADE
ON UPDATE CASCADE);
In the above mentioned case database would expect the column A to be unique individually and the same follows for column B. It does not matter if combination of (A,B) is unique.