I wanted to ask if anyone knows how to see in mysql the foreign key that has a table, because it seems that with show create table is not seen.
I wanted to ask if anyone knows how to see in mysql the foreign key that has a table, because it seems that with show create table is not seen.
Through MySQL from the command line, if the relationship was created successfully, just carry out the following command on the table that includes them:
DESCRIBE TABLE posts;
Remember that it is very important that your foreign key be convened in the mode: nombretabla_id
, that is usuario_id
since this will also help you to be clear about your foreign keys.
Just like in the column KEY
you identify it because it has the following PRI of PRIMARY KEY
Imagine the following scenario, you have a user table and you have other roles:
CREATE TABLE roles(
id INT PRIMARY KEY AUTO_INCREMENT,
rol_name VARCHAR(30) NOT NULL UNIQUE,
rol_status TINYINT(1) NOT NULL
)ENGINE=INNODB;
CREATE TABLE users(
id INT PRIMARY KEY AUTO_INCREMENT,
user_name VARCHAR(20) NOT NULL UNIQUE,
status_user TINYINT(1) NOT NULL,
rol_id INT NOT NULL,
CONSTRAINT fk_users_roles FOREIGN KEY(rol_id) REFERENCES roles(id)
);
So if you create these tables and then do the command that I already wrote you will come out:
That field that says rol_id
is my foreign key
You can use a describe
of your table, where columns, type and keys will appear (if primary or foreign):
describe tu_tabla;
Another way is to consult the construction script with:
show create table tu_tabla;
to determine which tables you are referring to.
Or make a query at information_schema
of mysql:
SELECT * FROM information_schema.TABLE_CONSTRAINTS
WHERE information_schema.TABLE_CONSTRAINTS.CONSTRAINT_TYPE = 'FOREIGN KEY'
AND information_schema.TABLE_CONSTRAINTS.TABLE_SCHEMA = 'tu_db'
AND information_schema.TABLE_CONSTRAINTS.TABLE_NAME = 'tu_tabla';