product set a column unique without being the primary key - sql

I wanna create the following table:
create table product (id bigint not null, product_type varchar(50), product_name varchar(100), available_from TIMESTAMP, available_to TIMESTAMP, primary key (id));
My table's key is the 'id'.
when inserting in the table, I wanna that the product_type be unique.
How to do that without setting the product_type the key for my table

ALTER TABLE <your table name>
ADD CONSTRAINT unique_product_type UNIQUE(product_type);
I can't see a table name in your create table SQL.

The function NEWID() generates a new unique id. Maybe you can make a trigger which insets this in every insert or make the default value to be NEWID(). Well the second option seems a bit better :P
CREATE TABLE Test123
(
ID NVARCHAR(200) DEFAULT (NEWID()),
name NVARCHAR(100)
)
INSERT INTO Test123 (name) VALUES ('test')
SELECT * FROM Test123
DROP TABLE Test123

Add a unique constraint, something like this (not tested):
http://www.w3schools.com/sql/sql_unique.asp
CREATE TABLE Product
(
id bigint NOT NULL,
product_type varchar(50),
product_name varchar(100),
available_from TIMESTAMP,
available_to TIMESTAMP,
primary key (id),
UNIQUE (product_type)
)

Related

Add a reference to a postgrest table

I want to alter the artist_label to be label_id BIGINT NOT NULL REFERENCES label(id) like how it is in the album_label table.
CREATE TABLE label (
id BIGSERIAL PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
parent_label_id BIGINT REFERENCES label(id)
);
CREATE TABLE album_label (
id BIGSERIAL PRIMARY KEY,
album_id TEXT NOT NULL UNIQUE,
label_id BIGINT NOT NULL REFERENCES label(id)
);
CREATE INDEX album_label_idx ON album_label(label_id);
CREATE INDEX album_uid_idx ON album_label(id)
CREATE TABLE artist_label (
artist_id VARCHAR(60) NOT NULL,
label_id BIGINT NOT NULL,
PRIMARY KEY (artist_id, label_id)
);
CREATE INDEX artist_label_idx ON artist_label(label_id);
If you haven't created the tables yet, you could modify the creation script and add that clause.
If you're asking how to modify the table after its creation, you can use an alter table statement:
ALTER TABLE artist_label
ADD CONSTRAINT artist_label_fk FOREIGN KEY (label_id) REFERENCES label(id)
SQLFiddle Example

Creating a table syntax errors in sql

I'm trying to create a SQL table for a school assignment. I'm required to name the constraints, and I am receiving a syntax error when creating my table
SELECT * FROM demo;
CREATE TABLE PRODUCT
(
ProductID NUMBER,
ProductDescription VARCHAR(200),
CONSTRAINT ProductPK PRIMARY KEY (ProductID)
);
CREATE TABLE ITEM
(
ItemNum NUMBER
CONSTRAINT Inull NOT NULL
CONSTRAINT ItemPK PRIMARY KEY(ItemNum),
ItemDesc VARCHAR(200)
);
The syntax error is as follows:
near "(": syntax error
It is likely something simple, but this is my first time writing SQL.
You have a few errors .. One, you are missing commas after ItemNum NUMBER and CONSTRAINT Inull NOT NULL
Additionally there are other ways to define NOT NULL, you technically don't need to use CONSTRAINT in traditional SQL.
And depending on the flavor of SQL you are using .. I like to use INT or INTEGER instead of NUMBER -- It's basically the same as NUMBER() with a few exceptions, but if you are using whole numbers I would use INT or INTEGER
SELECT * FROM demo;
create TABLE PRODUCT
(
ProductID INT,
ProductDescription VARCHAR(200),
CONSTRAINT ProductPK PRIMARY KEY (ProductID)
);
CREATE TABLE ITEM
(
ItemNum INT NOT NULL,
CONSTRAINT ItemPK PRIMARY KEY(ItemNum),
ItemDesc VARCHAR(200)
);
UPDATE
And as #Schwern suggests .. Your query can be further simplified, as there really is no need for CONSTRAINTS here .. I would also recommend Auto Incrementing your IDs .. Thus removing the need for NOT NULL on the ID. This also negates the need to supply an ID with the INSERT query ..
SELECT * FROM demo;
create TABLE PRODUCT
(
ProductID INT PRIMARY KEY AUTO_INCREMENT,
ProductDescription VARCHAR(200)
);
CREATE TABLE ITEM
(
ItemNum INT PRIMARY KEY AUTO_INCREMENT,
ItemDesc VARCHAR(200)
);

POSTGRESQL. Insert or update on table violates foreign key constraint

I am new in Posgresql. I have 5 tables and I am trying to INSERT properties to tables. When I tried to Insert 2nd time, I have this error in 'pgadmin'.
ERROR: insert or update on table "question" violates foreign key constraint "question_id_difficulty_fkey" DETAIL: Key (id_difficulty)=(9) is not present in table "difficulty". SQL state: 23503.
my schema is here
id SERIAL PRIMARY KEY,
name varchar
);
CREATE TABLE question (
id SERIAL PRIMARY KEY,
text varchar,
correct_answer varchar,
incorrect_answer1 varchar,
incorrect_answer2 varchar,
incorrect_answer3 varchar,
id_difficulty SERIAL REFERENCES difficulty(id),
id_category SERIAL REFERENCES category (id),
id_creator SERIAL REFERENCES game (id)
);
CREATE TABLE difficulty (
id SERIAL PRIMARY KEY,
name varchar
);
CREATE TABLE category (
id SERIAL PRIAMRY KEY,
name varchar
);
CREATE TABLE user (
id SERIAL PRIMARY KEY,
name varchar
)
You would need a corresponding entry in the difficulty table with an id of 9, so that a referencing id_difficulty column in the question table.
For example, if your difficulty table contained:
id | name
----+----------------
1 | easy
2 | reasonable
3 | difficult
4 | very difficult
5 | impossible
You could only set id_difficulty for rows in the question table to one of those id values. If you set 6, or 12 or anything other than 1 to 5, it would fail because the values are constrained by the values in the foreign key.
The id_difficulty, id_category and id_creator columns shouldn't be using serial, so these should have their defaults dropped:
ALTER TABLE question ALTER COLUMN id_difficulty DROP DEFAULT;
ALTER TABLE question ALTER COLUMN id_category DROP DEFAULT;
ALTER TABLE question ALTER COLUMN id_creator DROP DEFAULT;
Postgres now recommends using generated always as instead of serial. If you do this, then the types will align much more simply:
CREATE TABLE question (
id int generated always as identity PRIMARY KEY,
text varchar,
correct_answer varchar,
incorrect_answer1 varchar,
incorrect_answer2 varchar,
incorrect_answer3 varchar,
id_difficulty int REFERENCES difficulty(id),
id_category int REFERENCES category (id),
id_creator int REFERENCES game (id)
);
CREATE TABLE difficulty (
id int generated always as identity PRIMARY KEY,
name varchar
);
This makes what is happening much clearer. The data type for a foreign key reference needs to match the data type of the primary key. Postgres knows that serial is really int. But using generated always, it is obvious that they are the same.
In addition, generated always as is more consistent with standard SQL.

Issues with creating SQL Server table relationships

I have 2 tables.
First table:
create table abc
(
AId INT IDENTITY(1,1),
Name NVARCHAR(50),
EMP NVARCHAR(50)
CONSTRAINT PK_abc PRIMARY KEY (AId, Name)
)
And second table as:
create table def
(
AId INT,
Comment NVARCHAR(50)
constraint FK_aid FOREIGN KEY (AId) references abc (AId)
)
This throws an error:
There are no primary or candidate keys in the referenced table 'abc' that match the referencing column list in the foreign key 'FK_aid'.
So I updated as:
create table def
(
AId INT,
Name NVARCHAR(50),
Comment NVARCHAR(50)
constraint FK_AID FOREIGN KEY (AId, Name) references abc (AId, Name)
)
But this throws another error:
More than one key specified in column level FOREIGN KEY constraint, table 'def'.
Not sure what am I missing here.
---Updated----
Sorry for giving vague example. I was having difficulty in explaining my problem here. I have attached the screenshot of my problem. This show the eventual output that I am expecting by making join to 3 tables. Where the data of 3 tables is populated from an input form.
I have tried to provide in a clear way. Let me know if you need more inputs.
Attchment at:
https://imgur.com/a/jfFS6
Thanks
You have an identity column. Use it as the primary key:
create table abc (
AId INT IDENTITY(1,1) PRIMARY KEY,
Name NVARCHAR(50),
EMP NVARCHAR(50)
);
create table def (
AId INT,
Comment NVARCHAR(50)
constraint FK_aid FOREIGN KEY (AId) references abc (AId)
);
Name should not be in the definition of the primary key.
Looks like you have to do it as a separate statement:
ALTER TABLE [dbo].[def] WITH CHECK ADD CONSTRAINT [FK_def_abc] FOREIGN KEY([AId], [Name])
REFERENCES [dbo].[abc] ([AId], [Name])
GO
ALTER TABLE [dbo].[def] CHECK CONSTRAINT [FK_def_abc]
GO
But you still should NOT do that. Identity is unique and it alone should be used as primary key.

SQL Server : create error how to write database name with the table name

CREATE DATABASE agom COLLATE Arabic_CI_AS
CREATE TABLE Branches
(
ID INT PRIMARY KEY NOT NULL IDENTITY,
NAME VARCHAR(255) NOT NULL
)
CREATE TABLE agom.Brands
(
ID INT PRIMARY KEY NOT NULL IDENTITY,
NAME VARCHAR(255) NOT NULL
)
CREATE TABLE agom.Work_Order
(
NUMBER INT NOT NULL,
BRANCHID INT NOT NULL,
BRANDID INT NOT NULL,
WDATE DATE NOT NULL,
REPAIRSTATUS VARCHAR(255),
REPAIRCOST VARCHAR(255),
REMARK VARCHAR(500),
PRIMARY KEY (NUMBER,BRANCHID,BRANDID)
)
CREATE TABLE agom.Profiles
(
ID INT PRIMARY KEY NOT NULL IDENTITY,
USERNAME VARCHAR(25) NOT NULL,
PASS VARCHAR(25) NOT NULL
)
ALTER TABLE agom.Work_Order
ADD CONSTRAINT branchfk
FOREIGN KEY (BRANCHID) REFERENCES Branches(ID)
ALTER TABLE agom.Work_Order
ADD CONSTRAINT brandfk
FOREIGN KEY (BRANDID) REFERENCES Brands(ID)
I get an error that cannot create table
I try to write database name with the table name db.tablename but it's not working
I need to create the database then create the tables and its constraints but I don't know where is the error.I am a sql noob
It's never Database.Table.
It's either Table, or Schema.Table, or Database.Schema.Table, or Server.Database.Schema.Table.
You probably just want to insert USE agom right after create database, and then only refer to tables by name.
Alternatively, you can refer to your tables as agom.dbo.Work_Order, dbo being the default database schema.
See Using Identifiers As Object Names for general reference.