In relational database design, a primary key can uniquely identify each row in a table. A primary key comprises a single column or a set of columns.

learn more… | top users | synonyms

2
votes
4answers
152 views

Add autoincrement to existing PK

I created a table in a DB that already exists in another DB. It was initially populated with the old DB data. The table's PK had to receive the values that already exist on those records, so it ...
0
votes
0answers
32 views

Deleting a Row in Table- Best way to also delete all rows it affects? [on hold]

I have created a very simple SQL script (see below) that deletes rows in my table if a column name is a certain value. DELETE FROM table_name WHERE some_column=some_value; However, this table ...
2
votes
1answer
22 views

Surrogate vs composite key in hierarchical data structure

I'm currently evaluating a schema for a hierarchical data structure. My main problem is how I should design the schema to prevent inconsistent data (reference of foreign key in another hierarchy). The ...
0
votes
1answer
31 views

Phpmyadmin / mysql error defining primary keys as foreign keys of two separate tables

I am designing a mysql database using phpmyadmin 3.3.10.4. Everything in the database is InnoDB. I have two tables, person with primary key person.id_numberand e-mail_address with pk ...
1
vote
1answer
22 views

Find insert/update/delete queries that reference a partitioned view

I have just given a SQL Server with performance problems. The database is very large and use distributed views with a very large primary clustered key. (24 bytes). I would like to change this primary ...
2
votes
2answers
301 views

Can different tables have columns with the same name if those columns are not primary keys? [closed]

I know it's a simple enough question but my textbook is very confusing about it.
1
vote
1answer
30 views

Trying to understand how an auto increment primary key is better than no primary key and some other primary key questions

I am trying to understand primary keys better, how to use them effectively in table design and in queries. First, are the primary keys themselves used in WHERE clauses? For example, if I have a table ...
2
votes
1answer
54 views

Primary key addition for a table which contatins no data is too slow

We are using mysql. We are running 10 alter queries which contains 3 Primary key deletion and 4 column addition and 3 primary key addition. And this alter query is on three tables which don't contain ...
1
vote
1answer
27 views

Cities and States (a few data): single table or splitted?

I am designing a DB schema and I need to store cities and states. I expect only a few rows, let's say, 20 cities. Example: id | city | state 1 | Rio de Janeiro | RJ 2 | Niteroi ...
1
vote
0answers
36 views

Many-to-many relationship, what's the best choice for primary key? [duplicate]

In advance, I'd like to apologize if it's a duplicate question... I can't find a direct answer, probably because I'm not a native english speaker. Straight to the subject: I have 2 tables: company, ...
7
votes
1answer
103 views

Unique identifier field with a conditional

I have a database that's not in production, so the main table being CustodyDetails, this table has a ID int IDENTITY(1,1) PRIMARY KEY column and I'm looking for a way of adding another unique ...
3
votes
1answer
58 views

Update multiple Ids when SET = (Sql Query)

I want to update multiple records however the value being set is based on the primary key of the given record. This is not an issue with a single record as I can use: UPDATE T1 SET A1 = (SELECT ...
1
vote
0answers
21 views

General Definition of/for Primary Key for Table with Repeated Entries (i.e., not in 1NF)

my first post, my apologies if this does not belong here, please refer me to the/a right site. So I am looking for a general form for a primary key given a table containing non-atomic repeated ...
1
vote
1answer
33 views

Chance of duplicate on single select max into statement

Against my will, I am told to use the following method to generate a new pk value. INSERT INTO SERVER_MONITERING(SM_SEQ, CPU_USE, MEMORY_BASE, MEMORY_USE, DISK_BASE, DISK_USE, ...
0
votes
2answers
58 views

Check if a primary key value is referred in any other table

I am using oracle database. Is there a way I can check if the primary key value in one table is being used as a foreign key value (have any associations) in any other table? Example: if I have the ...
2
votes
2answers
98 views

Does a composite primary key break the first normal form?

Does having a composite/compound key (using multiple columns/attributes) to form the primary key break first normal form? (I am assuming that this is handled by the DBMS, and so the attribute ...
1
vote
0answers
105 views

Sudden PRIMARY KEY violation on IDENTITY column

I have a log table with an ID identity column. Everything works fine for years; then, yesterday, I see the following error in the logs: The statement has been terminated. Violation of PRIMARY ...
0
votes
2answers
66 views

Should I create a child table without primary key?

I'm designing a database with a child table that may contains billions of records in the future. So I try to use less fields (and smaller ones) as possible. So I'm considering not using a primary ...
3
votes
3answers
301 views

Programmatically finding minimum set of fields necessary to create a unique composite key

I am importing flat files from different sources into tables in SQL Server. I am creating a composite primary key using a combination of fields from the extracts that will give me a unique key for ...
4
votes
2answers
104 views

What could justify a non explicit - non atomic primary key

I'm working as a "guest" dev at a random company. I'm working with a table that does not have a primary key by definition(Oracle). The column named "SOMETHING_ID" is varchar2(10) and constructed at ...
0
votes
0answers
35 views

When should I create a primary key on multiple columns? [duplicate]

I'm interested to know in which case I should create a primary key on multiple columns and why ? To be more specific about my question, I want to know what are the advantages and disadvantages of ...
1
vote
0answers
23 views

Can I use a Composite Key as criteria for selection in a query?

I have created a meeting minutes table and would like to use two fields as a unique ID for each meeting (field one is a meeting code (lookup from another table) for each type of meeting and field two ...
0
votes
0answers
19 views

Structure of an SQLite database for storing analytics with counters and hits

Before making this post, I referred to this and this which contain extremely complex solutions and hence, this question: I am building a spam filtering app that takes posts from a feed and deletes ...
1
vote
1answer
31 views

What is the best approach to find all fields that has (Foreign Key and Primary Key) constraints in a Mysql database?

I have a mysql database that has 250 tables. I would like to run a query to retrieve: tableName ; FieldName ; ConstaintName (Primary or foreign key). I could use ...
0
votes
1answer
51 views

multi valued attribute as primary key

I am designing a database system for my current project and got this situation. I am having a functional dependency as D -> E Here D is multi valued attribute. D can be(i,j,k) or (i,j) or any ...
0
votes
1answer
85 views

Derived classes with same base class and class specific ids

I have three classes in my project. One base class and two derived classes. public class Animal { public long Id { get; set; } public DateTime Created { get; set; } public string Name { ...
1
vote
3answers
74 views

Is it a good idea to use a personal number as a primary key for a customer table?

We are designing a new system to handle customers at our firm and me and another developer are split on wether we should use the customers personal numbers as a primary key. My preference would be to ...
0
votes
0answers
58 views

Finding the closure and super keys of a schema?

I am working on a problem in which I have to look at the relation R with schema R(w, x, y, z), and the three functional dependencies: w -> z ; x,y -> w ;z -> y For every non-empty subset X of ...
0
votes
1answer
25 views

Using default values in primary key

Good day. I have a table like this: CREATE TABLE mytable ( myname varchar(30), mydate date DEFAULT CURRENT_DATE, PRIMARY KEY(myname, mydate) ); EDIT: Forgot to mention I am using a ...
2
votes
2answers
59 views

Does my table need a primary key and clustered index change?

I have a table holding 1.7 Milion rows. Definition: CREATE TABLE T( [ID] [uniqueidentifier] NOT NULL, [AID] [uniqueidentifier] NOT NULL, [BID] [uniqueidentifier] NOT NULL, [iType] [int] NOT NULL, ...
0
votes
0answers
35 views

Should Primary Keys be arbitrary?

I have a lookup table PerformanceID (incremental integer - Primary Key - Identity Column) PerformanceRating (Integers from 1 to 5) Description (100 char description of what the rating means) ...
0
votes
0answers
33 views

Is there a way of implementing a composite primary key with varying fields in MongoDB?

Here is the information about my development environment: -Mongodb 3.0.0 -Microsoft Visual Studio Professional 2013 -.NET Framework 4.5 We are trying to design some MongoDB collections at work ...
8
votes
2answers
98 views

SQL Server equivalent of Oracle USING INDEX clause

Is there a SQL Server 2008 equivalent of the USING INDEX clause in Oracle? Specifically for the construct: CREATE TABLE c(c1 INT, c2 INT); CREATE INDEX ci ON c (c1, c2); ALTER TABLE c ADD CONSTRAINT ...
0
votes
1answer
42 views

InnoDB: SELECT COUNT(*) using primary key 25 times slower on Windows

MySql 5.6, InnoDB. I have a table with 8 million records. It has an integer primary key, and a unique index u1 CHAR(40). It also has a few small fields and a VARCHAR(1000). SELECT COUNT(*) FROM ...
2
votes
1answer
216 views

DynamoDB - Multiple range keys

Is it possible to have multiple fields as range key? Say I have a table where each row is uniquely identified by <A,B,C> ------------------------------- A | B | C | D | E | ...
0
votes
2answers
28 views

Primary Keys for Adress tables

I'm working on a database that stores address information. Each address has a Street, Number, Bus (as in the apartment they live in) and CityCode parameter. The CityCode parameter is a FK to the City ...
1
vote
0answers
75 views

How to use an RSA public key as the primary key to a table in postgres

I have a data set that needs to be looked up by RSA public keys. These keys are 392 text characters long. What is the best way to make that a primary and/or unique key? Should it be a BYTEA ...
3
votes
0answers
66 views

Clustered index on a join table

Currently I have a table defined as follows, but I'm not sure what to cluster the table on: CREATE TABLE [Security].[UserGroups] ( [UserId] INT NOT NULL, [GroupId] INT NOT NULL, [IsAdmin] ...
2
votes
1answer
96 views

Oracle: How to create a “reverse” index primary key?

I can create a primary key for a table and then reverse it with the command alter index T_PK rebuild reverse online; How can I create it reversed?
-1
votes
1answer
34 views

Can look up tables be used to store primary keys?

I am in the designing phase of the database which deals with online shopping. That is primarily stores, categories, products etc. The business consultant for the project has suggested to use Look up ...
5
votes
1answer
248 views

Compacting a sequence in PostgreSQL

I have a id serial PRIMARY KEY column in a PostgreSQL table. Many ids are missing because I have deleted the corresponding row. Now I want to "compact" the table by restarting the sequence and ...
3
votes
3answers
342 views

MySQL - Unable to Convert Primary Key to Auto Increment

I have a MySQL table where the Primary Key (int) was set by the software to be equal to the Primary Key (auto incremented) of the main table. Because of a change in design I now need the field to auto ...
0
votes
1answer
51 views

Is unique index combined with a “not null” constraint equivalent to a PK constraint?

I have these legacy table definitions (from here): CREATE TABLE [dbo].[JobItems] ( [ItemId] UNIQUEIDENTIFIER NOT NULL, -- lots of other columns CONSTRAINT [PrimaryKey_GUID_HERE] ...
2
votes
3answers
273 views

Why does PK constraint require a separate index when I already have a suitable unique clustered index?

This is part of table definition from here: CREATE TABLE [dbo].[JobItems] ( [ItemId] UNIQUEIDENTIFIER NOT NULL, -- lots of other columns CONSTRAINT [PrimaryKey_GUID_HERE] PRIMARY KEY ...
5
votes
1answer
96 views

Compound Primary Key - Index both fields?

I have a many-to-many join table between users and roles. CREATE TABLE AppUserRole ( UserId INT NOT NULL, RoleId INT NOT NULL, CONSTRAINT PK_AppUserRole PRIMARY KEY (UserId, RoleId), ...
1
vote
0answers
16 views

Table with composite unique cluserted key and non-clustered surrogate primary key [closed]

We are running a legacy application which is an OLTP SQL 2008 R2 database having all tables defined as heaps with non-clustered index on surrogate primary key. I am considering introducing ...
0
votes
0answers
44 views

PostgreSQL 9.3: Minimize locking when updating a large table with primary key

I am building a DB solution to handle a fast stream of online events. Each event UPSERTs a row in a location_update table, using its car_id primary key. Since there are very few new car_ids, the ...
0
votes
1answer
34 views

How should I handle unknown primary keys? [closed]

I'm working as a database-administrator at a school. Recently the school upgraded its pupil administrative software into a web-based solution. This works great, but this web-based solution is missing ...
8
votes
3answers
222 views

Is it reasonable to mark all columns but one as primary key?

I have a table representing movies. The fields are: id (PK), title, genre, runtime, released_in, tags, origin, downloads. My database cannot be polluted by duplicated rows, so I want to enforce ...
1
vote
0answers
68 views

How to pass the value of PK to FK

Ok, so here it goes. I have a table called employee. then I have another table called languages. I need to copy the value of PK of employee table to foreign key in Languages table, rather than ...