Question:
is naming a text field as primary key legal in mysql/sql database?
2013-02-26 22:20:55 UTC
i am coding a database where no name should appear twice (i.e, every name should be unique)..so i decided to give the "names"-column as primary key.

i have so far not seen any database with "name" (i.e, text field) as primary key all i have ever seen is "id"(i.e. number field) as primary key.

so is it legal to do so?
Three answers:
adrian
2013-02-26 22:39:53 UTC
The tec rules of a primary Key are as follows

The PRIMARY KEY constraint uniquely identifies each record in a database table.

Primary keys must contain unique values.

A primary key column cannot contain NULL values.

Each table should have a primary key, and each table can have only ONE primary key.

.........................................................................

So technically there is no rule against it, BUT Primary Key is usually auto incremental (each new entry is given the next number) which does not work for names, this is why Primary Key is usually integer and not varchar, So doing this you will need to make sure Auto increment is turned off.

Also if you do what you suggest it could cause problems latter on if someone decides to change a name, or expand the table and mess up the Primary and render the table broken. I wouldnt do it.
TheMadProfessor
2013-03-01 11:10:28 UTC
Legal yes, but it's usually not a good idea. Using text fields as a primary key means a slowdown in table lookups, index maintenance and such (especially if there will be foreign key usage). Comparisons between numeric values is MUCH faster than between text values (and if it's used as a FK in many other tables, the tablespace savings can be significant.)
Erika
2016-08-09 13:49:08 UTC
I'm not one for all this intellect reading stuff, so I say get to the bottom of matters now. Maintain the dialog in neutral territory, that means do not make it look like your are looking to pressure him in to anything more than he is capable for. I don't consider there is some thing improper with getting a lil clarification. He might even have intended it as a friendly gesture. I'm a female to and that is one thing that perpetually will get us into problem with a man...The uncanny ability to position a spin on any little hint or indirect gesture or simple phrase and make it right into a viable romance. I've a male friend I say i like you to at all times and at the end of each dialog, but when he approach it some other approach than as a friend, he is aware of I left no room for error and told him i'll perpetually and handiest be a friend for existence.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...