DB
쿼리문을 이용하여 테이블 pk, unique, check, not null 구하기 (oracle)
잘 살아보세
2009. 4. 1. 14:44
예를 들어 테이블 USER 에 다음과 같은 constraint가 걸려있다고 할 경우
constraint PRIMARY_USER primary key (USERID),
constraint PRIMARY2_USER unique (USERNAME)
//constraint 체크
String GET_CONSTRAINT = "select uc.constraint_type, ucc.constraint_name, ucc.column_name from user_constraints uc,
user_cons_columns ucc where ucc.table_name='"+tempName.toUpperCase()
+"' and ucc.constraint_name = uc.constraint_name order by uc.constraint_type";
pstmt3 = con.prepareStatement(GET_CONSTRAINT);
rs3 = pstmt3.executeQuery(); // 선택된 테이블의 모든 정보
rsmd3 = rs3.getMetaData();
String con_type = "";
String con_name = "";
String col_name = "";
while(rs3.next()){
con_type = rs3.getString(1);
if(con_type.equals("P")){
con_type = "primary key"; //primary key
con_name = rs3.getString(2); //PRIMARY_USER
col_name = rs3.getString(3); //USERID
}else if(con_type.equals("U")){
con_type = "unique"; //unique
con_name = rs3.getString(2); //PRIMARY2_USER
col_name = rs3.getString(3); //USERNAME
}else if(con_type.equals("C")){
//CHECK, NOT NULL
}
}
constraint PRIMARY_USER primary key (USERID),
constraint PRIMARY2_USER unique (USERNAME)
//constraint 체크
String GET_CONSTRAINT = "select uc.constraint_type, ucc.constraint_name, ucc.column_name from user_constraints uc,
user_cons_columns ucc where ucc.table_name='"+tempName.toUpperCase()
+"' and ucc.constraint_name = uc.constraint_name order by uc.constraint_type";
pstmt3 = con.prepareStatement(GET_CONSTRAINT);
rs3 = pstmt3.executeQuery(); // 선택된 테이블의 모든 정보
rsmd3 = rs3.getMetaData();
String con_type = "";
String con_name = "";
String col_name = "";
while(rs3.next()){
con_type = rs3.getString(1);
if(con_type.equals("P")){
con_type = "primary key"; //primary key
con_name = rs3.getString(2); //PRIMARY_USER
col_name = rs3.getString(3); //USERID
}else if(con_type.equals("U")){
con_type = "unique"; //unique
con_name = rs3.getString(2); //PRIMARY2_USER
col_name = rs3.getString(3); //USERNAME
}else if(con_type.equals("C")){
//CHECK, NOT NULL
}
}