TABLES
CREATE TABLE LocalBusiness
(
BusinessID INT NOT NULL PRIMARY KEY,
BusinessName VARCHAR2 (20) NOT NULL,
TypeID INT,
Latitude DECIMAL (10,2),
Longitude DECIMAL (10,2),
Web_address VARCHAR2 (50) NOT NULL,
Postcode VARCHAR2 (10) NOT NULL,
official_rating int,
min_price NUMBER(4,2),
max_price NUMBER(4,2),
FOREIGN KEY (TypeID) REFERENCES LocalBusinessType (TypeID),
CONSTRAINT chk_Officialrating CHECK (official_rating> 0 AND official_rating<6 )
);
CREATE TABLE Address
(
AddressID INT NOT NULL PRIMARY KEY,
BusinessID INT,
AreaID INT,
Address VARCHAR2 (50) NOT NULL,
Postcode VARCHAR2 (10) NOT NULL,
FOREIGN KEY (BusinessID) REFERENCES LocalBusiness (BusinessID),
FOREIGN KEY (AreaID) REFERENCES Area (AreaID)
);
CREATE TABLE Phone
(
PhoneNoID INT NOT NULL PRIMARY KEY,
PhoneNo VARCHAR2 (15) NOT NULL,
BusinessID INT,
Description VARCHAR2 (50) NOT NULL,
FOREIGN KEY (BusinessID) REFERENCES LocalBusiness (BusinessID),
CONSTRAINT PhoneNo_unique UNIQUE (PhoneNo)
);
CREATE TABLE Email
(
EmailID INT NOT NULL PRIMARY KEY,
email_address VARCHAR2 (50) NOT NULL,
BusinessID INT,
Description VARCHAR2 (50) NOT NULL,
FOREIGN KEY (BusinessID) REFERENCES LocalBusiness (BusinessID),
CONSTRAINT email_unique UNIQUE (email_address)
);
CREATE TABLE Area
(
AreaID INT NOT NULL PRIMARY KEY,
AreaName VARCHAR2 (20) NOT NULL,
Region VARCHAR2 (20) NOT NULL
);
SELECT statement:
SELECT
LocalBussiness.BusinessName, Address.Address, Address.Postcode,
Area.AreaName, Area.Region, LocalBusiness.OfficialRating,
LocalBusiness.min_price, LocalBusiness_max_price,
Phone.description, Phone.PhoneNo,
Email.Description, Email.email_address,
LocalBusiness.Web_address
FROM
LocalBusiness
JOIN
Address ON LocalBusiness.BusinessID = Address.BusinessID
JOIN
Area ON Address.AreaID = Area.AreaID
AND LocalBusiness.BusinessID = Address.BusinessID
JOIN
Phone ON Phone.BusinessID = LocalBusiness.BusinessID
JOIN
Email ON Email.BusinessID = LocalBusiness.BusinessID
WHERE
TypeID = '1'
ORDER BY
LocalBusiness.BusinessName ASC;
The where clause in the sql join statement written above seem to be ineffective as the some values for the TypeID return incomplete data while others return no rows at all. How do I go about fixing this?
Yyou repeat a condition
AND LocalBusiness.BusinessID=Address.BusinessID in JOIN Area
Try avoinding this repetition
SELECT LocalBussiness.BusinessName, Address.Address, Address.Postcode,
Area.AreaName, Area.Region, LocalBusiness.OfficialRating,
LocalBusiness.min_price, LocalBusiness_max_price, Phone.description,
Phone.PhoneNo, Email.Description, Email.email_address,
LocalBusiness.Web_address
FROM LocalBusiness
JOIN Address ON LocalBusiness.BusinessID=Address.BusinessID
JOIN Area ON Address.AreaID=Area.AreaID
JOIN Phone ON Phone.BusinessID=LocalBusiness.BusinessID
JOIN Email ON Email.BusinessID=LocalBusiness.BusinessID
WHERE TypeID = '1'
ORDER BY LocalBusiness.BusinessName ASC;
Otherwise check th consistence of your data ..
Related
How do I select the highest number in one column and then search the the ID in other table?
I have these two tables:
create table armazens_has_artigos
(
stockMinimo int not null,
stockQuantidade int not null,
idArtigos int not null,
idArmazens int not null,
idZonasFisicas int not null
constraint fk_armazens_has_artigos_idArtigos
foreign key (idArtigos)
references artigos (idArtigos) on delete cascade,
constraint fk_armazens_has_artigos_idArmazens
foreign key (idArmazens)
references armazens (idArmazens) on delete cascade,
constraint fk_armazens_has_artigos_zonasFisicas_idZonasFisicas
foreign key (idZonasFisicas)
references zonasFisicas (idZonasFisicas) on delete cascade
);
In this one I need to select the highest StockQuantidade and then with the same id select in this table the nome
create table artigos
(
idArtigos int primary key,
nome varchar (45) not null,
descr varchar (45) not null,
precoCompra float not null,
precoVenda float not null,
unidadeRepresentacao varchar (45) not null
);
There might be more then one Artigos matching the criteria:
SELECT *
FROM artigos
WHERE idArtigos IN
(
SELECT idArtigos
FROM dbo.armazens_has_artigos
WHERE stockQuantidade =
(
SELECT MAX(stockQuantidade)FROM armazens_has_artigos
)
);
SELECT
nome
FROM artigos
JOIN armazens_has_artigos
ON armazens_has_artigos.idArtigos = artigos.idArtigos
WHERE armazens_has_artigos.stockQuantidade = (SELECT
MAX(stockQuantidade)
FROM armazens_has_artigos)
I have 3 tables in my database:
CREATE TABLE airlines (
airline_name VARCHAR NOT NULL PRIMARY KEY
);
CREATE TABLE airport (
code VARCHAR UNIQUE NOT NULL PRIMARY KEY,
name VARCHAR NOT NULL,
category VARCHAR NOT NULL,
city VARCHAR NOT NULL
);
CREATE TABLE volo (
code VARCHAR UNIQUE NOT NULL PRIMARY KEY,
departure_time TIME(0) NOT NULL,
departure_airport VARCHAR NOT NULL REFERENCES aeroporto(codice),
arrival_time TIME(0) NOT NULL,
arrival_airport VARCHAR NOT NULL REFERENCES aeroporto(codice),
airline VARCHAR NOT NULL REFERENCES airlines(airline_name)
);
I need to display which airline departs from all the airport in my DB. Any suggestions?
You can use aggregation and count the departure_airport:
select v.airline
from volo v
group by v.airline
having count(distinct departure_airport) = (select count(*) from airport);
I try to create three tables by using website suport compiler any code but I have a problem in the table of the bill.
When I run it I get to error show me it is near in foreign key
These are codes of three tables
CREATE TABLE patient (
Patient Id (5) Primary key,
Name Varchar (20) Not null ,
Age Int Not null ,
Weight Int Not null ,
Gender Varchar (10) Not null,
Address Varchar (50) Not null ,
Disease Varchar (20) Not null
);
CREATE TABLE doctors (
DoctorId Varchar (5) Primary key,
Doctorname Varchar (15) Not null,
Dept Varchar (15) Not null
);
CREATE TABLE bill (
Bill_no Varchar (50) Primary key,
Patient_Id Varchar (5) Foreign key,,
doctor_charge Int Not null,
patient_type Varchar (10) null,
no_of_days Int null,
lab_charge Int null,
bill Int Not null
);
Patient Table
CREATE TABLE patient
(
patient_id VARCHAR (5) PRIMARY KEY,
name VARCHAR (20) NOT NULL,
age INT NOT NULL,
weight INT NOT NULL,
gender VARCHAR (10) NOT NULL,
address VARCHAR (50) NOT NULL,
disease VARCHAR (20) NOT NULL
);
Errors
No data type has been assigned in Patient id column (Patient Id (5)
Primary key)
Patient id column name contains spaces. You need to
enclose the column name in double quotes or replace space with something else
(ex: _). It's not recommended to use spaces.
A column name with space
CREATE TABLE tablename ("column name" datatype);
Doctors Table
CREATE TABLE doctors
(
doctorid VARCHAR (5) PRIMARY KEY,
doctorname VARCHAR (15) NOT NULL,
dept VARCHAR (15) NOT NULL
);
Bill Table
CREATE TABLE bill
(
bill_no VARCHAR (50) PRIMARY KEY,
patient_id VARCHAR (5),
doctor_charge INT NOT NULL,
patient_type VARCHAR (10) NULL,
no_of_days INT NULL,
lab_charge INT NULL,
bill INT NOT NULL,
FOREIGN KEY (patient_id) REFERENCES patient(patient_id)
);
Errors
The way you have assigned foreign key is wrong. Please refer this and this article for more information. (Patient_Id Varchar (5) Foreign key,,)
There are two commans in the Patient_Id column (Patient_Id Varchar (5) Foreign key,,)
You have to provide reference of the table for which you want to use the reference key.
For example, you have table Persons which has Primary key PersonID, in that case if you want to use that as foreign key in another table, lets say Orders.
In Oracle
CREATE TABLE Orders (
OrderID numeric(10) not null,
OrderNumber numeric(10) not null,
PersonID numeric(10) not null,
CONSTRAINT fk_person_id
FOREIGN KEY (PersonID )
REFERENCES Persons(PersonID )
Your Case :
CREATE TABLE bill
( Bill_no Varchar (50) Primary key,
Patient_Id Varchar (5),
doctor_charge Int Not null,
patient_type Varchar (10) null,
no_of_days Int null,
lab_charge Int null,
bill Int Not null,
CONSTRAINT fk_patient_id
FOREIGN KEY (Patient_Id)
REFERENCES patient(Patient_Id)
);
Remove the 'Foreign Key' from the table creation script.
Add this to your SQL script:
ALTER TABLE [Bill] WITH CHECK ADD CONSTRAINT [FK_Bill_Patient] FOREIGN KEY([Patient_Id])
REFERENCES [Patient] ([Patient_Id])
GO
ALTER TABLE [Bill] CHECK CONSTRAINT [FK_Bill_Patient]
GO
The words FOREIGN KEY are only needed for introducing the name of the FK constraint. Since your other constraints are not named, you might as well skip that part and go straight to REFERENCES.
If you specify a foreign key constraint as part of the column definition, you can omit the datatype to allow it to inherit from its parent at the time of creation, which I think is good practice as the types will automatically match.
We use VARCHAR2 in Oracle, not VARCHAR.
You don't need to specify NULL for columns that are allowed to be null.
I am not sure a 5-character string is a good datatype for a unique ID. How will you generate the values? Normally an auto-incrementing sequence number simplifies this.
create table doctors
( doctorid varchar2(5) primary key
, doctorname varchar2(15) not null
, dept varchar2(15) not null );
create table patients
( patient_id varchar2(5) primary key
, name varchar2(20) not null
, age integer not null
, weight integer not null
, gender varchar2(10) not null
, address varchar2(50) not null
, disease varchar2(20) not null );
create table bills
( bill_no varchar2(50) primary key
, patient_id references patients -- Allow datatype to inherit from parent
, patient_type varchar2(10)
, no_of_days integer
, lab_charge integer
, bill integer not null );
Can't figure out why my question 3 is getting the error mentioned above.
Below is my code
/* Question 1 */
CREATE TABLE CUSTOMERS
(
CustomerID INT PRIMARY KEY,
CustFirstName VARCHAR(50) NOT NULL,
CustLastName VARCHAR(50) NOT NULL,
CustStreetAddress VARCHAR(50) NOT NULL,
CustCity VARCHAR(50) NOT NULL,
CustState VARCHAR(26) NOT NULL,
CustZipCode INT NOT NULL,
CustAreaCode INT NOT NULL,
CustPhoneNumber VARCHAR (26) NOT NULL
);
CREATE TABLE EMPLOYEES
(
EmployeeID INT PRIMARY KEY,
EmpFirstName VARCHAR(50) NOT NULL,
EmpLastName VARCHAR(50) NOT NULL,
EmpCity VARCHAR (50) NOT NULL,
EmpState VARCHAR(26) NOT NULL,
EmpZipCode INT NOT NULL,
EmpAreaCode INT NOT NULL,
EmpPhoneNumber VARCHAR(26) NOT NULL,
EmpBirthDate DATE NOT NULL
);
CREATE TABLE ORDERS
(
OrderNumber INT PRIMARY KEY,
OrderDate DATE NOT NULL,
ShipDate DATE NOT NULL,
CustomerID INT NOT NULL,
EmployeeID INT NOT NULL,
FOREIGN KEY(EmployeeID) REFERENCES EMPLOYEES(EmployeeID),
FOREIGN KEY(CustomerID) REFERENCES CUSTOMERS(CustomerID)
);
CREATE TABLE CATEGORIES
(
CategoryID INT PRIMARY KEY,
CategoryDescription VARCHAR(255) NOT NULL
);
CREATE TABLE PRODUCTS
(
ProductNumber INT PRIMARY KEY,
ProductName VARCHAR(50) NOT NULL,
ProductDescription VARCHAR(255) NOT NULL,
RetailPrice INT NOT NULL,
QuantityOnHand INT NOT NULL,
CategoryID INT NOT NULL,
FOREIGN KEY(CategoryID) REFERENCES CATEGORIES (CategoryID)
);
CREATE TABLE ORDER_DETAILS
(
OrderNumber INT NOT NULL,
ProductNumber INT NOT NULL,
QuotedPrice INT NOT NULL,
QuantityOrdered INT NOT NULL,
PRIMARY KEY (OrderNumber, ProductNumber),
FOREIGN KEY (OrderNumber) REFERENCES ORDERS(OrderNumber),
FOREIGN KEY(ProductNumber) REFERENCES PRODUCTS(ProductNumber)
);
CREATE TABLE VENDORS
(
VendorID INT PRIMARY KEY,
VendName VARCHAR(100) NOT NULL,
VendStreetAddress VARCHAR(50) NOT NULL,
VendCity VARCHAR(50) NOT NULL,
VendState VARCHAR(26) NOT NULL,
VendFaxNumber VARCHAR(50) NOT NULL,
VendWebPage VARCHAR(100) NOT NULL,
VendEmailAddress VARCHAR(100) NOT NULL
);
CREATE TABLE PRODUCT_VENDORS
(
ProductNumber INT NOT NULL,
VendorID INT NOT NULL,
WholeSalePrice INT NOT NULL,
DaysToDeliver INT NOT NULL,
PRIMARY KEY(ProductNumber, VendorID),
FOREIGN KEY(ProductNumber) REFERENCES PRODUCTS(ProductNumber),
FOREIGN KEY(VendorID) REFERENCES Vendors(VendorID)
);
/* QUESTION 2 */
SELECT
OrderDate, CustFirstName, CustLastName
FROM
CUSTOMERS C, ORDERS O
WHERE
C.CustomerID = O.OrderNumber;
/* QUESTION 3 */
SELECT
ProductNumber, WholeSalePrice, VendName
FROM
PRODUCTS P, PRODUCT_VENDORS PV, VENDORS V
WHERE
P.PRODUCTNUMBER = PV.ProductNumber AND PV.VendorID = V.VendorID;
I got the error
Ambiguous column name 'ProductNumber'
Because there are two table have column name of ProductNumber in your query you need to tell DB engine which column you want to get.
Also, use JOIN instead of , comma CROSS JOIN, because Join is more clear than a comma on the relationship between the two tables
SELECT PV.ProductNumber, WholeSalePrice, VendName
FROM PRODUCTS P
JOIN PRODUCT_VENDORS PV ON P.PRODUCTNUMBER = PV.ProductNumber
JOIN VENDORS V ON PV.VendorID = V.VendorID;
Your query stipulates SELECT ProductNumber From two tables that both have ProductNumber. In the SELECT columns list, prefix ProductNumber with the table name from which you want to receive the data.
also, Have a look at your question 2. I don't think you really want to join customer id to order number, and you should use JOIN syntax intead of joining in the WHERE clause
Ambiguous error means that you are calling a certain field in which exist in both Table and the SQL has no idea where to get it. See, with your query, you're just literally calling the ProductNumber field in which the SQL has no idea where to get it since there are ProductNumber fields on both Tables, and you didn't specify any table.
so it is better when you have same column in multiple tables use table preface 1st then column like table1.col1,table2.col2
so in your case
SELECT p.ProductNumber, WholeSalePrice, v.VendName
FROM PRODUCTS P join
PRODUCT_VENDORS PV on P.PRODUCTNUMBER = PV.ProductNumber //
join VENDORS V on PV.VendorID = V.VendorID //use join instead your's
On a database I have the following tables:
create table dbo.Stores (
Id int not null
Name nvarchar (120) not null
)
create table dbo.Users (
Id int not null
Name nvarchar (120) not null
)
Each User or Shop can have:
1 - One physical address;
2 - One website or social media addresses.
Should I have tables for Addresses and Social Media. For example:
create table dbo.Addresses (
Id int not null
Street nvarchar (120) not null,
PostalCode nvarchar (12) not null
City nvarchar (40) not null,
Latitude float null,
Longitude float null
)
create table dbo.UserAddresses (
UserId int not null,
AddressId int not null
)
And the same for Social Media, phone numbers and so on ...
Should I just add Columns to the Users table?
Update 1
For website addresses and social media addresses I am considering having the following:
create table dbo.UserWebAddresses (
UserId int not null,
WebAddressTypeId int not null,
Value nvarchar(200) not null
)
create table dbo.WebAddressTypes (
Id int not null,
Name nvarchar(20) not null
)
Does this make sense?
Add those columns directly to dbo.Stores and dbo.Users if there are no duplicated rows. If they might be duplicated, then create a new table and use FKs.
CREATE TABLE dbo.Stores
(
Id INT NOT NULL IDENTITY(1, 1) PRIMARY KEY,
Name NVARCHAR(120) NOT NULL,
Address_Id INT NULL
);
CREATE TABLE dbo.Users
(
Id INT NOT NULL IDENTITY(1, 1) PRIMARY KEY,
Name NVARCHAR(120) NOT NULL,
Address_Id INT NULL
);
CREATE TABLE dbo.Addresses
(
Id INT NOT NULL IDENTITY(1, 1) PRIMARY KEY,
Street NVARCHAR(120) NOT NULL,
PostalCode NVARCHAR(12) NOT NULL,
City NVARCHAR(40) NOT NULL,
Latitude FLOAT NULL,
Longitude FLOAT NULL
);
ALTER TABLE dbo.Stores
ADD CONSTRAINT Stores_Address_Id_FK FOREIGN KEY (Address_Id) REFERENCES dbo.Addresses(Id);
ALTER TABLE dbo.Users
ADD CONSTRAINT Users_Address_Id_FK FOREIGN KEY (Address_Id) REFERENCES dbo.Addresses(Id);
We can also go with the following solution as well if I understood everything correct:
CREATE TABLE dbo.Stores
(
Id INT NOT NULL IDENTITY(1, 1) PRIMARY KEY,
Name NVARCHAR(120) NOT NULL,
Address_Id INT NULL,
Latitude FLOAT NULL,
Longitude FLOAT NULL
);
CREATE TABLE dbo.Users
(
Id INT NOT NULL IDENTITY(1, 1) PRIMARY KEY,
Name NVARCHAR(120) NOT NULL,
Address_Id INT NULL,
Latitude FLOAT NULL,
Longitude FLOAT NULL
);
CREATE TABLE dbo.Addresses
(
Id INT NOT NULL IDENTITY(1, 1) PRIMARY KEY,
Street NVARCHAR(120) NOT NULL,
PostalCode NVARCHAR(12) NOT NULL,
City NVARCHAR(40) NOT NULL
);
ALTER TABLE dbo.Stores
ADD CONSTRAINT Stores_Address_Id_FK FOREIGN KEY (Address_Id) REFERENCES dbo.Addresses(Id);
ALTER TABLE dbo.Users
ADD CONSTRAINT Users_Address_Id_FK FOREIGN KEY (Address_Id) REFERENCES dbo.Addresses(Id);