Creating a primary key for an existing table--will this blow up my website?

3 posts / 0 new
Last post
#1 Thu, 11/10/2011 - 16:42
Kevin P

Creating a primary key for an existing table--will this blow up my website?

Thanks in advance for any help you can provide on the below!

I have a website that displays a list of college majors that users can use to select and search for schools. I want to edit one of these majors. All of the majors exist in a table that has only one field: major_name

I am unable to edit the records of this field because there is no primary key. My questions:

1) How can I create a primary key for this table, and what type of field/settings should I use?

2) Will creating a primary key affect my website? I don't want to mess up the PHP that runs the search function.

3) Is it better to edit an existing record that I no longer require (such as a major that is no longer offered?) Or, is it better to somehow deactivate that major and create a new record?

Thanks so much for your help!

  • Kevin
Fri, 11/11/2011 - 08:54
Kevin P

Any help would be much appreciated! Thanks!

Fri, 11/11/2011 - 09:15
andreychek

Howdy,

Well, your questions are rather complex to answer succinctly :-)

Primary keys are related to the MySQL table schema -- when you create a table, you can create certain fields as primary keys.

It's also possible to alter the MySQL table schema to make a field a primary key.

But, doing all that is tricky :-)

It may be simpler to do from within phpMyAdmin, but to do it from the MySQL command line, you'd need to use MySQL's "alter table" command. I'd suggest Googling for specific examples, but here is their documentation on it:

http://dev.mysql.com/doc/refman/5.1/en/alter-table.html

As far as how that'll affect your existing site -- well, that's difficult to say for sure without knowing anything about your site, but I suspect it shouldn't matter.

However, since you're dealing with changing your table structure, I'd certainly suggest making a good backup first, and I'd also suggest testing all this on a separate server first so that you don't risk damaging live data :-)

-Eric

Topic locked