private ErdNode constructNode(String tableName) throws MainException {

		List<ColumnDetail> columns = dbAccess.getDetailsOfColumns(tableName);
		CachedRowSet foreignKeys   = dbAccess.getForeignKeys(tableName);
		Set<String> fkTableNames   = new HashSet<String>();
		List<String> fkColumns     = new ArrayList<String>();
	
		// store all the table names that are being referenced by 'tableName' and all the foreign keys of 'tableName' 
		try {
			while(foreignKeys.next()) {
				fkTableNames.add(foreignKeys.getString("PKTABLE_NAME"));
				fkColumns.add(foreignKeys.getString("FKCOLUMN_NAME"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new MainException("ERDBuilder failed to get foreign keys from table " + tableName + " : " + e.getMessage());
		}
		
		Iterator<String> fkTableNamesItr = fkTableNames.iterator();
		String fkTableName;

		// if a table has no foreign key, it is an entity type
		if (fkTableNames.size() == 0) {
			ErdNode entity = new ErdNode(tableName, tableName, ErdNodeType.ENTITY_TYPE, dbAccess.getDetailsOfColumns(tableName));
			entityTypes.put(tableName, entity);
			return entity;
		}
		
		// if a table's foreign keys only reference 1 table
		else if (fkTableNames.size() == 1) {
			
			fkTableName = fkTableNamesItr.next();
			ErdNode fkNode = getErdNodeOfTable(fkTableName);
			
			List<String> primaryKey    = dbAccess.getPrimaryKeys(tableName);
			boolean isReferenced       = dbAccess.isBeingReferenced(tableName);
			
			// if a table's foreign keys reference 1 other table only and 
			// the table is not being referenced by any other table and if
			// 1) it is an all-key relation / foreign key is part of primary key (non-foreign key is m:m attribute) or 
			// 2) foreign key is non-prime (non-foreign key is 1:m attribute) or
			// 3) foreign key is the primary key (optional m:1 attribute) 
			// then the table is of the same entity type as the table its foreign keys references to (merge both entities)
			boolean isMultiVal1 = (columns.size() == primaryKey.size());
			boolean isMultiVal2 = (!hasIntersection(fkColumns, primaryKey));
			if (!isReferenced && (isMultiVal1 || isMultiVal2 || isEqual(fkColumns, primaryKey))) {
				
				Iterator<ColumnDetail> colItr = columns.iterator();
				while (colItr.hasNext()) {
					ColumnDetail column = colItr.next();
					Iterator<String> fkItr = fkColumns.iterator();
					while (fkItr.hasNext()) {
						String fkName = fkItr.next();
						if (!column.getName().equals(fkName)) {
							// if there is multi valued attributes, mark those values that are not foreign keys as multi valued
							if (isMultiVal1 || isMultiVal2)
								column.setIsMultiValued(true);
							fkNode.addAttribute(column);
						}
					}
				}
				
				return fkNode;
			}
			
			// if a table's foreign keys reference 1 other table only and
			// the table is being referenced by other table or it has non-prime attribute and
			// foreign keys are non-prime and not nullable (EX) or is proper subset of primary key (ID)
			// then the table is a weak entity
			// else the table is an entity
			else {
				ErdNode entity;
				boolean isWeakEntity = true;
				if ((isReferenced || columns.size() > primaryKey.size()) && 
					 ((!hasIntersection(fkColumns, primaryKey) && !isNullableSetOfCols(fkColumns, columns)) || isProperSubset(fkColumns, primaryKey)))
					entity = new ErdNode(tableName, tableName, ErdNodeType.WEAK_ENTITY_TYPE, columns);
				else {
					entity = new ErdNode(tableName, tableName, ErdNodeType.ENTITY_TYPE, columns); 
					isWeakEntity = false;
				}
				
				if (isWeakEntity)
					fkNode.addSpecialLink(entity);
				else {
					fkNode.addLink(entity);
					entity.addLink(fkNode);
				}
				entityTypes.put(tableName, entity);
				return entity;
			}
		}
		
		// if a table's foreign keys references more than 1 tables, that table is a relationship type
		else if (fkTableNames.size() > 1) {
			ErdNode relationship = new ErdNode(tableName, tableName, ErdNodeType.RELATIONSHIP_TYPE, columns);
			
			while(fkTableNamesItr.hasNext()) {
				fkTableName = fkTableNamesItr.next();
				ErdNode fkNode = getErdNodeOfTable(fkTableName);
				
				relationship.addLink(fkNode);
				fkNode.addLink(relationship);
			}
			
			relationshipTypes.put(tableName, relationship);
			return relationship;
		}
		
		else
			throw new MainException("ERDBuilder error constructing node");
		
	}