473,785 Members | 2,801 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to change the primary key for already existing table?

How to change the primary key for already existing table?
Jun 28 '07 #1
2 37722
rickbray66
7 New Member
How to change the primary key for already existing table?

You can do this with an "ALTER TABLE" command:

Suppose you had the following sample table with id as the primary key:


Expand|Select|Wrap|Line Numbers
  1. create table mytest (
  2.   id int not null default 0,
  3.   some_val in not null default 0,
  4.   foo varchar(32),
  5.   primary key (id));
  6.  
Given the above table, the sample "ALTER TABLE" commands would be:

Expand|Select|Wrap|Line Numbers
  1. alter table mytest
  2.   drop primary key;
  3. alter table mytest
  4.   add primary key(id,some_val);
  5.  
Keep in mind, however, that the data in your existing table may affect your ability to effectively change the primary key, such as any NULL values in a field. Any fields you want to add to the primary key definition must be declared as "NOT NULL", so you may have to alter that column as well.


Rick
Jun 28 '07 #2
JD07
1 New Member
But if the primary key is referenced by foreign keys and has an index, constraints are lost by dropping it.
Mar 1 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
6581
by: dev | last post by:
how create a temp table as a copy of a existing table and then update1 field and insert the hole temp table back in the existing table? please any help? if i have 10 fields in 1 record and about 100 records and a field.status=1 in a existing_table and i want to create a temp_table with all the recordse and values of the existing_table and then update the field.status to 2 and insert in 1 query the temp_table in the existing_table
7
7984
by: Mike Hubbard | last post by:
I have read many many messages about temporary tables and stored procedures. Still, I am struggling with a simple concept. I have a java program which creates a temporary table. I now want to access (read) this table in a stored procedure (written in SQL). This stored procedure is called from the same java program. My question is - how do I "compile" the stored procedure and avoid the error message: DB21034E The command was processed...
3
4910
by: Raj | last post by:
Hi, I am trying to add some more information to the table which already has a lot a data (like 2-3000 records). The new information may be adding 2-3 new columns worth. Now my questions are: (1)Is it a good idea to add new columns to the existing table? then it will create these new columns for all old records, will it not result in wasting a lot of space?? (2)Is it a good idea to create a new table with the new information and have as a...
7
4402
by: Nerrad | last post by:
Hi, i am currently working on a project which requires me to create a form to allow the user to change their password. I know there is a user-level security login which is easier and safer but was told that i could and even must not use it. GOD KNOWS WHY So i created a custom login form with userID and password which validates with a table containing all the users and their passwords. Then, i thought it would be easier to allow the...
3
3471
by: leah | last post by:
I encountered problem when i tried to create a primary key in an existing table which does not have one yet. There are too many records in it and it's hard to find out which one is 'duplicated'. I'm not sure whether, if there is any duplicate records in the table. How can i solve this problem? Thanks leah
2
10878
by: VictorS | last post by:
Hello everybody! I’ve inherited a moderate size Employee database. Main Employees table has a SSN as a primary key, but it is a “Text” data type. I’m now told to delete social security numbers due to security reasons, change primary key to preferably autonumber or other unique value, but still keep last 4 digits as a new field. tblMain structure SSN;Text; primary key LastName; Text FirstName;Text
2
1276
by: cyclops | last post by:
Hello everyone, I need to add 10 fields into an existing table which already has 186 fields. but i am not able to do so as i keep getting error message when i try to save the table. Property value is too large. (Error 3309) help on this says "This error can appear if: You have exceeded the maximum number of columns allowed in a table or the maximum number of locks for a single file. The indexed property of a field was changed from...
1
5171
by: arcingmad | last post by:
Hi I am using the following code in a button click event to run a query stored in a .mdb file from an external geographic program ArcGIS via a jet adodb connection I have the need to run an existing make table query, my problem is it will over write an existing table. i.e when run in access a warning that you are about to overwrite an existing table will be shown and you will be prompted to accept or reject. When run via the external program...
1
13498
by: praween4 | last post by:
friends please let me know how to change the name of the column to an already existing table
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9949
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.