On Thu, Dec 13, 2012 at 7:53 AM, laxglx <laxgl...@gmail.com> wrote: > Can anybody plz tell me how to add a foreign key an existing table using SQL > Queries? >>> I got the command, nut can't understand > > ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) > REFERENCES addresses (address) MATCH FULL;
as Chris has noted, this question as stated doesn't have anything to do with Django. what i'm guessing here is that you got that command from your database docs and want to know how to apply that to modify your tables. right? in principle, you shouldn't have to need that. Django lets you define the relationships in the model declaration, including foreign keys constraints and indexes. but.... if you have just added the foreign key, Django won't alter an existing table. (the syncdb command never modifies an existing table, it only creates new tables when not found on the database) if that's the case, you have three options: - delete the table and redo the syncdb. Django will create the table with the current definition in the models. pro: works every time. con: you lose all existing data on that table. - analyze the current table structure, what the new model represents and manually issue the needed ALTER TABLEs. pro: won't lose data if done correctly. con: not easy to do it right. - use South. it handles all the data structure migrations for you. pro: very reliable, very easy, very maintanable. con: you have to learn to use it (but it's not hard, and the docs are very good) > > What's "distfk" here if it is key name what is (address) ? and what is > addresses(address)? "distfk" is the name of the constraint. i don't think it's used anywhere except an identifier. the first "address" is the name of your referring field ("source field") the second "address" (in fact "addresses(address)") is the referred field ("target field"). typically it's more like "address_id" for the first and "address(id)" for the second, since in the vast majority of cases it's preferable that foreign key fields use the id field for reference. > And What does mean Match Full that seems specific to your RDBMS. i'll guess that it means use the whole field to match. typically on very long text fields wouldn't be practical to copy the whole content to an index and to the referring key, so it can use a length limited prefix. Match Full sounds like not to use any prefix, but the full field. -- Javier -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.