Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to see my table column is foreign key or not in H2 database

Tags:

database

h2

I have created two tables user and contact. The user is the parent and contact is the child table. I refer userId as the foreign key in contact table. I have gone through the following query.

CREATE TABLE user(
  userId INT NOT NULL AUTO_INCREMENT,
  name VARCHAR(50) NULL,
  phone VARCHAR(50) NULL,
  email VARCHAR(50) NULL,
  address VARCHAR(150) NULL,
  loginName VARCHAR(45) NOT NULL,
  password VARCHAR(50) NOT NULL,
  role INT(1) NOT NULL DEFAULT 2,
  loginStatus INT(1) NOT NULL DEFAULT 1,
  PRIMARY KEY(userId),
);

=====Second Table=========

CREATE TABLE contact(
  contactId INT NOT NULL AUTO_INCREMENT,
  userId INT NULL,
  name VARCHAR(50) NULL,
  phone VARCHAR(50) NULL,
  email VARCHAR(50) NULL,
  address VARCHAR(150) NULL,
  remark VARCHAR(150) NULL,
  PRIMARY KEY(contactId),
  CONSTRAINT fk_con_userId FOREIGN KEY (userId)
    REFERENCES user (userId)
  ON DELETE CASCADE ON UPDATE NO ACTION
);

And to see the column information I write the following query in H2 database.

show columns from contact;I assume the red area should have shown me that the userId is foreign key

But my table does not show the userId as the foreign key.

like image 697
Noman_ibrahim Avatar asked Apr 22 '18 20:04

Noman_ibrahim


People also ask

How do you check whether a column is a foreign key?

You can use the OBJECTPROPERTY() function in SQL Server to check whether or not a table has one or more foreign key constraints. To do this, pass the table's object ID as the first argument, and TableHasForeignKey as the second argument.

Does H2 support foreign key?

H2 Alter Table Add Foreign Key. The RazorSQL alter table tool includes an Add Foreign Key option for adding foreign keys to H2 database tables. The add foreign key function lists all of the columns of the table and allows the user to choose one or more columns to add to the foreign key for the table.

Which column is the foreign key in the table?

The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders" table. The FOREIGN KEY constraint prevents invalid data from being inserted into the foreign key column, because it has to be one of the values contained in the parent table.

How to find the foreign key of a table?

You can also see the list of foreign keys in Keys folder under the table. With this option, if the naming is right, you see referenced table but usually not which column has a foreign key. SSMS by default names new foreign keys in following way: where Mytable is a table that holds foreign key and Othertable is the primary key table.

How to name a foreign key in H2?

Show activity on this post. By using a Constaint Name Definition the foreign key is named explicitly, otherwise H2 assigns it a name based on it's own naming scheme e.g. CONSTRAINT_74. I feel this makes it safer to manage the constraint later by avoiding ambiguity on use of the name and referencing the name directly defined previously e.g.

How do I view metadata from a foreign key in access?

For more information, see Metadata Visibility Configuration. Open the Table Designer for the table containing the foreign key you want to view, right-click in the Table Designer, and choose Relationships from the shortcut menu. In the Foreign Key Relationships dialog box, select the relationship with properties you want to view.

How to see table foreign keys in SSMS?

Another way to see table foreign keys is to create diagram, add table and their related tables. Links will show you existing foreign keys. See complete tutorial on diagrams in SSMS or use this simplified guide:


1 Answers

Yeah, the show command you used does not show the foreign keys. The foreign key you created is there, nevertheless.

To check the foreign key is there you can query the INFORMATION_SCHEMA as in:

select * from information_schema.constraints
  where table_name = 'CONTACT'
    and column_list = 'USERID';

This query shows one row that gives you the details of your FK. The column SQL has the value:

ALTER TABLE PUBLIC.CONTACT ADD CONSTRAINT PUBLIC.FK_CON_USERID FOREIGN KEY(USERID) INDEX PUBLIC.FK_CON_USERID_INDEX_6 REFERENCES PUBLIC.USER(USERID) ON DELETE CASCADE NOCHECK

like image 182
The Impaler Avatar answered Oct 13 '22 12:10

The Impaler