I need to create two tables in relationship one to many. I have created following statements. Maven gives me an error about "unique constraint". I don't know how to fix it, someone can explain me how to create correct relationship in this example?
CREATE TABLE Owner(
owner_id INT GENERATED BY DEFAULT AS IDENTITY,
firstname varchar(20),
lastname varchar(20)
)
"CREATE TABLE Picture(
picture_id INT GENERATED BY DEFAULT AS IDENTITY,
owner_id INT, name varchar(20),
width INT, height INT,
FOREIGN KEY(pic_owner_id) REFERENCES Owner(owner_id)
)
You need to add a primary key.
CREATE TABLE Owner(
owner_id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
firstname varchar(20),
lastname varchar(20)
)
Related
I'm very new to database designing and I'm creating a small project to understand it better, I'm trying to come up with a new school crm. I started with the below schema:
create table student(
id int not null,
firstname varchar(100),
lastname varchar(100),
PRIMARY key (id)
);
create table address(
id int not null,
street varchar(30) not null,
city varchar(30) not null,
PRIMARY KEY (id)
);
-- they have own roles for app
create table guardians(
id int not null,
firstname varchar(100),
lastname varchar(100),
primary key (id)
);
-- teacher have own roles
create table teacher(
id int not null,
firstname varchar(100),
lastname varchar(100),
PRIMARY key (id)
)
guardians and teachers will have password for login the web portal. The reason why I created separate table is mainly because teachers might have column's like salary, schoolId etc in the future. But now if I look at the schema here, mostly the firstName, lastname is common for all student, teacher and guardians.
How can I have a common table for such repeatable field names? Or this design is good? Also I wanted to have another table ROLE which going to give permissions at app level to update/insert certain tables. Not sure how can I design it as well.
The schema you have planned will end up having and lot of redundancies in different tables (like firstName and lastName as you have described for each of the three tables). Also, if in future a new role turns up you will be creating new table and leading to more redundancy.
I would suggest it would we better if you create one table for user and define role of the user in that table.
e.g.:
create table user(
u_id int not null,
firstname varchar(100),
lastname varchar(100),
role varchar(100),
PRIMARY key (id)
);
You can maintain role specific data in different table and can be accessed via primary key of user table.
e.g.:
create table teacher(
t_id int not null,
u_id int,
Salary int,
qualification varchar(100),
PRIMARY key (id),
FOREIGN KEY (u_id) REFERENCES user(u_id)
)
Having a role column in user table will solve your second problem as well.
I have question with a ER diagram I'm creating for a database, I basically have 2 tables, one is called 'Employee' and the other is called 'Store'. Each store has certain number of employees, I need to know in what store the employee is located. The thing is, A store is required to have a boss/manager who has to be one of the employees. The picture below shows how I made it. However when I create these tables on PostgreSQL I get an error because I'm referencing a table who has not been created yet.
This is how I'm trying to create the two tables, how can I solve this?
Any ideas?
create table Store(
store_id serial primary key,
storeName varchar(20),
employee_id int,
foreign key (employee_id) references Employee(employee_id)
);
create table Employee(
employee_id serial primary key,
firstname varchar(50),
lastname varchar(50),
address varchar(50)
email varchar(100),
store_id int,
foreign key (store_id) references Store(store_id)
);
Error:
SQL Error [42P01]: ERROR: relation "employee" does not exist
All you would have to do is create the foreign key constraints after the tables have been created like this
create table Store(
store_id serial primary key,
storeName varchar(20),
employee_id int
);
create table Employee(
employee_id serial primary key,
firstname varchar(50),
lastname varchar(50),
address varchar(50),
email varchar(100),
store_id int
);
alter table Store add constraint fk_store_employee_id foreign key (employee_id) references Employee(employee_id) DEFERRABLE INITIALLY DEFERRED;;
alter table Employee add constraint fk_employee_store_id foreign key (store_id) references Store(store_id) DEFERRABLE INITIALLY DEFERRED;;
the DEFERRABLE INITIALLY DEFERRED makes it that the constraint is checked at the end of the transaction otherwise you would get a foreign key constraint violation since the employee wouldn't exist when you create the store and the store wouldn't exist when you create the employee.
here is a working example https://www.db-fiddle.com/f/71b3KhwDMSgKmsWsbAeUTR/1
but even with the DEFERRABLE INITIALLY DEFERRED how would you know what id an employee and store is going to get?
i think you might be better off using a third table to store store managers like this
create table Store(
store_id serial primary key,
storeName varchar(20)
);
create table Employee(
employee_id serial primary key,
firstname varchar(50),
lastname varchar(50),
address varchar(50),
email varchar(100),
store_id int,
foreign key(store_id) references Store(store_id)
);
create table StoreManager (
store_id int,
employee_id int,
primary key(store_id, employee_id),
foreign key(store_id) references Store(store_id),
foreign key(employee_id) references Employee(employee_id)
);
this you can first create the store then the employee and then you can pair the two in the StoreManager table
here is a working example https://www.db-fiddle.com/f/71b3KhwDMSgKmsWsbAeUTR/2
I have one teacher_details table that has teacher_id as the primary key and one exam_details table which has exam_id as one attribute.
I want one teacher can't create the same exam_id more than one time but it can be possible that 2 teachers can create the same exam_id.
Should I make exam_id as the primary key or I will check that in the servlet?
I am using sql-server for database.
create table teacher_details(
teacher_id varchar(20) not null primary key,
teacher_name varchar(30));
create table exam_details(exam_id varchar(20), exam_name varchar(30), teacher_id varchar(20), foreign key (teacher_id) references teachers_details(teacher_id));
You should make the combination of exame_id and teacher_id the primary key of your exam_details table.
Also, you should get used to naming your constraints:
create table teacher_details
(
teacher_id varchar(20) not null,
teacher_name varchar(30),
constraint pk_teacher_details primary key (teacher_id)
);
create table exam_details (
exam_id varchar(20),
exam_name varchar(30),
teacher_id varchar(20),
constraint fk_teacher_exam foreign key (teacher_id) references teacher_details(teacher_id),
constraint pk_exam_details primary key (teacher_id, exam_id)
);
You should check this in servlet, because if you make exam_id as primary key, than two teachers would not be able to create the same exam_id
Below is the CREATE script for all those tables
Table schools schema
CREATE TABLE schools
(
NAME VARCHAR(20),
levell VARCHAR(20),
adress VARCHAR(20),
main_language VARCHAR(20),
email VARCHAR(20),
information VARCHAR(80),
mission VARCHAR(80),
vision VARCHAR(80),
type VARCHAR(20),
fees REAL,
phone_number INT,
PRIMARY KEY (NAME, levell)
)
Table school_phones schema
CREATE TABLE school_phones
(
NAME VARCHAR(20) PRIMARY KEY,
phone_number INT,
FOREIGN KEY (NAME, phone_number) REFERENCES schools
)
--phone.name references school
Table elementary_schools schema
CREATE TABLE elementary_schools
(
NAME VARCHAR(20) FOREIGN KEY REFERENCES schools(NAME) ,
levell VARCHAR(20) FOREIGN KEY REFERENCES schools(levell) ,
PRIMARY KEY(NAME , levell)
)
--elementary.name references school
Table elementary_school_supplies schema
CREATE TABLE elementary_school_supplies
(
NAME VARCHAR(20) PRIMARY KEY FOREIGN KEY REFERENCES elementary_schools(
NAME),
supplies VARCHAR(20)
)
--Elementary_Schools.name references elementary
Table middle_schools schema
CREATE TABLE middle_schools
(
NAME VARCHAR(20) PRIMARY KEY FOREIGN KEY REFERENCES schools
)
--middle.name references school
Table high_schools schema
CREATE TABLE high_schools
(
NAME VARCHAR(20) PRIMARY KEY FOREIGN KEY REFERENCES schools
)
The errors are displayed in the below pictures
Error1:
Error2:
Any help to solve this would be appreciated. Thanks in Advance.
Your first issue is that your PK on Schools is a composite of name and levell. You're trying to reference these with separate FK's, you need a composite FK too.
You want to do the same thing that you've done with the School_Phones table, something like this;
CREATE TABLE Elementary_Schools (name varchar(20), levell varchar(20), primary key (name, levell), foreign key (name, levell) references Schools)
The second issue is pretty self explanatory, you can only FK fields of the same data type. Your phone_number field (int) cannot reference levell as it's a varchar(20). These need to both be the same data type.
I am writing a query to create a database in SQL server 2008 R2. I plan to use it with entity framework in a mvc3 application
I have users, games and highscores.
What I want ot do is this:
I want to create a many-to-many relationship between users and games.
Each User can have multiple games and each game can have multiple users.
and each relation user-game has 1 high score.
So far I created the users, games, highscores tables:
CREATE TABLE Users
(
UserId INT IDENTITY(1,1),
FirstName VARCHAR(64) NOT NULL,
LastName VARCHAR(64) NOT NULL,
Email VARCHAR(200) NOT NULL,
CreatedDate DateTime NULL,
PhoneNumber VARCHAR(32) NULL,
Password VARCHAR(64),
CONSTRAINT pk_users_userid PRIMARY KEY(UserId)
)
CREATE TABLE Games
(
GameId INT IDENTITY(1,1),
Url VARCHAR(64) NOT NULL,
CONSTRAINT pk_gamess_gameid PRIMARY KEY(GameId)
)
CREATE TABLE Highscores
(
HighscoreId INT IDENTITY(1,1),
Value INT NOT NULL,
CONSTRAINT pk_surveyors_surveyorid PRIMARY KEY(HighscoreId)
)
What is the best way to create the many-to-many user-game relation and give each relation one high score? should I create a 4th table to hold the relation? And if I do so will I be able to use the database with Entity framework in an MVC3 application or would that complicate it?
Thank you
Additional note: I am planning to use this database with entity framework in a MVC 3 web application.
CREATE TABLE Users (
UserId INT IDENTITY(1,1),
FirstName VARCHAR(64) NOT NULL,
LastName VARCHAR(64) NOT NULL,
Email VARCHAR(200) NOT NULL,
CreatedDate DateTime NULL,
PhoneNumber VARCHAR(32) NULL,
Password VARCHAR(64),
CONSTRAINT pk_users_userid PRIMARY KEY(UserId)
)
CREATE TABLE Games (
GameId INT IDENTITY(1,1),
Url VARCHAR(64) NOT NULL,
CONSTRAINT pk_gamess_gameid PRIMARY KEY(GameId)
)
CREATE TABLE Highscores (
HighscoreId INT IDENTITY(1,1),
UserID Int,
GameID Int,
Value INT NOT NULL,
CONSTRAINT pk_surveyors_surveyorid PRIMARY KEY(HighscoreId)
)
ALTER TABLE [dbo].[Highscores] ADD CONSTRAINT [FK_Highscores_Games] FOREIGN KEY([GameID])
ALTER TABLE [dbo].[Highscores] ADD CONSTRAINT [FK_Highscores_Users] FOREIGN KEY([UserID])