addForeignKey
void sqlDataManager.addForeignKey ( String table, String columnName, String foreignTable, String foreignColumnName ) (professional and enterprise editions only)
Description
Manually setup table connection (key matching).
If SqlDataManager.buildSchemas is called, any foreign keys manually added before that point will be overridden or erased.
Parameters
- table Name of the database table with the primary key, as a string.
- columnName Column/field name of the primary key, as a string.
- foreignTable Name of the database table with the foreign key, as a string.
- foreignColumnName Column/field name of the foreign key, as a string.
Return Values
Returns void.
Change Log
Version |
Description |
5.0 |
Available for professional and enterprise editions. |
If the database has some indication of foreign keys then these will be followed automatically. If the database does not allow for foreign key references then you will need to build the table connections using this method.
Examples
Setup Table Connections
// Import classes
import com.screenscraper.datamanager.*;
import com.screenscraper.datamanager.sql.*;
import org.apache.commons.dbcp.BasicDataSource;
// Set Variables
host = "127.0.0.1:3306";
database = "mydb";
username = "user";
password = "pwrd";
parameters = "autoReconnect=true&useCompression=true";
// Build the BasicDataSource for the database connection
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName( "com.mysql.jdbc.Driver" );
ds.setUsername( username );
ds.setPassword( password );
ds.setUrl( "jdbc:mysql://" + host + "/" + database + "?" + parameters );
// Get MySQL datamanager
dm = new SqlDataManager( ds, session );
// Build Schemas
dm.buildSchemas();
// Setup table connections
// parameter order: "child_table", "child_column", "parent_table", "parent_column"
dm.addForeignKey( "job", "person_id", "person", "id");
dm.addForeignKey( "address", "person_id", "person", "id");