Database management systems_pgadmin III - database

CREATE TABLE Products
(
pid INT NOT NULL,
pname varchar(50) NOT NULL,
price INT NOT NULL,
stock INT NOT NULL,
PRIMARY KEY (pid)
);
CREATE TABLE Customer
(
Cid INT NOT NULL,
Cname varchar(50) NOT NULL,
Caddress varchar(150) NOT NULL,
Ccontact varchar(20) NOT NULL,
PRIMARY KEY (Cid),
);
CREATE TABLE orders
(
orderid int not null,
quantity int not null,
purchased_on date not null,
totalprice float not null,
Cid INT NOT NULL,
Pid INT NOT NULL,
PRIMARY KEY (orderid),
FOREIGN KEY (pid) REFERENCES Products(pid)
FOREIGN KEY (cid) REFERENCES Customers(pid)
);
why the code is getting not able to run pgadmin III,
thanks.

All the scripts should be corrected as follows. Read the comments in front of corrected lines. All the capitalized column names and table names are placed inside double quotes.
CREATE TABLE "Products"
(
pid INT NOT NULL,
pname varchar(50) NOT NULL,
price INT NOT NULL,
stock INT NOT NULL,
PRIMARY KEY (pid)
);
CREATE TABLE "Customer"
(
"Cid" INT NOT NULL,
"Cname" varchar(50) NOT NULL,
"Caddress" varchar(150) NOT NULL,
"Ccontact" varchar(20) NOT NULL,
PRIMARY KEY ("Cid") -- Removed additional comma (,)
CREATE TABLE orders
(
orderid int not null,
quantity int not null,
purchased_on date not null,
totalprice float not null,
"Cid" INT NOT NULL,
"Pid" INT NOT NULL,
PRIMARY KEY (orderid),
FOREIGN KEY ("Pid") REFERENCES "Products"(pid), -- Added missing comma (,)
FOREIGN KEY ("Cid") REFERENCES "Customer"("Cid") -- Referenced table should be Customer not Customers and reference key should be Cid not pid
);

Related

Table contains no primary or candidate keys that match the referencing column list in the foreign key

This is my first table :
CREATE TABLE [dbo].[County]
(
[CountyId] INT IDENTITY (1, 1) NOT NULL,
[County] VARCHAR(50) NOT NULL,
CONSTRAINT [PK_County] PRIMARY KEY CLUSTERED ([CountyId] ASC)
);
This is my second table :
CREATE TABLE [dbo].[Theatre]
(
[TheatreId] INT IDENTITY (1, 1) NOT NULL,
[TheatreName] VARCHAR(50) NOT NULL,
[CountyId] INT NOT NULL,
CONSTRAINT [PK_Theatre]
PRIMARY KEY CLUSTERED ([TheatreId] ASC),
CONSTRAINT [FK_Theatre_County]
FOREIGN KEY ([CountyId]) REFERENCES [dbo].[County] ([CountyId])
);
This is my third table :
CREATE TABLE [dbo].[Movies]
(
[CinemaId] INT NOT NULL,
[CategoryId] INT NOT NULL IDENTITY(101, 1),
[CinemaName] VARCHAR(50) NOT NULL,
[TheatreId] INT NOT NULL,
[ShowTimings] TIME (7) NOT NULL,
CONSTRAINT [PK_Movies]
PRIMARY KEY CLUSTERED ([CinemaId], [CategoryId]),
CONSTRAINT [FK_Movies_Theatre]
FOREIGN KEY ([TheatreId]) REFERENCES [dbo].[Theatre] ([TheatreId])
);
This is my last table:
CREATE TABLE [dbo].[Reviews]
(
[MovieId] INT IDENTITY (1, 1) NOT NULL,
[Name] VARCHAR(50) NOT NULL,
[Genres] VARCHAR(50) NOT NULL,
[Image] VARCHAR(50) NOT NULL,
[ShortDescription] TEXT NOT NULL,
[Rating] VARCHAR(50) NOT NULL,
[Grade] VARCHAR(50) NOT NULL,
[CategoryId] INT NOT NULL,
CONSTRAINT [PK_Reviews]
PRIMARY KEY CLUSTERED ([MovieId] ASC),
CONSTRAINT [FK_Reviews_Movies]
FOREIGN KEY ([CategoryId]) REFERENCES [Movies]([CategoryId]),
);
I have created a relationship between the tables yet I am getting an error while I am updating the last table:
SQL71516 :: The referenced table '[dbo].[Movies]' contains no primary or candidate keys that match the referencing column list in the foreign key. If the referenced column is a computed column, it should be persisted.
Can any about please tell me what is my mistake that I am doing?
Just like the error says, Movies.CategoryId is not a key, so you can't reference it in a Foreign Key. You need a Categories table that has CategoryId as its primary key. Then both Reviews and Movies can have a Foreign Key referencing Categories.

Foreign key constraint is incorrectly formed issue

Please help to understand "Foreign key constraint is incorrectly formed" issue in this code
create TABLE books
(
book_id INT NOT NULL AUTO_INCREMENT,
book_name VARCHAR(255) NOT NULL,
book_description VARCHAR(255) NOT NULL,
book_number_of_pages INT NOT NULL,
book_state VARCHAR(255) NOT NULL,
author_name VARCHAR(255) NOT NULL,
PRIMARY KEY (book_id)
);
create TABLE authors
(
author_id INT NOT NULL AUTO_INCREMENT,
author_name VARCHAR(255) NOT NULL,
author_surname VARCHAR(255) NOT NULL,
book_name VARCHAR(255) NOT NULL,
PRIMARY KEY (author_id),
FOREIGN KEY (book_name) REFERENCES books (book_name)
ON DELETE CASCADE
ON UPDATE CASCADE
);
The 2nd create table statement failed, since the referenced column book_name in table books has no index.
create TABLE books (
book_id INT NOT NULL AUTO_INCREMENT,
book_name VARCHAR(255) NOT NULL,
book_description VARCHAR(255) NOT NULL,
book_number_of_pages INT NOT NULL,
book_state VARCHAR(255) NOT NULL,
author_name VARCHAR(255) NOT NULL,
PRIMARY KEY (book_id),
KEY(book_name)
);
However it's not a good design to store information (book_name) redundant. Instead of book_name you should store the id.

Query to Return a value from a linked table in sql

I'm trying to develop an app for a driving school.
I have a table for instructors:
CREATE TABLE [dbo].[Instructori]
(
[nume] NVARCHAR(50) NOT NULL,
[prenume] NVARCHAR(50) NOT NULL,
[CNP] CHAR(13) NOT NULL,
[Nastere] SMALLDATETIME NOT NULL,
[Angajare] SMALLDATETIME NOT NULL,
[IDmasina] INT NOT NULL,
[IDCategorie] INT NOT NULL,
[Sex] NCHAR(1) NOT NULL,
[NrStudenti] INT NULL DEFAULT 0,
[RataPromovare] DECIMAL(5, 2) NULL DEFAULT 0,
[TotalStudenti] AS [dbo].Total_Stud(CNP),
CONSTRAINT [PK_Instructori] PRIMARY KEY CLUSTERED ([CNP] ASC),
CONSTRAINT [Instr]
FOREIGN KEY ([IDCategorie]) REFERENCES [dbo].[Categorie] ([IDCategorie]),
);
One for Students:
CREATE TABLE [dbo].[Studenti]
(
[nume] NVARCHAR(50) NOT NULL,
[prenume] NVARCHAR(50) NOT NULL,
[Nastere] SMALLDATETIME NOT NULL,
[Legislatie_p] INT NULL,
[Categorii_dobandite] NVARCHAR(50) NULL,
[Categorie] INT NOT NULL,
[Data_examnen] SMALLDATETIME NOT NULL,
[IDLectie] INT NOT NULL,
[CNP] CHAR(13) NOT NULL,
[CNPI] CHAR(13) NOT NULL,
CONSTRAINT [PK_Studenti] PRIMARY KEY CLUSTERED ([CNP] ASC),
CONSTRAINT [FK_Categorie]
FOREIGN KEY ([Categorie]) REFERENCES [dbo].[Categorie] ([IDCategorie]),
CONSTRAINT [Lectie]
FOREIGN KEY ([IDLectie]) REFERENCES [dbo].[Legislatie] ([IDLectie]),
CONSTRAINT [Instructor1]
FOREIGN KEY ([CNPI]) REFERENCES [dbo].[Instructori] ([CNP])
);
and one for cars named "Masina" which is not really relevant.
"Conducere" is the linking table between the 3 others, and it's only used to count how many hours each student drive.
CREATE TABLE [dbo].[Conducere]
(
[CNP_Instructor] CHAR(13) NOT NULL,
[IDMasina] INT NOT NULL,
[CNP_Student] CHAR(13) NOT NULL,
[Numar_ore] INT NOT NULL DEFAULT 0,
PRIMARY KEY CLUSTERED ([CNP_Instructor] ASC),
CONSTRAINT [Student]
FOREIGN KEY ([CNP_Student]) REFERENCES [dbo].[Studenti] ([CNP]),
CONSTRAINT [FK_Masina]
FOREIGN KEY ([IDMasina]) REFERENCES [dbo].[Masina] ([IDMasina]),
CONSTRAINT [Instructor]
FOREIGN KEY ([CNP_Instructor]) REFERENCES [dbo].[Instructori] ([CNP])
);
I already coded a function for the total number of students for each instructor.
CREATE FUNCTION dbo.Total_Stud(#CNP CHAR(13))
RETURNS INT
AS
BEGIN
RETURN
(SELECT Count(CNP)
FROM Studenti
WHERE CNPI = #CNP)
END
I'm trying to find the students that have the same instructor(based on their CNP) and did not drive 30h yet. Students still in school ish.
You can SUM the hours for each student and filter the results by HAVING:
CREATE FUNCTION dbo.Student_Below_RequiredHours(#CNP CHAR(13),#RequiredHours int)
RETURNS TABLE
AS
BEGIN
RETURN
(SELECT CNP_Student
FROM [dbo].[Conducere]
WHERE CNP_Instructor = #CNP
GROUP BY CNP_Student
HAVING SUM([Numar_ore])<#RequiredHours)
END

How to fix Msg 102, Level 15, State 1, Line 8 error

I'm getting error at [InvoiceId]:
Msg 102, Level 15, State 1, Line 8
Incorrect syntax near 'clustured'
My code:
CREATE TABLE tbl_sales_invoice_info
(
id int identity(1,1) NOT NULL,
invoiceid nvarchar(50) NOT NULL,
invoicedate Date NULL,
customer_id int NULL,
grand_total Float(53) NULL,
total_paid Float(53) NULL,
balance Float(53),
PRIMARY KEY clustured ( [invoiceid] ASC)
);
From the documentation, a primary key in SQL Server is automatically created as a clustered index:
When you create a PRIMARY KEY constraint, a unique clustered index on the column or columns is automatically created if a clustered index on the table does not already exist and you do not specify a unique nonclustered index. The primary key column cannot allow NULL values.
So just try removing the CLUSTERED keyword:
create table tbl_sales_Invoice_info (
id int Identity(1,1) not null,
InvoiceId Nvarchar(50) Not Null,
InvoiceDate date null,
Customer_id int null,
Grand_Total float(53) null,
Total_paid float(53) Null,
Balance Float(53),
primary key ([InvoiceId])
);
You can either remove keyword Clustered or just correct the spelling mistake.
CREATE TABLE tbl_sales_invoice_info
(
id int identity(1,1) NOT NULL,
invoiceid nvarchar(50) NOT NULL,
invoicedate Date NULL,
customer_id int NULL,
grand_total Float(53) NULL,
total_paid Float(53) NULL,
balance Float(53),
PRIMARY KEY Clustered ([invoiceid] ASC)
);

Can a non-primary key columns be referenced from a foreign key?

Here is table one I want to refer DesignationId to other table but it is not working
create table Employees
(
EmployeeID int identity(1,1) primary key,
EmployeeNumber int not null,
LocationID int not null,
EmployeeName varchar(20) not null,
DesignationID int not null,
CategoryID int not null,
)
Second table is that .. on third row it is showing error
create table Designation
(
DesignationID int primary key ,
JobTitle varchar(20) not null,
CONSTRAINT fk_Designation_Employees
FOREIGN KEY (DesignationID)
REFERENCES Employees (DesignationID),
)
You are creating this incorrectly. Try it this way instead:
create table Designation
(
DesignationID int primary key ,
JobTitle varchar(20) not null,
)
create table Employees
(
EmployeeID int identity(1,1) primary key,
EmployeeNumber int not null,
LocationID int not null,
EmployeeName varchar(20) not null,
DesignationID int not null,
CategoryID int not null,
CONSTRAINT fk_Employees_Designation
FOREIGN KEY (DesignationID)
REFERENCES Designation (DesignationID)
)
Many employees linked to a designation. One-To-Many relationship.

Resources