unable to create primary key on phpmyadmin sql - sql

this is my code
CREATE TABLE orders(
id integer NOT NULL,
name varchar(12),
orderno int,
city varchar(12),
price int
PRIMARY KEY ('id')
);
please check this image for error

try this
CREATE TABLE orders(
id int NOT NULL PRIMARY KEY,
name varchar(12),
orderno int,
city varchar(12),
price int
);

While Creating Table you can Declare the primary key Attribute with the type of the attribute. like.
CREATE TABLE orders(
id integer PRIMARY KEY NOT NULL,
name varchar(12),
orderno int,
city varchar(12),
price int);

Related

SQL Can't create Foreign key

I'm sure my question would be kinda silly, but I can't solve it. Here is my issue:
create table Product
(
ProductID int PRIMARY KEY,
ProductName varchar(16),
RetrailPrice int,
WholesalePrice int,
MonthDelivery int,
Waste int,
StorageName varchar(16)
)
create table Storage
(
StorageID int PRIMARY KEY,
StorageName varchar(16),
City varchar(16),
Employees int,
Area int,
ProductID int
)
And of course I want to link them
alter table Product
add FOREIGN KEY (StorageName) references Storage(StorageName)
But I can't do it, I'm wrong somewhere :/
You have to reference the PK from the Storage table. StorageName is a poor choice for a FK. What if there are Storages with the same name in different cities?
Replace the StorageName in the Product table with StorageID
create table Product(
ProductID int PRIMARY KEY,
ProductName varchar(16),
RetrailPrice int,
WholesalePrice int,
MonthDelivery int,
Waste int,
StorageID int) --this has chnaged, it will hold the id/PK of the Storage
create table Storage(
StorageID int PRIMARY KEY,
StorageName varchar(16),
City varchar(16),
Employees int,
Area int,
ProductID int)
And then:
alter table Product
add FOREIGN KEY (StorageID) references Storage(StorageID)

Database design with creating a product that has two prices

I'm creating a database design for our water refilling system and I'm just new to databases. I am stuck with creating table that provides two different prices for a product. To further explain my problem, here's an example, a product's ('5 GALLON') price changes when it is delivered or bought on point by a customer. For example a delivered ('5 GALLON') is 45 pesos while a bought on point gallon is only 40 pesos. Can someone help me please?
Here's my codes so far
create table Product (
product_id int primary key,
prodtype_id int,
product_name varchar(55),
product_quantity int
)
-----NOT SURE IF THESE TWO TABLES ARE CORRECT
create table DeliveryPrice (
prod_id int,
product_price money,
foreign key (prod_id) references Product
)
create table OnPointPrice(
prod_id int,
product_price money,
foreign key (prod_id) references Product
)
You're likely better off just having the two prices in the Product table. They are attributes of the product, so that's where they belong.
Also, you should specify which columns are NOT NULL in your database (which should be most of them).
So is this correct?
create table Product(
product_id int primary key,
product_name varchar(55) not null,
product_quantity int not null,
pickup_price money not null,
delivery_price money not null
)
create table Customer(
customer_id int primary key,
customer_name varchar (255) not null,
customer_address varchar(200),
customer_phone int
)
create table INVOICE(
inv_number int primary key,
customer_id varchar(5),
foreign key (customer_id) references Customer,
inv_date date not null,
bought_mode char(10) not null
)
create table LINE(
INV_NUMBER int,
foreign key (INV_NUMBER) references INVOICE,
LINE_NUMBER INT not null,
PRIMARY KEY (INV_NUMBER, LINE_NUMBER),
line_quantity int not null,
line_price money not null
)

ERROR: No unique constraint matching given keys for referenced table

For some reason I'm getting an error* in my code. I'm quite new to PostgreSQL, and simply SQL. What is causing this error?
*there is no unique constraint matching given keys for referenced table "tech".
BEGIN;
CREATE TABLE Person (
person_id SERIAL PRIMARY KEY,
firstname VARCHAR(128),
lastname VARCHAR(128),
email_adr VARCHAR(128),
UNIQUE(person_id, email_adr)
);
CREATE TABLE Phone (
person_id INT REFERENCES Person(person_id),
phone_nr INT PRIMARY KEY,
UNIQUE(phone_nr)
);
CREATE TABLE Tech (
tech_id INT REFERENCES Person(person_id),
username VARCHAR(80) PRIMARY KEY,
password VARCHAR(80) NOT NULL,
location Varchar(128),
UNIQUE(username, tech_id)
);
CREATE TABLE Customer (
customer_id INT REFERENCES Persons(person_id),
addresse VARCHAR(255) NOT NULL,
UNIQUE(customer_id)
);
CREATE TABLE Task (
task_id SERIAL PRIMARY KEY,
payment MONEY,
tech INT REFERENCES Tech(tech_id) NOT NULL,
customer INT REFERENCES Customer(customer_id) NOT NULL,
start_date DATE NOT NULL,
end_dato DATE,
UNIQUE(tech, customer, start_date, end_date)
);
COMMIT;
In table Task you trying to reference to table Tech by tech_id. To do that you must add UNIQUE CONSTRAINT to tech_id in Tech.
Right now in table Tech you have UNIQUE(username, tech_id) that means that values in column tech_id could by doubled Ex.
Tech
-------------------------------
tech_id username, ....
------------------------------
1 'John'
2 'Tony'
1 'Nataly'
Acctually the better idea is to set reference by PRIMARY KEY, so in your case username in table Tech.
If you want to leave structure the way present in question, you should just add UNIQUE(tech_id) in column Tech.
What do you think of this code?
BEGIN;
CREATE TABLE Person (
person_id SERIAL PRIMARY KEY,
firstname VARCHAR(128),
lastname VARCHAR(128),
email_adr VARCHAR(128),
UNIQUE(person_id),
UNIQUE(email_adr)
);
CREATE TABLE Phone (
person_id INT,
phone_nr INT PRIMARY KEY,
);
CREATE TABLE Tech (
tech_id INT,
username VARCHAR(80) PRIMARY KEY,
password VARCHAR(80) NOT NULL,
location Varchar(128),
FOREIGN KEY(tech_id) REFERENCES Person(person_id),
UNIQUE(username),
UNIQUE(tech_id)
);
CREATE TABLE Customer (
customer_id INT REFERENCES Persons(person_id),
addresse VARCHAR(255) NOT NULL,
FOREIGN KEY(tech_id) REFERENCES Person(person_id),
UNIQUE(customer_id)
);
CREATE TABLE Task (
task_id SERIAL PRIMARY KEY,
payment MONEY,
tech varchar(80) REFERENCES Tech(username) NOT NULL,
customer INT REFERENCES Customer(customer_id) NOT NULL,
start_date DATE NOT NULL,
end_dato DATE,
UNIQUE(tech, customer, start_date, end_date)
);
COMMIT;

Sturctured Query Language

create table customer(
custno number(10) constraint foo primary key,
custname character(15),
city character(15),
phone number(10));
create table invoice(
invo number(10) constraint inv primary key,
invdate date(10),
constarint c references customer(custno));
I cannot able to create the second table and am having doubt of the data type date and If I neglected that attribute(invdate) still I cannot able to create the second table.Reason please.
create table customer
( custno int constraint foo primary key
,custname character(15)
,city character(15)
,phone int
)
go
create table invoice
( invo int constraint inv primary key
constraint ck references customer(custno)
,invdate date
)

How do I create a table with a foreign key reference?

Here are my two tables I created.
CREATE TABLE Employee
(
EmpID int IDENTITY(1,1) PRIMARY KEY,
LastName VARCHAR(50) NOT NULL,
FirstName VARCHAR(50) NOT NULL,
StreetAddress VARCHAR(75),
City VARCHAR(255),
State VARCHAR(25),
ZipCode VARCHAR(5),
EmployeeType VARCHAR (20),
HourlyWage DECIMAL(18,2)
)
CREATE TABLE WagesPayable
(
FOREIGN KEY (EmpID) REFERENCES Employee (EmpID),
WorkedHours DECIMAL(18,2),
PayRate DECIMAL(18,2),
TotalPayable AS (WorkedHours * PayRate),
DateLastPaid DATETIME
)
Whenever I try and create the WagesPayable table I get the following error:
Msg 1769, Level 16, State 1, Line 26
Foreign key 'EmpID' references invalid column 'EmpID' in referencing table 'WagesPayable'.
What am I missing here? Your help is much appreciated.
You need to declare the column before the reference. You can do this all in one step:
CREATE TABLE WagesPayable
(
EmpID int REFERENCES Employee (EmpID),
WorkedHours DECIMAL(18,2),
PayRate DECIMAL(18,2),
TotalPayable AS (WorkedHours * PayRate),
DateLastPaid DATETIME
)
The SQL Fiddle is here.
You aren't creating a column EmpID for table WagesPayable.
So first you need to create the column, and then reference it as a foreign key like this :
CREATE TABLE WagesPayable
(
EmpID int,
WorkedHours DECIMAL(18,2),
PayRate DECIMAL(18,2),
TotalPayable AS (WorkedHours * PayRate),
DateLastPaid DATETIME,
FOREIGN KEY (EmpID) REFERENCES Employee (EmpID)
)