Sei sulla pagina 1di 4

View Latest Questions and Answers | Ask Questions?

| Tutorials

Home Java Frameworks Database Technology Web Development Build/Test Tools Servers PHP

Home Sql Mysql-alter Mysql Alter Foreign Key
Search
Posted on: January 16, 2009 at 12:00 AM
Mysql Alter Foreign Key
A Foreign Key is a field that marks to the
primary key of another table. The use of
the foreign key is used to ensure
referential integrity of the data.
Mysql Alter Foreign
Key

A Foreign Key is a field that marks to the
primary key of another table. The use of
the foreign key is used to ensure referential
integrity of the data.
Understand with Example
The Tutorial illustrate an example from 'Mysql Alter Foreign Key'. To understand with example we create
a table 'publisher'. The Table 'publisher' has primary field name 'publisher_id'.
Query to create table "publisher" :
publisher CREATE TABLE ` publisher` (
` publisher_id` decimal( 10 , 0 ) NOT NULL,
` publisher_name` varchar( 50) NOT NULL,
` contact_name` varchar( 50 ) default NULL,
PRIMARY KEY ( ` publisher_id` ) ;
)
Describe the table "publisher" :
The Describe show you the Fieldname, Data Type, Null etc of table publisher.
mysql> describe publisher;
+----------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+---------------+------+-----+---------+-------+
| publisher_id | decimal( 10 , 0 ) | NO | P RI | | |
| publisher_name | varchar( 50 ) | NO | | | |
| contact_name | varchar( 50 ) | Y ES | | | |
Google Advance Search RoseIndia.Net Search
Ask Questions?
If you are facing any
programming issue, such as
compilation errors or not able
to find the code you are
looking for.
Ask your questions, our
development team will try to
give answers to your
questions.
0
Like
4,709 people like
this. Be the first
of your friends.
Tutorial Topics
( 1057 ) Hibernate Framework
( 836 ) Struts Framework
( 567 ) Spring Framework
( 196 ) XML
( 528 ) Ajax
( 109 ) JavaScript
( 1806 ) Java
( 71 ) Web Services
( 145 ) Database
( 90 ) Technology
( 503 ) Web Development
( 406 ) PHP
Mysql Alter Foreign Key http://www.roseindia.net/sql/mysql-alter/mysql-alter-foreign-key.shtml
1 of 4 03-10-2012 23:34
+----------------+---------------+------+-----+---------+-------+
3 rows in set ( 0.02 sec)
Query to create table "book":
The Query create table is used to create a table book with required fieldname and data type
respectively.
CREATE TABLE ` book` (
` book_id` decimal( 10, 0 ) NOT NULL,
` publisher_id` decimal( 10 , 0 ) NOT NULL,
` publisher_name` varchar( 50) NOT NULL
)
Describe table "book":
Query to Alter table Book Add Foreign Key contraint:
mysql> describe book;
+----------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+---------------+------+-----+---------+-------+
| book_id | decimal( 10, 0 ) | NO | | | |
| publisher_id | decimal( 10, 0 ) | N O | | | |
| publisher_name | varchar( 50) | N O | | | |
+----------------+---------------+------+-----+---------+-------+
3 rows in set ( 0.00 sec)
The Query Alter is used to change the structure of existing table 'books' and add constraint foreign key
'publisher_id and publisher_name' that add the referential integrity of the data from parent table to child
table.
mysql> ALTER TABLE book add CONSTRAINT fk_publisher FOREIGN KEY ( p u b l i s h e r _ i d
Query OK, 0 rows affected ( 0.14 sec)
Records: 0 Duplicates: 0 Warnings: 0
Describe table "book"
mysql> describe book;
+----------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+---------------+------+-----+---------+-------+
| book_id | decimal( 10 , 0 ) | NO | | | |
| publisher_id | decimal( 10 , 0 ) | NO | M UL | | |
| publisher_name | varchar( 50 ) | NO | | | |
+----------------+---------------+------+-----+---------+-------+
3 rows in set ( 0.02 sec)
Related Tags for Mysql Alter Foreign Key:
table, data, sed, key, field, int, tab, for, ie, to, ref, ld, e, primary, it, integrity, not, use, im, in,
no, m, nt, refer, s, su, foreign, at, k, mark, is, ha, ar, s, s, ri, referential, ren, referential, th,
ab, abl, hat, fe, pr, o


Mysql Alter Foreign Key http://www.roseindia.net/sql/mysql-alter/mysql-alter-foreign-key.shtml
2 of 4 03-10-2012 23:34
Previous Index Next
Your Name (* ) :
Your Email :
Ask Questions? Discuss: Mysql Alter Foreign Key
Post your Comment


Ads by Google
MySQL
Reporting SQL Server
Database Hosting MySQL
Ads by Google
PHP and MySQL
MySQL Server Support
SQL Database Tool
Ads by Google
SQL Syntax
SQL Query
SQL Tutorial
Mysql Alter Foreign Key http://www.roseindia.net/sql/mysql-alter/mysql-alter-foreign-key.shtml
3 of 4 03-10-2012 23:34
Subject (* ):
Your Comment (* ):


SUBMIT

JSF Development
Outsourcing
ERP
M-Commerce
Flex Development
Software Solutions
Web Designing
Web Redesigning
Web Development
Logo Design
Web Design Packages
Domain Registration
Website Development
SEO Services
Search Eng. Optimization
Search Eng. Submission
SEO Tips
SEO Portfolio
Web Promotion Plans
Web Promotion Services
Content Development
Article Writing
Blog Writing
News Writing
SEO Copywriting
Technical Documentation
Article Marketing
Content Development
ASP.NET Hosting
Unix Hosting
E-Commerce Hosting
Windows Hosting
Hosting Plan
Web Hosting Services
CRM
E-Commerce Solutions
Services
Home Privacy Policy
All Rights are Reserved for Rose India
Mysql Alter Foreign Key http://www.roseindia.net/sql/mysql-alter/mysql-alter-foreign-key.shtml
4 of 4 03-10-2012 23:34

Potrebbero piacerti anche