Get Foreign Keys

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,‘nameValue‘)");

    ResultSet rs = null;
    DatabaseMetaData meta = conn.getMetaData();
     // The Oracle database stores its table names as Upper-Case,
     // if you pass a table name in lowercase characters, it will not work.
     // MySQL database does not care if table name is uppercase/lowercase.
     //
     rs = meta.getExportedKeys(conn.getCatalog(), null, "survey");
     while (rs.next()) {
       String fkTableName = rs.getString("FKTABLE_NAME");
       String fkColumnName = rs.getString("FKCOLUMN_NAME");
       int fkSequence = rs.getInt("KEY_SEQ");
       System.out.println("getExportedKeys(): fkTableName="+fkTableName);
       System.out.println("getExportedKeys(): fkColumnName="+fkColumnName);
       System.out.println("getExportedKeys(): fkSequence="+fkSequence);
     }

    st.close();
    conn.close();
  }

  private static Connection getHSQLConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    System.out.println("Driver Loaded.");
    String url = "jdbc:hsqldb:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }

  public static Connection getMySqlConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/demo2s";
    String username = "oost";
    String password = "oost";

    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }

  public static Connection getOracleConnection() throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:caspian";
    String username = "mp";
    String password = "mp2";

    Class.forName(driver); // load Oracle driver
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }
}
时间: 2024-11-08 07:39:42

Get Foreign Keys的相关文章

[PostgreSQL] Use Foreign Keys to Ensure Data Integrity in Postgres

Every movie needs a director and every rented movie needs to exist in the store. How do we make sure something in another table exists before inserting new data? This lesson will teach us about foreign keys and references. CREATE TABLE directors ( id

A MySQL foreign keys drop table, re-create table example

Summary: How to drop MySQL database tables and recreate them when you have foreign keyrelationships between the tables. This is pretty obscure, but I thought I'd post it here so I wouldn't forget how to do this ... if you ever have a situation when u

MySQL添加外键Foreign Keys出错,报错[HY000][3780]

今天写开发文档的时候需要做一下数据结构相关的内容,于是就想着一个快捷一点儿的操作,直接在DataGrip里面调用视图,然后将视图打印为PDF,这样就可以直接截图获取到图片了.由于开发的过程中也没有怎么注重外键的建立,因为本来就是一个小小的比赛,所以生成视图的时候外键联系的箭头都没有,真的是自闭了,如果这个直接给评委看,评委估计会气哭...所以我打算花点时间加一些外键上去,心里想着也花不了多少时间,谁知道,这下子还遇到一些哲学问题! 首先看下,本来数据库表视图是这样的,我只是想多增加一些外键约束,

How can I list all foreign keys referencing a given table in SQL Server?

1. EXEC sp_fkeys 'TableName' 2. SELECT obj.name AS FK_NAME, sch.name AS [schema_name], tab1.name AS [table], col1.name AS [column], tab2.name AS [referenced_table], col2.name AS [referenced_column]FROM sys.foreign_key_columns fkcINNER JOIN sys.object

How do I see all foreign keys to a table or column?

down voteaccepted For a Table: SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = '<database>' AND REFERENCED_TABLE_NAME = '<table&g

MySQL Foreign Key

ntroduction to MySQL foreign key A foreign key is a field in a table that matches another field of another table. A foreign key places constraints on data in the related tables, which enables MySQL to maintain referential integrity. Let’s take a look

sql server create foreign key

in table design view(right click table and choose design), right click on a column, and select 'relationship', then create your foreign keys, sql, stop changes that require recreating table: select tools ->options-> designer, then remove checked fro

Database Primary key and Foreign key [From Internet]

Database Primary key and Foreign key --Create Referenced Table CREATE TABLE Department ( DeptID int PRIMARY KEY, --define primary key Name varchar (50) NOT NULL, Address varchar(100) NULL ) --Create Referencing Table CREATE TABLE Employee ( EmpID int

MySQL foreign key - How to define a foreign key in MySQL

table of contents Creating a MySQL foreign key MySQL foreign key support in database engines MySQL foreign keys and ON UPDATE and ON DELETE MySQL foreign key FAQ: How do I define a foreign key in MySQL? Answer: Here's a quick example of how I typical