473,406 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

how to make the newly added column as foreign keyto its parent table in mysql?

Hi All,

I have one table for that i want to add one more column and making that column as foreign key to some other table.How can i do that?can any one give the syntax?

here i know the syntax for adding a column:
Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE <table_name> ADD COLUMN <column_name> <datatype>
Regards,
Suma
Sep 12 '07 #1
4 1270
amitpatel66
2,367 Expert 2GB
Hi All,

I have one table for that i want to add one more column and making that column as foreign key to some other table.How can i do that?can any one give the syntax?

here i know the syntax for adding a column:
Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE <table_name> ADD COLUMN <column_name> <datatype>
  2.  
Regards,
Suma
After Adding a column, use the below syntax to add foreign key to the column of a particular table:
Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE <table_name> ADD FOREIGN KEY (<column_name>) REFERENCES <table_name>(<column_name>)
The second column_name in the above query syntax should be a PRIMARY KEY
Sep 12 '07 #2
Thank you very much! i am new to DB.

How can i add primary key constraint to the existing column?

Regards,
suma
Sep 12 '07 #3
amitpatel66
2,367 Expert 2GB
Thank you very much! i am new to DB.

How can i add primary key constraint to the existing column?

Regards,
suma
Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE <table_name> ADD PRIMARY KEY (<column_name>)
Sep 12 '07 #4
pbmods
5,821 Expert 4TB
Please use CODE tags when posting source code:

[CODE=MySQL]
MySQL code goes here.
[/CODE]
Sep 12 '07 #5

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

Similar topics

2
by: kindermaxiz | last post by:
hi there i have a mysql table and i want the user to add columns using a php interface and then display the table with the newly added column. I was thinking about putting all the columns...
5
by: Olivier Crèvecoeur | last post by:
Hello, Excuse me for my poor english. I would kike know if create index on the foreign key it's necessary or if Oracle, are optimized for using foreign key whithout index. Best regards ...
0
by: dcp | last post by:
I just installed the 4.1.0-alpha-max-nt version of MySql and have just started playing around with it. My first test was to try to create a couple of tables, one with a foreign key constraint. ...
0
by: Jeremiah Jacks | last post by:
I just upgraded to MySQL 4.0.14-standard for RedHat Linux and am using = the pre-compiled binaries. I have a database with INNODB tables. When I insert a row into one of the child tables, I get...
1
by: Andrew DeFaria | last post by:
I created the following .sql file to demonstrate a problem I'm having. According to the manual: If |ON DELETE CASCADE| is specified, and a row in the parent table is deleted, then InnoDB...
3
by: bk3 | last post by:
I'm having a bit of a problem creating foreign keys on a MySQL database. The tables create and everything seems to be fine. But I just wanted to test out adding a row to one of the child tables...
1
by: fangio | last post by:
Hello, we're using InnoDB. After defining a table with "ON UPDATE CASCADE" and _without_ "ON DELETE" constraint, we deleted an entry in the parent table. The referenced tuple in the child table...
4
by: Simon Bond | last post by:
Hi there, I should point out I am very new to MySql, im trying to learn what I can. Basically im using Navicat to work with my databases. When making a foreign key, i am asked to enter the...
6
by: tlyczko | last post by:
I am working on an audits database. The main table, Audits, has an AuditID primary key. Another table, 1:1 relationship, ProgramAudits, has AuditID as a foreign key. I have a master/main form...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.