How to add primary key and foreign key to a BigQuery? - database

I am trying to learn bigquery but I noticed I cannot add Primary key or Foreign Keys at all.
Here is an example:
CREATE TABLE db.VENDOR
(
V_CODE INT64,
V_NAME String NOT NULL,
V_CONTACT String NOT NULL,
V_AREACODE String NOT NULL,
V_PHONE String NOT NULL,
V_STATE String NOT NULL,
V_ORDER String NOT NULL,
PRIMARY KEY(V_Code)
);
CREATE TABLE db.PRODUCT
(
P_CODE string Not Null,
P_DESCRIPT string NOT NULL,
P_INDATE DATETIME NOT NULL,
P_QOH int64 NOT NULL,
P_MIN int64 NOT NULL,
P_PRICE NUMERIC NOT NULL,
P_DISCOUNT NUMERIC NOT NULL,
V_CODE int64,
CONSTRAINT PRODUCT_V_CODE_FK
FOREIGN KEY (V_CODE) REFERENCES VENDOR (V_CODE)
);
When I use primary key, I get an error:
Primary Key is not supported
and for the foreign key I get:
Table name "VENDOR" missing dataset while no default dataset is set in the request
Is there anyway to use PK or FK in BigQuery? if yes, How?

Primary keys and foreign keys constraints are not supported. They are not needed for OLAP databases. If you need them, chances are that you need OLTP instead like Cloud SQL or Cloud Spanner

Related

How to add Foreign key constraint on array in PostgreSQL?

How to add Foreign key constraint on array in PostgreSQL?
look_up table for roles
CREATE TABLE party_role_cd
(
party_role_cd bigint NOT NULL,
code character varying(80) NOT NULL,
CONSTRAINT party_role_cd PRIMARY KEY (party_role_cd)
);
Party can have zero or many roles [ 0-N relationship ]
CREATE TABLE party
(
party_id biging NOT NULL,
party_role_cd bigint[] NOT NULL,
CONSTRAINT party_id PRIMARY KEY (party_id)
);
How to add the foreign key constraint for party_role_cd array in party table?
That's not implemented in PostgreSQL. Currently, FK constraints only operate on equality between whole column values. No array-to-element references. There is an open TODO item that has been added in 2010. See:
https://wiki.postgresql.org/wiki/Todo#Referential_Integrity
https://www.postgresql.org/message-id/1288033876.6278.6.camel#vanquo.pezone.net
There were even attempts to implement it, but never to completion.

SQL Server & Visual Studio - Insert Data in tables doesn't work

I want to insert my tables from my SQL Server Management Studio code with data via Visual Studio.
But it doesn't work. The ID of my table doesn't get a value automatically (but I have command it with identity(1,1)) and an error appears that it stands in conflict with a foreign-key-constraint.
create table [ProduktZahlungFENutzer]
(
ID int PRIMARY KEY IDENTITY(1,1) NOT NULL,
Endpreis decimal(3, 2)
);
CREATE TABLE [FHAngehörige]
(
FHAngehörigeID INT NOT NULL IDENTITY(1,1)
PRIMARY KEY Constraint fhA_feN
REFERENCES FENutzer(FENutzerID),
Name VARCHAR(50) NOT NULL,
Fachbereich INT NOT NULL,
Email VARCHAR(50) NOT NULL UNIQUE
)
CREATE TABLE [FENutzer]
(
FENutzerID INT IDENTITY(1,1) NOT NULL
PRIMARY KEY constraint t_nutzer
references ProduktZahlungFENutzer(ID),
Aktiv INT NOT NULL,
LetzterLogin datetime NOT NULL DEFAULT GETDATE(),
BENutzerID int NOT NULL
constraint FENutzer_BENutzer
foreign key references BENutzer(BENutzerID),
auth_id int not null
constraint FENutzer_auth
foreign key references auth2(id)
)
Please help
You have established a reference
FHAngehörigeID INT NOT NULL IDENTITY(1,1) PRIMARY KEY Constraint fhA_feN
REFERENCES FENutzer(FENutzerID),
This means that you need a record in your table FENutzer.FENutzerID that match FHAngehörigeID, in this case you cannot use IDENTITY
Your construction makes no sense - this way, you're creating two tables that depend on each other, at the primary key level - so you would have to have an existing entry in FENutzer in order to insert a new row into FHAngehörige, and this at the same time is only possible if you already have an existing row in FHAngehörige to insert the row in FEnutzer.....
You need to clean this up:
both tables need a primary key and the int identity(1,1) is a very good choice
one of the tables must reference the other, but with a normal foreign key attribute - not on the primary key.....
Since you've not given any indication as to which of the tables is the "main" table and which the "child/auxiliary" table, I'm just picking one over the other - so your table structure should be something like:
CREATE TABLE dbo.FHAngehoerige
(
FHAngehoerigeID INT NOT NULL IDENTITY(1,1)
CONSTRAINT PK_FHAngehoerige PRIMARY KEY CLUSTERED,
Name VARCHAR(50) NOT NULL,
Fachbereich INT NOT NULL,
Email VARCHAR(50) NOT NULL UNIQUE
)
CREATE TABLE dbo.FENutzer
(
-- define the PK for the table
FENutzerID INT IDENTITY(1,1) NOT NULL
CONSTRAINT PK_FENutzer PRIMARY KEY CLUSTERED,
-- define the **foreign key** to the main table
FHAngehoerigeID INT NOT NULL
CONSTRAINT FK_FENutzer_FHAngehoerige
FOREIGN KEY REFERENCES dbo.FHAngehoerige(FHAngehoerigeID),
Aktiv INT NOT NULL,
LetzterLogin datetime NOT NULL DEFAULT GETDATE(),
BENutzerID int NOT NULL
constraint FENutzer_BENutzer
foreign key references BENutzer(BENutzerID),
auth_id int not null
constraint FENutzer_auth
foreign key references auth2(id)
)
Also: I would strongly recommend (from my own personal, bad experience) to AVOID any special characters like umlauts or accents or anything like that in table and column names.....

Can I have a foreign key in my table be null?

I have this DDL:
CREATE TABLE [dbo].[Test] (
[TestId] VARCHAR (20) NOT NULL,
[Title] NVARCHAR (100) NOT NULL,
[TopicId] INT NOT NULL,
CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED ([TestId] ASC),
CONSTRAINT [FK_TestTopic] FOREIGN KEY ([TopicId]) REFERENCES [dbo].[Topic] ([TopicId])
);
I would like the allowed values of the TopicId to be null or one that matches the TopicId in the Topic table.
Can someone tell me how I can describe the foreign key for this?
Yes, foreign key constraint columns may allow NULLs. Simply change the foreign key column to allow NULL values. Referential integrity of NULL values are not checked. This is a common practice for optional (zero-or-more) relationships.
Well.. You could do that very easily just by making the field to allow null.. But, keep this in your mind, 'you are breaking data modelling'.

Dropping PRIMARY KEY constraint

I have created a table which has two columns combined as a primary key.
CREATE TABLE [dbo].[Workflow_Name]
(
[Workflow_ID] [int] NOT NULL,
[Unique_Workflow_ID] [int] NOT NULL,
[Workflow_Name] [varchar](255) NULL,
[Row_ID] [int] NULL,
[ReleaseVersion] [varchar](255) NULL,
[Release] [varchar](255) NULL,
CONSTRAINT [PK_WorkFlowName] PRIMARY KEY CLUSTERED
([Workflow_ID] ASC, [Unique_Workflow_ID] ASC )
)
As seen , [Workflow_ID] ASC, [Unique_Workflow_ID] ASC combined together are forming the Primary key.
Now i want to remove [Unique_Workflow_ID] from the Primary key constraint and maintain only [Workflow_ID] as Primary Key.
How to do it?
You can do it by executing the following statements in SSMS Query Window after selecting the database that the table is in.
ALTER TABLE [dbo].[Workflow_Name]
DROP CONSTRAINT [PK_WorkFlowName]
ALTER TABLE [dbo].[Workflow_Name]
ADD CONSTRAINT [PK_WorkFlowName] PRIMARY KEY ([Workflow_ID] ASC)
Please note, in order for it to work, if you have existing rows in the [Workflow_Name], then the data in this new single column Primary Key i.e. in [Workflow_ID] must be unique per row. Otherwise the ALTER statement will (rightly) throw an error that data is not unique in that column.
You can remove your primary key constraint and then create a new one only with the Workflow_ID. But make sure it has only unique values.

How to find all references of a particular primary key in a sqlite table?

I have set PRAGMA foreign_keys=ON;
I'm trying to delete some records in a sqlite3 table and it displays Error: constraint failed
sqlite> delete from auth_user where id = 110;
Error: constraint failed
It works if the PRAGMA foreign_keys was OFF. The database has so many tables and the error is so vague. I think other database systems will list the tables that reference the primary key if we attempt deletion.
What is the efficient way I can find all the tables that reference that particular primary key id=110?
Schema:
CREATE TABLE "auth_user" (
"id" integer NOT NULL PRIMARY KEY,
"username" varchar(30) NOT NULL UNIQUE,
"first_name" varchar(30) NOT NULL,
"last_name" varchar(30) NOT NULL,
"email" varchar(75) NOT NULL,
"password" varchar(128) NOT NULL,
"is_staff" bool NOT NULL,
"is_active" bool NOT NULL,
"is_superuser" bool NOT NULL,
"last_login" datetime NOT NULL,
"date_joined" datetime NOT NULL
);
I don't think there is a clear-cut way to list the foreign key constraints with SQLite. You can however list all tables with such constraints as shown below. You can then parse the returned SQL to find the constraints.
Say you have a two parent-child tables:
sqlite> PRAGMA foreign_keys=ON;
sqlite> create table Parent (
...> Id INTEGER PRIMARY KEY AUTOINCREMENT,
...> foo TEXT);
sqlite> create table Child (
...> Id INTEGER PRIMARY KEY AUTOINCREMENT,
...> ParentId INTEGER NOT NULL,
...> bar TEXT,
...> FOREIGN KEY (ParentId) REFERENCES Parent(Id));
You can list the CREATE TABLE statements that have foreign keys:
sqlite> SELECT sql FROM sqlite_master WHERE sql LIKE '%REFERENCES%';
CREATE TABLE Child (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
ParentId INTEGER NOT NULL,
bar TEXT,
FOREIGN KEY (ParentId) REFERENCES Parent(Id))
You may already know this, but deleting rows that are constrained some foreign key while foreign keys are turned off kinda breaks referential integrity of the database, so I can only advise you against doing that.

Resources