Question

I have a web application where I use database to store a persons details its not more than 20 columns but one of my friend said me to have 3 to 4 small tables with the foreign key constraints.

Below is my register table:

create table register(
CustId int(100) not null AUTO_INCREMENT primary key,
FirstName varchar(300) default null,
LastName varchar(300) default null,
Gender varchar(200) default null,
Category varchar(200) default null,
DateOfBirth varchar(200) default null,
Age int(3)default null,
Address varchar(1000) default null,
Country varchar(500) default null,
State varchar (500) default null,
city varchar(500)default null,
PinCode int(10)default null,
UserName varchar(500)default null,
EmailId varchar(500)default null,
ContactNo varchar(20) default null,
MobileNo varchar(20) default null,
TimeStamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

Below is my login table:

create table login(
UserName varchar(100)default null,
PassWord varchar(100)default null,
CustId int(100) not null primary key,
FirstName varchar(300) default null,
LastName varchar(300) default null,
Gender varchar(200) default null,
Category varchar(200) default null,
DateOfBirth varchar(200) default null,
Age int(3)default null,
Address varchar(1000) default null,
Country varchar(500) default null,
State varchar (500) default null,
city varchar(500)default null,
PinCode int(10)default null,
EmailId varchar(500)default null,
ContactNo varchar(20) default null,
MobileNo varchar(20) default null,
AffiliateGroup varchar(20)default null,
LastUsed TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 
);

So is it necessary to do what my friend said keeping performance in mind or can I carry on the way it looks above? Need your guidance as am new to this foreign key concepts.

If i have to divide the table into small tables with the foreign key concept then how does the DAO below can be modified?

Below is a DAO with all the CRUD operations? How to make changes? If at all I need to divide my table into small tables?

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.affiliate.DTO.Affiliate;
import com.affiliate.DTO.Client;

public class OnholdDAO {


    private DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;


    public List<Affiliate> listAffiliate() throws SQLException {

        List<Affiliate> affiliates = new ArrayList<Affiliate>();
        Connection conn = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        String sql="SELECT * from onhold where Category='Affiliate'";

        try {
            conn = dataSource.createConnection();
            statement = conn
                    .prepareStatement(sql);
            rs = statement.executeQuery();

            while (rs.next()) {
                Affiliate affiliate = new Affiliate();
                affiliate.setId(rs.getInt("CustId"));
                affiliate.setFirstName(rs.getString("FirstName"));
                affiliate.setLastName(rs.getString("LastName"));
                affiliate.setGender(rs.getString("Gender"));
                affiliate.setCategory(rs.getString("Category"));
                affiliate.setDate(rs.getDate("DateOfBirth"));
                affiliate.setAge(rs.getInt("Age"));
                affiliate.setAddress(rs.getString("Address"));
                affiliate.setCountry(rs.getString("Country"));
                affiliate.setState(rs.getString("State"));
                affiliate.setCity(rs.getString("city"));
                affiliate.setPinCode(rs.getInt("PinCode"));
                affiliate.setUserName(rs.getString("UserName"));
                affiliate.setEmailId(rs.getString("EmailId"));
                affiliate.setContactNo(rs.getString("ContactNo"));
                affiliate.setMobileNo(rs.getString("MobileNo"));
                affiliates.add(affiliate);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);

        } finally {
            if (rs != null)
                try {
                    rs.close();
                } catch (SQLException ignore) {
                }
            if (statement != null)
                try {
                    statement.close();
                } catch (SQLException ignore) {
                }
            if (conn != null)
                try {
                    conn.close();
                } catch (SQLException ignore) {
                }
        }
        return affiliates;
    }


    public List<Client> listClient() throws SQLException {

        List<Client> clients = new ArrayList<Client>();

        String sql="SELECT * from onhold where Category='Client'";
        try {
            conn = dataSource.createConnection();
            ps = conn
                    .prepareStatement(sql);
            rs = ps.executeQuery();

            while (rs.next()) {
                Client client = new Client();
                client.setId(rs.getInt("CustId"));
                client.setFirstName(rs.getString("FirstName"));
                client.setLastName(rs.getString("LastName"));
                client.setGender(rs.getString("Gender"));
                client.setCategory(rs.getString("Category"));
                client.setDate(rs.getDate("DateOfBirth"));
                client.setAge(rs.getInt("Age"));
                client.setAddress(rs.getString("Address"));
                client.setCountry(rs.getString("Country"));
                client.setState(rs.getString("State"));
                client.setCity(rs.getString("city"));
                client.setPinCode(rs.getInt("PinCode"));
                client.setUserName(rs.getString("UserName"));
                client.setEmailId(rs.getString("EmailId"));
                client.setContactNo(rs.getString("ContactNo"));
                client.setMobileNo(rs.getString("MobileNo"));
                clients.add(client);

            }
        } catch (SQLException e) {
            throw new RuntimeException(e);

        } finally {
            if (rs != null)
                try {
                    rs.close();
                } catch (SQLException ignore) {
                }
            if (ps != null)
                try {
                    ps.close();
                } catch (SQLException ignore) {
                }
            if (conn != null)
                try {
                    conn.close();
                } catch (SQLException ignore) {
                }
        }
        return clients;

    }

public void createCustomer(int id){


        String sql="insert into onhold(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        String sql1="insert into onhold(UserName,CustId,FirstName,LastName,Gender,Category,Dateofbirth,Age,Address,Country,State,city,PinCode,EmailId,ContactNo,MobileNo)select  UserName,CustId,FirstName,LastName,Gender,Category,Dateofbirth,Age,Address,Country,State,city,PinCode,EmailId,ContactNo,MobileNo from register where CustId="
                + id + "";

        try {
            conn = dataSource.createConnection();
             ps = conn
                    .prepareStatement(sql);
            ps.executeUpdate(sql1);
            ps.setInt(1, id);

        }

        catch (SQLException e) {
            throw new RuntimeException(e);

        } finally {
            if (rs != null)
                try {
                    rs.close();
                } catch (SQLException ignore) {
                }
            if (ps != null)
                try {
                    ps.close();
                } catch (SQLException ignore) {
                }
            if (conn != null)
                try {
                    conn.close();
                } catch (SQLException ignore) {
                }
        }

    }

public void readClient(int id){

String sql= "select * from onhold where CustId= "+id+"";



        try{


            conn = dataSource.createConnection();
             ps = conn.prepareStatement(sql);

             rs = ps.executeQuery();
            if (rs.next()) {

                rs.getString("FirstName");
                rs.getString("LastName");
                rs.getString("Gender");
                rs.getString("Category");
                rs.getDate("DateOfBirth");
                rs.getString("Age");
                rs.getString("Address");
                rs.getString("Country");
                rs.getString("State");
                rs.getString("City");
                rs.getInt("PinCode");
                rs.getString("UserName");
                rs.getString("EmailId");
                rs.getString("ContactNo");
                rs.getString("MobileNo");

            }
        }

         catch (SQLException e) {
                throw new RuntimeException(e);

            } finally {
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                    }
                }
            }
    }

public void updateCustomer(int id){}


public void deleteCustomer(int id) {
    String sql="delete from onhold where CustId=" + id
            + "";

    try {

        conn = dataSource.createConnection();
         ps = conn
                .prepareStatement(sql);
        ps.executeUpdate();
    } catch (SQLException e) {
        throw new RuntimeException(e);

    } finally {
        if (rs != null)
            try {
                rs.close();
            } catch (SQLException ignore) {
            }
        if (ps != null)
            try {
                ps.close();
            } catch (SQLException ignore) {
            }
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException ignore) {
            }
    }
}


}

Please help in understanding this. I also went through a stackoverflow article where it is given that if a database columns are of the same entity then no need of dividing it into small tables. I think my table columns are all of same entity.

Was it helpful?

Solution

I depends on what you are trying to achieve, but the standard are that tables should represent entities. Your table names refer to actions on your system and not really the entity of the information they are holding.

When you start to look at tables as entities you will see that it would make sense to have 1 table for addresses, 1 table for users, 1 table for contacts(which I think is what your system is supposed to hold). You can even go beyond and see that user and contact share some data and group that data into one person or people table but I wouldn't do this since too much normalisation will make it harder for you to work.

In the end you should end up with something like

Users Table - id (PK) - addressId (FK) - username - password - first_name - last_name - birthdate - etc..

Contacts Table - id (PK) - userId (FK) - addressId (FK) - first_name - last_name - phone - age - birthdate - etc..

Address Table - id (PK) - street1 - street2 - zip - city - etc..

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top