Relational databases organize data in tables. Each table in the database contains information about objects of concern and relationships that objects have with other objects. So, one can associate a first-order logic predicate with each table.
STUDENT | |
---|---|
SID | NAME |
1111 | Jones |
2222 | Smith |
3333 | Blake |
4444 | Damon |
5555 | Bell |
COURSE | |
---|---|
CNO | TITLE |
CSc2310 | Java |
CSc2510 | Theory |
CSc4710 | Database |
ENROLL | ||
---|---|---|
SID | CNO | GRADE |
1111 | CSc2310 | F |
1111 | CSc2510 | F |
2222 | CSc2510 | A |
2222 | CSc4710 | B |
3333 | CSc2310 | A |
3333 | CSc2510 | A |
3333 | CSc4710 | F |
We can associate predicates student, course, and enroll with the three tables. The arities of the relations correspond to the number of columns in the table.
A query using predicate logic is of the form
{ X1,...,Xn | P(X1,...,Xn) }
where P(X1,...,Xn) is a wff in predicate logic with free variables X1,...,Xn.Note: We will use a slightly different syntax for well-formed formulas to be able to use the Query system on the server. To use the system run the following 2 commands the first time you sign in:
cp ~raj/.cshrc . source .cshrcand to run the query system, execute the following command:
java edu.gsu.cs.drc.DRC ~raj/enrollDBYou will see a DRC> prompt where you can enter a query. To exit the query system, enter the exit; command.
Now, some query examples:
{ N | (exists S,G)(student(S,N) and enroll(S,'CSc2510',G)) }
{ C,T | (exists S,G)(course(C,T) and enroll(S,C,G) and student(S,'Smith')) }
{ N | (exists S,C1,G1,C2,G2)(student(S,N) and enroll(S,C1,G1) and enroll(S,C2,G2) and C1 <> C2) }
{ N | (exists S)(student(S,N) and enroll(S,'CSc2510','A')) }
{S,N | student(S,N) }
{C,T | course(C,T) }
{S,C,G | enroll(S,C,G) }
[~][6:12am] java edu.gsu.cs.drc.DRC ~raj/enrollDB DRC> {S,N | student(S,N) } ANSWER(S:INTEGER,N:VARCHAR) Number of tuples = 5 1111:Jones: 2222:Smith: 3333:Blake: 4444:Damon: 5555:Bell: DRC> {C,T | course(C,T) } ANSWER(C:VARCHAR,T:VARCHAR) Number of tuples = 3 CSc2310:Java: CSc2510:Theory: CSc4710:Database: DRC> {S,C,G | enroll(S,C,G) } ANSWER(S:INTEGER,C:VARCHAR,G:VARCHAR) Number of tuples = 7 1111:CSc2310:F: 1111:CSc2510:F: 2222:CSc2510:A: 2222:CSc4710:B: 3333:CSc2310:A: 3333:CSc2510:A: 3333:CSc4710:F: DRC> { N | (exists S,G)(student(S,N) and enroll(S,'CSc2510',G)) } ANSWER(N:VARCHAR) Number of tuples = 3 Jones: Smith: Blake: DRC> { C,T | (exists S,G)(course(C,T) and enroll(S,C,G) and student(S,'Smith')) } ANSWER(C:VARCHAR,T:VARCHAR) Number of tuples = 2 CSc2510:Theory: CSc4710:Database: DRC> { N | (exists S,C1,G1,C2,G2)(student(S,N) and enroll(S,C1,G1) and enroll(S,C2 ,G2) and C1 <> C2) } ANSWER(N:VARCHAR) Number of tuples = 3 Jones: Smith: Blake: DRC> { N | (exists S)(student(S,N) and enroll(S,'CSc2510','A')) } ANSWER(N:VARCHAR) Number of tuples = 2 Smith: Blake: DRC> exit; [~][6:14am]