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
)
Related
create database my_data
create table tblperson(
pid int primary key,
pname varchar(15),
pgender varchar(15)
)
create table tblgender(
gid varchar(15) primary key,
gender varchar(15)
)
Alter table tblperson add constraint giFK
Foreign key (pgender) references tblgender(gid)
I have 5 tables in a DB like this (script below)::
DB SCRIPT
CREATE TABLE EQUIPE (
code_equipe char(3) primary key,
nom varchar(30),
directeur varchar(30));
CREATE TABLE PAYS (
code_pays varchar(3) primary key,
nom varchar(20));
CREATE TABLE COUREUR (
num_dossart number(3) primary key,
code_equipe char(3),
nom varchar(30),
code_pays varchar(3));
CREATE TABLE ETAPE (
num_etape number(1) primary key,
date_etape date,
kms number(3),
ville_depart varchar(20),
ville_arrivee varchar(20));
CREATE TABLE TEMPS (
num_dossart number(3),
num_etape number(1),
temps_realise number(6),
primary key(num_dossart,num_etape));
ALTER table COUREUR add CONSTRAINT FK_avoir_code_equipe FOREIGN KEY (code_equipe) REFERENCES EQUIPE(code_equipe);
ALTER table COUREUR add CONSTRAINT FK_avoir_code_pays FOREIGN KEY (code_pays) REFERENCES PAYS(code_pays);
ALTER table TEMPS add CONSTRAINT FK_avoir_num_dossart FOREIGN KEY (num_dossart) REFERENCES COUREUR(num_dossart);
ALTER table TEMPS add CONSTRAINT FK_avoir_num_etape FOREIGN KEY (num_etape) REFERENCES ETAPE(num_etape);
and my query
select num_etape,max(temps_realise) from TEMPS group by num_etape
gave this result
and i want to update it to give result like this
this is
If I read it correctly, a simple join does the job:
select t.num_etape,
c.nom,
max(n.temps_realise)
from temps t join coureur c on c.num_dossart = t.num.dossart
group by t.num_etape,
c.nom;
I have solved my question thanks guys
select num_dossart,nom,num_etape,dernier from COUREUR C,
(select num_etape ,max(temps_realise) as dernier from TEMPS GROUP BY num_etape) T
where num_dossart =
(select num_dossart from TEMPS where temps_realise = dernier )
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;
This question already has an answer here:
ORA-00907: missing right parenthesis
(1 answer)
Closed 8 years ago.
I tried to create Foreign key and PRIMARY KEY
My code is ok with Table1
CREATE TABLE Persons
(
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
Table2:
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int(10),
P_Id int FOREIGN KEY REFERENCES Persons(P_Id)
);
error :-ORA-00907: missing right parenthesis
Don't know what the error is.
When you declare the FK "inline" you must not specify the foreign key keyword:
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int(10),
P_Id int REFERENCES Persons(P_Id)
);
Btw: you don't even need to list the column name when using an inline definition, P_Id int REFERENCES Persons would be enough.
That will generate a system-named constraint (e.g. SYS_C0066866), so it's generally better to use a format where you can specify the name of the constraint:
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int(10),
P_Id int,
constraint fk_orders_person foreign key (p_id) REFERENCES Persons(P_Id)
);
If you're inlining your foreign key definition in the column's definition, you don't need the foreign key phrase:
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int(10),
P_Id int REFERENCES Persons(P_Id)
);
For MySql
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int(10),
P_Id int,
FOREIGN KEY(P_Id) REFERENCES Persons(P_Id)
);
For SQL
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int(10),
P_Id int FOREIGN KEY REFERENCES Persons(P_Id)
);
Check Manual
I have Assignment due in which i'm stuck on a question.
Add a “Sales Detail” table to your database. This table is related to the Orders and Products tables. It shows the product and quantity ordered at least (add other fields if you wish but explain why you added them on your paper).
There is no description of this table on the diagram provided. Use your best database design skills here!
Create Table SalesDetail
(
SaleDetailID int,
ProductID char(5),
ManufactureID char(3) not null,
OrderNo int,
qtyOrdered int
PRIMARY
)
Alter Table SalesDetail
Add FOREIGN KEY (ProductID)
REFERENCES Products(ProductID)
My Error is I can not get it to link SalesDetail table to Products table.
Msg 1776, Level 16, State 0, Line 1
There are no primary or candidate keys in the referenced table 'Products' that match the referencing column list in the foreign key 'FK__SalesDeta__Produ__5EBF139D'.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.
Create Table Customers
(
CustomerNo char(4)
Constraint ck_CustomerNoHas4positionsWithNumbers
Check(CustomerNo like'[0-9],[0-9],[0-9],[0-9]'),
Company varchar(50) not null,
CustomerRep char(3),
CreditLimt money default(20000.00),
PRIMARY KEY(CustomerNo)
)
Create Table Salesreps
(
EmployeeNo char(3)
Constraint ck_EmployeeNoHasDigits check(EmployeeNo like'[0-9],[0-9],[0-9]'),
FirstName varchar(25) not null,
LastName varchar(25) not null,
Age int,
SalesRepOffice char(2) not null,
Title varchar(50),
HireDate Date not null,
Manager char(3) not null,
Quota money,
Sales money not null,
PRIMARY KEY(EmployeeNo)
)
Create Table Offices
(
Office char(2) Constraint ck_checkOfficeHasNumbersOnly check(Office like'[0-9],[0-9]'),
City varchar(25) not null,
Region varchar(10) not null,
Manager char(3) not null,
Target money,
Sales money not null
PRIMARY KEY(Office)
)
Create Table Orders
(
OrderNo int,
OrderDate Date not null,
CustomerNo char(4) not null,
SalesRep char(3) not null
PRIMARY KEY(OrderNo)
)
Create Table Products
(
ManufactureID char(3)
Constraint ck_ManufactureIDifItHasLettersOnly check(ManufactureID like'[a-z],[a-z],[a-z]'),
ProductID char(5)
Constraint ck_ProductIDhasTwoLettersAndThreeNumbers check(ProductID like'[0-9],[0-9],[a-z],[a-z],[a-z]'),
Description varchar(50) not null,
Price money not null,
QtyOnHand int not null,
PRIMARY KEY(ManufactureID, ProductID)
)
--Add Foreign Keys to all tables who needs them
Alter Table Customers
Add constraint fk_customerrep
FOREIGN KEY (CustomerRep)
REFERENCES Salesreps(EmployeeNo)
Alter Table Salesreps
Add constraint fk_salesrepoffice
FOREIGN KEY (SalesRepOffice)
REFERENCES Offices(Office),
constraint fk_manager
FOREIGN KEY (Manager)
REFERENCES Salesreps(EmployeeNo)
Alter Table Offices
Add constraint fk_officesmanger
FOREIGN KEY (Manager)
REFERENCES Salesreps(EmployeeNo)
Alter Table Orders
Add constraint fk_customerno
FOREIGN KEY (CustomerNo)
REFERENCES Customers(CustomerNo),
constraint fk_salesrep
FOREIGN KEY (SalesRep)
REFERENCES Salesreps(EmployeeNo)
The table Products has a composite key (ManufactureID, ProductID), so you cannot uniquely identify a product by just the ProductId. Therefore you have to create a composite foreign key that references to both ManufactureId and ProductID:
Alter Table SalesDetail
Add FOREIGN KEY (ManufactureId, ProductID)
REFERENCES Products(ManufactureID, ProductID)
ProductID is not a primary key like the error says. In your code
PRIMARY KEY(ManufactureID, ProductID)
This creates a primary key that both of those columns combined.
The primary key for Products is (ManufactureID, ProductID). So the SalesDetail table should contain both these columns, and both should be part of the foreign key constraint:
Alter Table SalesDetail
Add FOREIGN KEY (ManufactureID, ProductID)
REFERENCES Products(ManufactureID, ProductID)