To add a foreign key (grade_id) to an existing table (users), follow the following steps:
ALTER TABLE users ADD grade_id SMALLINT UNSIGNED NOT NULL DEFAULT 0;
ALTER TABLE users ADD CONSTRAINT fk_grade_id FOREIGN KEY (grade_id) REFERENCES grades(id);
More Related Contents:
- MySQL Creating tables with Foreign Keys giving errno: 150
- MySQL Error 1215: Cannot add foreign key constraint
- Mysql error 1452 – Cannot add or update a child row: a foreign key constraint fails
- MySQL foreign key constraints, cascade delete
- How do I see all foreign keys to a table or column?
- MySQL Cannot Add Foreign Key Constraint
- How to find all tables that have foreign keys that reference particular table.column and have values for those foreign keys?
- Required to join 2 tables with their FKs in a 3rd table
- Error Code: 1005. Can’t create table ‘…’ (errno: 150)
- Basics of Foreign Keys in MySQL?
- MySQL – Conditional Foreign Key Constraints
- How do I use on delete cascade in mysql?
- How to change the foreign key referential action? (behavior)
- How to create relationships in MySQL
- How to truncate a foreign key constrained table?
- MySQL InnoDB foreign key between different databases
- MySQL Removing Some Foreign keys
- Force InnoDB to recheck foreign keys on a table/tables?
- Is it possible to reference one column as multiple foreign keys?
- How to insert values in table with foreign key using MySQL?
- Show constraints on tables command
- Meaning of “n:m” and “1:n” in database design
- MySQL foreign key to allow NULL?
- MySQL Errno 150
- How to use django models with foreign keys in different DBs?
- How to remove constraints from my MySQL table?
- Foreign key constraints: When to use ON UPDATE and ON DELETE
- MySQL “ERROR 1005 (HY000): Can’t create table ‘foo.#sql-12c_4’ (errno: 150)”
- “General error: 1005 Can’t create table” Using Laravel Schema Build and Foreign Keys
- MySQL Relationships