primary-key

MySql - Is primary key unique by default?

北战南征 提交于 2019-12-03 10:26:20
If I define a column as a primary key in MySql, is it also unique key by default or do I need to also define it as unique key (in case I want it to be unique)? I saw this question What is the difference b/w Primary Key and Unique Key that explain the difference between the two but doesn't exactly answer my question. Does PK is UK by default or I need to explicitly define it. Primary key is always unique in every SQL. You dont have to explicitly define it as UNIQUE. On a side note: You can only have onePrimary key in a table and it never allows null values. Also you can have only one primary

How to alter length of varchar in composite primary key?

南笙酒味 提交于 2019-12-03 10:20:53
In MSSQL I have a table created like this: CREATE TABLE [mytable] (fkid int NOT NULL, data varchar(255) CONSTRAINT DF_mytable_data DEFAULT '' NOT NULL); ALTER TABLE [mytable] ADD CONSTRAINT PK_mytable_data PRIMARY KEY (fkid, data); Now I want to increase the length of the 'data' column from 255 to 4000. If I just try: ALTER TABLE [mytable] ALTER COLUMN data varchar(4000); Then I get this error: The object 'PK_mytable_data' is dependent on the column 'data' If I try this: ALTER TABLE [mytable] DROP CONSTRAINT PK_mytable_data; ALTER TABLE [mytable] ALTER COLUMN data varchar(4000); ALTER TABLE

When we don't need a primary key for our table?

怎甘沉沦 提交于 2019-12-03 10:12:14
问题 Will it ever happen that we design a table that doesn't need a primary key? 回答1: No. The primary key does a lot of stuff behind-the-scenes, even if your application never uses it. For example: clustering improves efficiency (because heap tables are a mess). Not to mention, if ANYONE ever has to do something on your table that requires pulling a specific row and you don't have a primary key, you are the bad guy. 回答2: Yes. If you have a table that will always be fetched completely, and is being

Using text as a primary key in SQLite table bad?

人盡茶涼 提交于 2019-12-03 09:43:28
Is it bad to have text as a primary key in an SQLite database? I heard that it's bad for performance reasons, is this true? And will the rowid be used as the actual primary key in such a case? Is it bad to have text as a primary key in an SQLite database? I heard that it's bad for performance reasons, is this true? I never heard that somebody used string as primary key in table. For me (and I honestly hope also for others) very "ugly" practise with very low performance. If you'll use string as primary key you needs to think about a "few" things: Will be combination of 3 symbols enough? Or

ID for tags in tag systems

会有一股神秘感。 提交于 2019-12-03 08:51:24
问题 I'm implementing a tag system similar to StackOverflow tag system. I was thinking about when storing the tags and relating to a question, that relationship will be directly with the tag name or it's better create a field tagID to "link" the question with the tag? Looks that linking directly to tag name is easier, but it doesn't look good, mainly why when working with statistics and/or tag categorization (IMHO) can be hard to manage this. Another problem is when one admin decides "fix" a tag

How to manage non-autoincrement primary key in Rails?

廉价感情. 提交于 2019-12-03 08:38:44
I have lots of situations where I'd like to have a non-autoincrement primary key when using Rails. Example: I have one-to-one relationship between A and B. B describes some specific features added to A thus can't exist without A. So we have: A has one B B belongs to A The natural thinking would be having B.A_id as primary key. So I tried create_table b, :id=>false in migration and set_primary_key :a_id in B's model, but it doesn't create actual primary keys in the database. And I want them (as well as foreign keys), as the database will be used not only by this Rails app. If I create primary

surrogate vs natural key: hard numbers on performance differences?

扶醉桌前 提交于 2019-12-03 07:45:56
问题 There's a healthy debate out there between surrogate and natural keys: SO Post 1 SO Post 2 My opinion, which seems to be in line with the majority (it's a slim majority), is that you should use surrogate keys unless a natural key is completely obvious and guaranteed not to change. Then you should enforce uniqueness on the natural key. Which means surrogate keys almost all of the time. Example of the two approaches, starting with a Company table: 1: Surrogate key: Table has an ID field which

What data type is recommended for ID columns?

不羁岁月 提交于 2019-12-03 07:25:55
I realize this question is very likely to have been asked before, but I've searched around a little among questions on StackOverflow, and I didn't really find an answer to mine, so here goes. If you find a duplicate, please link to it. For some reason I prefer to use Guid s ( uniqueidentifier in MsSql) for my primary key fields, but I really don't know why this would be better. In many of tutorials I've walked myself through lately an automatically incremented int has been used. I can see pro's and cons with both: A Guid is always of the same size and length, and there is no reason to worry

How to define a VB.NET DataTable Column as primary key after creation

此生再无相见时 提交于 2019-12-03 07:24:41
I am importing Tables from a Oracle DataBase, using a VB.NET dataAdapter. I use the "fill" command to add the imported data to a DataSet. How is it possible to define a specific column of a DataTable as PrimaryKey, after the DataTable is already filled with data? You can set the primary key of a table by: Dim table As New DataTable() table.Columns.Add(New DataColumn("MyColumn")) Dim primaryKey(1) As DataColumn primaryKey(1) = table.Columns("MyColumn") table.PrimaryKey = primaryKey To be able to use the primary key, you need to ensure that all values for the given column are unique. I primarily

Get a List of all Primary Keys in a Database

北战南征 提交于 2019-12-03 07:24:32
问题 Is this the best way to - Get a List of all Primary Keys in a Database - or is there something better? SELECT KCU.TABLE_NAME AS Table_Name, KCU.CONSTRAINT_NAME AS Constraint_Name, KCU.COLUMN_NAME AS COLUMN_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU ON KCU.CONSTRAINT_SCHEMA = TC.CONSTRAINT_SCHEMA AND KCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME AND KCU.TABLE_SCHEMA = TC.TABLE_SCHEMA AND KCU.TABLE_NAME = TC.TABLE_NAME WHERE TC.CONSTRAINT_TYPE =