Creating Entity Relationship Diagram - sql

I created the ERD of my system and now I would like to create a SQL Code.
So should a SQL Code look like this?:
CREATE TABLE Student
(
StudentID INT NOT NULL IDENTITY PRIMARY KEY,
FirstName VARCHAR(255),
LastName VARCHAR(255),
ADDRESS VARCHAR(255),
PhoneNumber VARCHAR(255),
Email VARCHAR(255),
GroupID INT NOT NULL FOREIGN KEY
);

Your problem is with the FOREIGN KEY part of your query, you are not defining the foreign key there. If you now remove that, your query will work, but without a defined FK:
CREATE TABLE Student
(
StudentID INT NOT NULL IDENTITY PRIMARY KEY,
FirstName VARCHAR(255),
LastName VARCHAR(255),
ADDRESS VARCHAR(255),
PhoneNumber VARCHAR(255),
Email VARCHAR(255),
GroupID INT NOT NULL
);
If you want to create the foreign key, you need to do something like this (with the correct table and column):
CREATE TABLE Student
(
StudentID INT NOT NULL IDENTITY PRIMARY KEY,
FirstName VARCHAR(255),
LastName VARCHAR(255),
ADDRESS VARCHAR(255),
PhoneNumber VARCHAR(255),
Email VARCHAR(255),
GroupID INT NOT NULL REFERENCES Group(Group_ID)
);

Your "FOREIGN KEY" declaration is incomplete - you need to say which table/column the foreign key references.
If you just want to get the table built,
CREATE TABLE Student
(
StudentID INT NOT NULL IDENTITY PRIMARY KEY,
FirstName VARCHAR(255),
LastName VARCHAR(255),
ADDRESS VARCHAR(255),
PhoneNumber VARCHAR(255),
Email VARCHAR(255),
GroupID INT NOT NULL);
should work.

Related

Create an auto incrementing primary key in DuckDB

Many database engines support auto-incrementing primary keys, and I would like to use this approach in my new DuckDB approach, but I can't figure out how to set it up. For example, in MySQL:
CREATE TABLE Persons (
Personid int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (Personid)
);
create a table:
CREATE TABLE Persons (
Personid integer primary key,
LastName varchar(255) not null,
FirstName varchar(255),
Age integer
);
create a sequence:
CREATE SEQUENCE seq_personid START 1;
Insert some data:
INSERT INTO Persons VALUES (nextval('seq_personid'), 'Doe', 'John', 99);

table in ms sql with primary key and auto_increment problem

hi i found an answer here and used the exemple
CREATE TABLE Persons (
Personid int IDENTITY(1,1) PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
but when i enter records, it does not allow duplicates in column lastname?!
i would expect id 3 and "hans" but it makes NULL?! it shouldnt be a problem that there is again name hans in row 3 ...
what do i wrong?
If do you want to do autoincrement on Personid, you should do:
CREATE TABLE Persons (
Personid int AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (Personid) );
And if do you want insert something into:
INSERT INTO Persons (LastName, FirstName, Age int) VALUES (..,..,..)
I hope I've helped.

Why Auto_Increment not working in SQL?

CREATE TABLE Persons
(
ID int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
When I run this code, it is not run successfully.
Syntax for MySQL
CREATE TABLE Persons (
ID int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
Syntax for SQL Server
CREATE TABLE Persons (
ID int IDENTITY(1,1) PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
Syntax for Oracle
CREATE SEQUENCE seq_person
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 10;
If this is Microsoft SQL Server then change to IDENTITY instead of Auto_Number. If it's Postgres then I believe you want SERIAL as the datatype instead of INT. If this is MySQL then it should work as-is.
What RDBMS are you using?

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;

How can I re-design with fewer tables?

I have designed a database for online donation. I have not added foreign key relationships because first I want to achieve a simple design. It's currently 11 tables, and it seems already more complicated than necessary.
The first one is registration table. The other 8 are different item types which will be donated by donors or by receivers, and 1 will be for post table it means when the user or receiver wants donate they will post what they want to donate or what they want ask others to give them. The last one will be for comment, the comment might be on the post by donors or receivers.
Can any one can help me re-design this in a simpler way?
Create Table Registration
(
ID int NOT NULL primary key,
FirstName varchar(25) NOT NULL,
LastName varchar(25) NOT NULL,
Username varchar(20),
Gender varchar(10),
Home_Address varchar(100),
Office_Address varchar(100),
City varchar(25),
State varchar(25),
Zip varchar(25),
Contact_No int ,
Email varchar(25)
);
2 posts
CREATE TABLE Post
( p_id int constraint p_pk primary key,
Username varchar(25),
Status varchar(25),
image nvarchar(max),
date_time varchar(100)
);
3,
CREATE TABLE Books
(
ISBN nvarchar(200) constraint ISBN_pk primary key,
Username varchar(25),
Book_title varchar(25),
Authorname varchar(25),
Publicationdate varchar(25),
Purchasedate varchar(25),
Book_edition varchar(25)
);
4,
CREATE TABLE images
(I_id int constraint i_pk primary key,
Username varchar(25),
Title varchar(25),
url varchar(50),
description varchar(100)
);
5,
CREATE TABLE laptop
(
L_id int constraint L_pk primary key,
Username varchar(25),
Model varchar(25),
Speed varchar(25),
Ram varchar(25),
HD varchar(50),
Screen varchar(50)
);
6, other items
CREATE TABLE other_items
(O_id int constraint O_pk primary key,
Username varchar(25),
Item_title varchar(25),
Item_type varchar(25),
Item_description varchar(100),
Itempicture varchar(100)
);
7, recipient
CREATE TABLE Recipient
(R_id int constraint R_pk primary key,
Firstname varchar(25),
Lastname varchar(25),
National_id_no varchar(50),
Address varchar(100),
Contact_no varchar(100)
);
8
CREATE TABLE Shoesimages
(
s_id int constraint Rss_pk primary key,
Username varchar(25), standard varchar(25),
Gender varchar(25),
Colour varchar(25),
Description varchar(100)
);
9,
CREATE TABLE uniform
(U_id int constraint u_pk primary key,
Username varchar(25),
Standard varchar(25),
Gender varchar(25),
Colour varchar(25),
Description varchar(100)
);
10,
CREATE TABLE Research_paper
(
Rs_id int constraint Rs_pk primary key,
Username varchar(25),
title varchar(25),
authorname varchar(25),
year_of_publish varchar(25),
venu varchar(100)
);
11,
CREATE TABLE Comments
(
C_id int constraint C_pk primary key,
Username varchar2(25),
comment nvarchar(max),
date_time varchar2(100)
);