表中的数据库列限制

时间:2014-05-06 06:07:38

标签: mysql sql database jdbc

我有一个Web应用程序,我使用数据库存储一个人的详细信息,其不超过20列但我的一个朋友说我有3到4个带有外键约束的小表。

以下是我的注册表:

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
);

以下是我的登录表:

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 
);

所以我的朋友说要记住表现还是我可以按照上面的方式进行操作?作为这个外键概念的新手,需要你的指导。

如果我必须将表格分成带有外键概念的小表,那么下面的DAO如何被修改?

以下是所有CRUD操作的DAO?如何进行更改?如果有的话我需要将我的表分成小表?

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) {
            }
    }
}


}

请帮助理解这一点。我还浏览了一篇stackoverflow文章,其中给出了如果数据库列属于同一个实体,则无需将其划分为小表。我认为我的表列都是同一个实体。

1 个答案:

答案 0 :(得分:0)

我取决于你想要实现的目标,但标准是表应该代表实体。您的表名称是指您系统上的操作,而不是它们所持有的信息的实体。

当你开始将表看作实体时,你会发现有1个表用于地址,1个表用于用户,1个表用于联系人(我认为这是你的系统应该保留的)是有意义的。您甚至可以超越并看到该用户和联系人共享一些数据并将这些数据分组到一个人或人员表中,但我不会这样做,因为过多的规范化将使您更难以工作。

最后你应该得到像

这样的东西

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..

希望这有帮助。

相关问题