To show correctly PRIMARY KEY in Postgres - database

Which one of the following ways would you use in declaring Primary Keys by Postgres?
#1
CREATE TABLE user(
user_id PRIMARY KEY,
...
)
#2
CREATE TABLE user(
user_id NOT NULL,
...
CONSTRAINT user_pk PRIMARY KEY(user_id);
)

I would use method #1.
The indication of which column is the primary key is kept closer to the actual column definition
You don't have to think up a name for the constraint; a name will be automatically generated
One reason to use method #2 is if your primary key were to span more than one column. In that case, method #1 won't work because it only supports a single column primary key.

Related

id auto_inc column, but no primary key?

I apologize for the bad title, but I wasn't sure how else to phrase it.
Let's imagine for a moment that I wanted to create a us_states table as follow:
create table us_states
(
id serial,
name varchar(256) not null constraint us_states_pk primary key,
code varchar(256) not null
);
What tangible benefits, if any, are there to having an auto incremental id column in a db_table if I don't plan on leveraging it as the primary key for said db_table?
There is zero value in adding an auto-incremented numerical column if it isn't a primary key or unique constraint.
If you have a unique column like name in your case, there are only two considerations not to use it as primary key:
storing a number instead of the name in tables that reference this one will save space
it is painful and should be avoided to modify primary key columns, so if the names change as part of the normal operation, it makes sense to use a different column as primary key

Creating in MS-SQL a primary key results in a dynamic index name format eg PK__dummy__3213E83F2E1BDC42

Hope for help because of the following problem. Assume we have a table
CREATE TABLE [dbo].[dummy](
[id] [char](36) NOT NULL,
[name] [varchar](50) NOT NULL
) ON [PRIMARY]
If I create a primary key like this (version 1)
ALTER TABLE dummy ADD CONSTRAINT PK_dummy PRIMARY KEY (ID);
I get a unique name. In this case PK_dummy.
But if I create a primary key like this (version 2)
ALTER TABLE dummy ADD PRIMARY KEY Clustered (ID);
The name changes with every recreation of this primary key.
The format is always PK__dummy__"a dynamic number"
What is the meaning of this number?
And how can I identify primary keys created with version 2 in a hugh database?
Thanks for hints.
What is the meaning of this number?
This depends on product version - it is either based on a unique id or generated randomly.
how can I identify primary keys created with version 2 in a huge database?
SELECT *
FROM sys.key_constraints
WHERE is_system_named = 1
If you don't define the name of a constraint, index, key, etc, SQL Server will give it a name. To ensure uniqueness across the database, it therefore will add "random" characters at the end.
If having a consistent name is important then define the name in your statement, as you did in the first statement.

Composite primary key in sql server

I am trying to create a composite primary key in Sql server.
The syntax I have tried is:
create table Installment_details
(
constraint P_key primary key(Account_No,month),
Account_No int not null,
foreign key (Account_No) references Account_details(Account_no) on delete cascade,
Month char(15) not null,
D#te date,
Receipt_no varchar(15),
Amount_received int,
Amount_left int,
Amount_receiver char(50),
)
As far as I know it should create column with column name P_key for primary key but whenever I make a entry in table it doesn't show this column.
You are confused about the terms you're using. It's not the same a Primary Key and a Column. For example, you're creating a Primary Key based on two existing columns, and the name P_Key it's the name of the Primary Key, which is the way SQL SERVER (in this case) can identify a row in the Table (it cannot be two rows with the same values on those two columns).
I hope this clarifies a little bit the issue.
I think you are getting it wrong P_key in your code is constraint's name not a column name.
Also composite key is not a column, it is used when you don't have a column with unique values. So you take combination of two or more column as primary key so that we can uniquely identify a row.

H2 Composite Primary Key (fields get unique but not primary key)

I have a problem with a composite primary key in my h2 database table.
This is the create statement of the table:
CREATE TABLE IF NOT EXISTS TTColumn (
Name VARCHAR(15) NOT NULL,
TTName CHAR(8) NOT NULL,
Type VARCHAR(15) NOT NULL,
Length INTEGER NOT NULL,
Position INTEGER NOT NULL,
IsDBLogType BIT NOT NULL,
PRIMARY KEY(TTName,Name),
FOREIGN KEY(TTName) REFERENCES TrackingTable(Name))
(TTName is the name of the table for the columns, so its the same for each column of the table, but each column is unique in a table so its an composite key)
If i fill this table with data, i get the following exception
rg.h2.jdbc.JdbcSQLException: Eindeutiger Index oder Primärschlüssel verletzt: "CONSTRAINT_INDEX_E8 ON PUBLIC.TTCOLUMN(TTNAME) VALUES ('KIBLCOVI', 1)"
Unique index or primary key violation: "CONSTRAINT_INDEX_E8 ON PUBLIC.TTCOLUMN(TTNAME) VALUES ('KIBLCOVI', 1)"; SQL statement:
INSERT INTO TTColumn (Name,TTName,Type,Length,Position,IsDBLogType) VALUES (?,?,?,?,?,?) [23505-189]
As you can see the column "Name" is not part of the primary key anymore, like i defined in the create statement and i dont know why.
I have this problem in other tables too and adding the primary with alter table, after creating it, resolves in the same problem. When "Name" is the first column in the primary key, it gets the only column in the primary key, so the last entrys get removed.
I know, one solution would be to use id's, but I want to use a composite primary key, because i hate to add id's when its not necessary.
Is there something wrong with my create statement or do you have any other hints?
P.s. i tried the Version 1.4.189 and Version 1.3.176 as .jar (no server)
Edit:
Hmm, following the output from show columns:
NAME|VARCHAR(15)|NO|UNI|NULL|
TTNAME|CHAR(8)|NO|UNI|NULL|
TYPE|VARCHAR(15)|NO||NULL|
LENGTH|INTEGER(10)|NO||NULL|
POSITION|INTEGER(10)|NO||NULL|
ISDBLOGTYPE|BOOLEAN(1)|NO||NULL|
Somehow the column Name and TTName are UNI (Unique) but not a primary key.
if i change the order in the primary key declaration, the column TTName becomes a primary key field, but Name stays unique.

Postgres: two foreign keys to same primary key field

create table date_dimension (
id serial primary key,
date_id date,
..... others
);
create table temp (
id serial primary key,
from_date integer,
to_date integer,
value integer,
foreign key (from_date, to_date) references date_dimension(id, id)
);
How can I refer both from_date and to_date to id field in date_dimension?
The current code fails to do that saying
ERROR: there is no unique constraint matching given keys for referenced table "date_dimension"
Thank you
each FOREIGN KEY constraint added to a table will always relate one row in the referencing table to one row* in the referant. If you want to have each row in the referencing to refer to two distinct rows in the referant, you need two, separate foreign key constraints.
you want:
foreign key (from_date) references date_dimension(id)
foreign key (to_date) references date_dimension(id)
You almost always want to have exactly the rows in the foreign key to be the same as the primary key in the referant.
* Actually, there may be more than one row in the referant if the foreign key is smaller than a candidate key of the referant. this is seldom useful, though, and almost certainly unrelated to the problem you're describing

Resources