Need Help To Create DataBase? - sql

I Going To Create A DataBase in SQL Server 2014 But I Have Problem .
A Need Use This Option : When The User Want To Register , Select Country , City Of The Country Display And Select it .
For Example : When User Select The U.S.A , Display (NewYourk , Washington , . . . )
Pic Of Prog
CREATE TABLE orders
(
OrderID INT IDENTITY (1,1) NOT NULL PRIMARY KEY,
Fname VARCHAR(50) NOT NULL,
Lname VARCHAR(50) NOT NULL,
Tel VARCHAR(15),
Counts INT NOT NULL,
DaysID INT NOT NULL,
CountryID INT NOT NULL,
CityID INT NOT NULL,
Address VARCHAR(1024) NOT NULL,
FOREIGN KEY (DaysID) REFERENCES WeekDays(DaysID),
FOREIGN KEY (CountryID) REFERENCES Country(ContryID),
FOREIGN KEY (CityID) REFERENCES City(CityID)
)
GO
CREATE TABLE WeekDays
(
DaysID INT IDENTITY (10000001,1) NOT NULL PRIMARY KEY,
DaysName VARCHAR(50) NOT NULL
)
GO
CREATE TABLE Country
(
CountryID INT IDENTITY (2000000,1) NOT NULL PRIMARY KEY,
CountryName VARCHAR(100)
)
CREATE TABLE City
(
CityID INT IDENTITY (2000000,1) NOT NULL PRIMARY KEY,
CityName VARCHAR(100)
)

For that you must have a FOREIGN KEY of CountryID in City table so that you can Fetch the cities of USA
CREATE TABLE City
(
CityID INT IDENTITY (2000000,1) NOT NULL PRIMARY KEY,
CityName VARCHAR(100)
CountryID INT
FOREIGN KEY (CountryID ) REFERENCES Country(CountryID )
)
--FETCH RECORDS
SELECT * FORM City
WHERE CountryID=1 -- OR whatever the id of the Country

Related

Direct relation in SQL

I'm a beginner in SQL and I'm trying to make a small database, but I was asked to make a direct relation between OrderNumber in Orders table and CustomerNumber in Customers table, aren't I using a direct relation in here?.
Here is my code:
Customers table:
CREATE TABLE Customers
(
CustomerNumber VARCHAR(25) PRIMARY KEY NOT NULL,
CustomerName VARCHAR(50),
Phone INT NOT NULL,
Country VARCHAR(50),
City VARCHAR(50),
State VARCHAR(50),
PostalCode VARCHAR(5) NOT NULL
);
Orders table:
CREATE TABLE Orders
(
OrderNumber VARCHAR(25) ,
CustomerNumber VARCHAR(25) PRIMARY KEY NOT NULL,
ProductName VARCHAR(200) NOT NULL UNIQUE,
OrderDate DATE NOT NULL,
requiredDate DATE NOT NULL,
Status VARCHAR(50)
);
I believe, you probably want something like this:
CREATE TABLE Customers
(
CustomerNumber INT PRIMARY KEY NOT NULL,
CustomerName VARCHAR(50),
Phone INT NOT NULL,
Country VARCHAR(50),
City VARCHAR(50),
State VARCHAR(50),
PostalCode VARCHAR(5) NOT NULL
);
CREATE TABLE Orders
(
OrderNumber INT PRIMARY KEY NOT NULL,
CustomerNumber INT NOT NULL FOREIGN KEY REFERENCES Customers(CustomerNumber),
ProductName VARCHAR(200) NOT NULL UNIQUE,
OrderDate DATE NOT NULL,
requiredDate DATE NOT NULL,
Status VARCHAR(50)
);
Changed the datatype of CustomerNumber and OrderNumber to INT
Made Orders.CustomerNumber a foreign key with a relation to the Customer table (CustomerNumber column)

ALTER TABLE statement conflicted with the FOREIGN KEY constraint

When creating the foreign key I came across this error. Below is my code.
create table tblPerson(
ID int not null primary key,
Fullname varchar(50) not null,
Email varchar(50) not null,
GenderId int
)
create table tblGender (
ID int not null primary key,
Gender varchar(50) not null
)
alter table tblPerson add constraint tblPerson_GenderId_FK
foreign key (GenderId) references tblGender(ID)
You want to identify any "do not align" rows....
I have made the below.
You won't be able to add the FK constraint, if any rows come back from the SELECT query.
I have also removed the hungarian notation for "tbl". I would advise against it.
create table dbo.Person(
ID int not null primary key,
Fullname varchar(50) not null,
Email varchar(50) not null,
GenderId int )
create table dbo.Gender (
ID int not null primary key,
Gender varchar(50) not null
)
/* any rows below? your FK creation will fail */
Select *, p.GenderId as 'HoustonWeHaveAProblemValue' from dbo.Person p Where Not Exists (Select 1 from dbo.Gender g where g.ID = p.GenderId)
alter table dbo.Person add constraint Person_GenderId_FK
foreign key (GenderId) references dbo.Gender(ID)
Hi please use the following code to achieve your goal :
1-First create tblGender:
create table tblGender (
ID int not null primary key,
Gender varchar(50) not null
)
2-Then create table tblPerson with the relationship between 2 tables since the beginning:
create table tblPerson(
ID int not null primary key,
Fullname varchar(50) not null,
Email varchar(50) not null,
GenderId int references tblGender(ID)
)
works fine.

Relationship 1 to n

create database shop1
use shop1
create table mathang
(
MatHangID INT primary key not null,
TenMatHang varchar(50),
SoLuong int not null,
Price int not null,
)
create table nhacungcap
(
MatHangID INT foreign key references mathang,
TenNhaCungCap varchar(50) ,
DiaChi varchar(100),
SoDienThoai int ,
CONSTRAINT pk_M_CC primary key (MatHangID)
)
create table khachhang
(
KhachHangID int not null primary key,
TenKhachHang varchar(50) not null,
[DiaChi] varchar(100) not null,
[SoDienThoai] varchar(50) not null,
)
create table donhang
(
DonHangID int references to khachhang(KhachHangID),
TenDonHang varchar(50),
SoLuong int,
CONSTRAINT pk_DHID primary key (DonHangID)
)
I can not find errors with this, it shows the foreign key error to add this relation ship. Any one please help.
Remove the to in
...
DonHangID int references to khachhang(KhachHangID)
...
to get
...
DonHangID int references khachhang(KhachHangID)
...
. The to doesn't belong there syntactically.

Address table applied to Users and Stores

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

Where clause in Mutiple table sql joins

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 ..