Hi,

This is an example:

ALTER TABLE cart ADD CONSTRAINT fk_cart_customers FOREIGN KEY (custid)
 REFERENCES customers (custid)
 ON DELETE CASCADE
 ON UPDATE NO ACTION;

(take from the GUI tool Database Workbench, avoids having to know the syntax ;) )


With regards,

Martijn Tonies
Upscene Productions
http://www.upscene.com

Download Database Workbench for Oracle, MS SQL Server, Sybase SQL
Anywhere, MySQL, InterBase, NexusDB and Firebird!

-----Original Message----- From: thufir
Sent: Monday, November 17, 2014 10:42 AM
To: mysql@lists.mysql.com
Subject: alter table for foreign key

Looking at the docs:

http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

how do I alter the child table, links, so that it has a foreign key with
the parent table, feeds?

The feed_id field in links should, in fact, be constrained by the foreign
key of feeds with a RESTRICT reference option.



mysql>
mysql> show tables;
+---------------------------+
| Tables_in_rome_aggregator |
+---------------------------+
| feeds                     |
| links                     |
| pages                     |
+---------------------------+
3 rows in set (0.00 sec)

mysql>
mysql> describe feeds;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment |
| created  | datetime     | NO   |     | NULL    |                |
| accessed | varchar(45)  | NO   |     | NULL    |                |
| url      | varchar(767) | NO   | UNI | NULL    |                |
| status   | int(11)      | NO   |     | 0       |                |
+----------+--------------+------+-----+---------+----------------+
5 rows in set (0.05 sec)

mysql>
mysql> describe links;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int(11)      | NO   | PRI | NULL    | auto_increment |
| created | datetime     | NO   |     | NULL    |                |
| feed_id | int(11)      | NO   |     | 0       |                |
| link    | varchar(767) | NO   | UNI | NULL    |                |
| status  | int(11)      | NO   |     | 0       |                |
+---------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

mysql>


While I know that I should alter the table, I don't know the syntax to
establish the foreign key constraint.


thanks,

Thufir


--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/mysql

Reply via email to