is it possible to create table with multiple one-to-one relationships?
I am tring to obtain it via below query but get SQL error.
Tables are:
Order:
id
cart id (1:1)
user id (1:1)
payment_method_id
shipping_method_id
total price
User
id
email
phone
first name
last name
address
post code
city
password NULL
Cart:
id
cookie
cartItem_id(1:many)
grandTotal
I would like to create table Order with two columns having one-to-one association.
create table order
( id int auto_increment
, cart_id int
, user_id int
, payment_method_id int
, shipping_method_id int
, total_price int
, primary key(user_id)
, primary key(cart_id));
I copied the below query but I am getting error and don't know why.
create table order(id int auto_increment, cart_id int, user_id int, payment_method_id int, shipping_method_id int, total_price int, primary key(id), foreign key (user_id) references user(id), foreign key (cart_id) references cart(id));
it says:
[42000][1064] You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near 'order(id int auto_increment, cart_id int, user_id int,
payment_method_id int, sh' at line 1
What is it I cannot see? I only changed Id into id.
Firstly, You can't have more than one Primary key on a Table.
Secondly, To have a relation you need to use Foreign Key
Try below Query:
create table order
( id int auto_increment
, cart_id int
, user_id int
, payment_method_id int
, shipping_method_id int
, total_price int
, primary key(id)
, FOREIGN KEY (user_id) REFERENCES User(Id)
, FOREIGN KEY (cart_id) REFERENCES Cart(Id));
Related
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.
I have 4 tables:
User
create table user (
id int not null identity(1,1) primary key,
name varchar(30),
soyad varchar(50),
city varchar(30),
e_mail varchar(100),
pass varchar(10)
);
Category
create table category(
id int not null primary key,
name varchar(50)
);
Competition
create table competition(
id int primary key,
date date,
category_id int foreign key references category(id)
);
Competition_User
create table competition_user(
id int primary key,
competition_id int foreign key references competition(id),
joker_id int foreign key references joker(joker_id),
user_id int foreign key references user(id),
point int
);
I want write SQL code for this:
Write the SQL query, which will list which of the most competing members from each category got the highest score, in which category it competed, and how many questions are correct and how many questions are wrong.
I write this SQL query but failure:
SELECT category.id, user.id, count(competition_user.competition.id) AS competitioncount
FROM user, competition_user, competition, category
WHERE competition.id = competition_user.competition_id AND compettion.category_id = category.id AND user.id = competition.user_id
GROUP BY category.id, user.id
ORDER BY competitioncount desc
Please help me, how can i get a list of users of the department with a filter by position?
CREATE TABLE COMPANY
(
ID INT GENERATED BY DEFAULT AS IDENTITY,
NAME VARCHAR(255),
PRIMARY KEY (ID),
UNIQUE KEY COMPANY_NAME (NAME)
);
CREATE TABLE USER
(
ID INT GENERATED BY DEFAULT AS IDENTITY,
NAME VARCHAR(255),
LASTNAME VARCHAR(255),
DATE_OF_BIRTH DATE ,
PRIMARY KEY (ID),
);
CREATE TABLE POSITION
(
ID INT NOT NULL GENERATED BY DEFAULT AS IDENTITY,
POSITION VARCHAR (50),
USERID INT NOT NULL,
PRIMARY KEY (ID),
FOREIGN KEY (USERID) REFERENCES USER(ID)
);
CREATE TABLE DEPARTMENT
(
ID INT GENERATED BY DEFAULT AS IDENTITY,
USER_ID INT NOT NULL,
COMPANY_ID INT NOT NULL,
DEPARTMENT_CATEGORY VARCHAR(50),
PRIMARY KEY (ID),
FOREIGN KEY (USER_ID) REFERENCES USER(ID),
FOREIGN KEY (COMPANY_ID) REFERENCES COMPANY(ID)
);
CREATE TABLE CATEGORY
(
ID INT NOT NULL GENERATED BY DEFAULT AS IDENTITY,
NAME VARCHAR (50),
PRIMARY KEY (ID)
);
CREATE TABLE DEPARTMENT_CATEGORY
(
DEPARTMENT_ID INT NOT NULL,
CATEGORY_ID INT NOT NULL,
FOREIGN KEY (DEPARTMENT_ID) REFERENCES DEPARTMENT(ID),
FOREIGN KEY (CATEGORY_ID) REFERENCES CATEGORY(ID)
);
Please help me!!
it look like you wanted to display results from department and position table that has both the data you need, but be specific about your filter (position) as u need to provide it in your where clause
select user_ID, department
from department as a
join user as b
on a. user_id = b. user_id
where position = 'put the specific position you wanted to be filtered here'
but your two userID columns are written differently as (User_id or USERID) correct that too not get failure result when you execute the task
I have the following code:
CREATE TABLE _CLIENT
(
client_id int ,
client_name varchar(50),
type varchar(50),
constraint _CLIENT_pk PRIMARY KEY(client_id),
constraint _CLIENT_ch CHECK (client_id>0),
typee_id INT NOT NULL REFERENCES CLIENT_TYPE(typee_id)
)
CREATE TABLE CLIENT_TYPE
(
typee_id int NOT NULL,
name_type varchar(50),
constraint CLIENT_TYPE_pk PRIMARY KEY(typee_id)
)
The foreign key throws an error saying:
Foreign key 'FK__Number__Name__1CF15040' references invalid table 'Users.Name'
what's the wrong here?
I don't know what exact error message you are getting, but you have error in the current script and I think you mean this error:
Foreign key 'FK___CLIENT__typee_i__55BFB948' references invalid table
'CLIENT_TYPE'.
You should first create CLIENT_TYPE table, so the script should look like:
CREATE TABLE CLIENT_TYPE
(
typee_id INT NOT NULL ,
name_type VARCHAR(50) ,
CONSTRAINT CLIENT_TYPE_pk PRIMARY KEY ( typee_id )
)
CREATE TABLE _CLIENT
(
client_id INT ,
client_name VARCHAR(50) ,
type VARCHAR(50) ,
CONSTRAINT _CLIENT_pk PRIMARY KEY ( client_id ) ,
CONSTRAINT _CLIENT_ch CHECK ( client_id > 0 ) ,
typee_id INT NOT NULL
REFERENCES CLIENT_TYPE ( typee_id )
)
As a general rule, you should first create base tables and then tables which depend on those ones.
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