In IOS coredata, multi-table queries do not have SQL intuition, but coredata can still perform related operations.
For multi-table queries, there must be a certain relationship between the table and the table. For operations such as external connections and left connections, coredata will become powerless (Please advise ).
In the previous section, we introduced the relational query operations for databases.
The following shows the relationship between tables in a relational database using coredata.
The relationship between the generation of coredata and how to set the relationship will not be discussed in detail. See the previous article.
The created relationship graph:
Establish the above relationship diagram step by step:
First, create a department table, employee table, rank table, salary level table, and account bank table.
After creating the table above, we also need to establish the relationship between the tables.
Relationship between departments and employees: 1 V N
Relationship between departments and positions: 1 V N
Relationship between employees and positions: many-to-one relationship
Employee and bank: one employee can only provide one bank, but one bank can open cards for multiple employees. Therefore, the relationship is n V 1;
Position and salary level: one position corresponds to only one salary level; 1v1
Insert test data below:
-(Ibaction) onbtnclick :( ID) sender
{
Department * dept = [nsentitydescription insertnewobjectforentityforname: @ "department" inmanagedobjectcontext: Self. managedobjectcontext];
Dept. dp_deptname = @ "HR ";
Department * dept2 = [nsentitydescription insertnewobjectforentityforname: @ "department" inmanagedobjectcontext: Self. managedobjectcontext];
Dept2.dp _ deptname = @ "Dev ";
Department * dept3 = [nsentitydescription insertnewobjectforentityforname: @ "department" inmanagedobjectcontext: Self. managedobjectcontext];
Dept3.dp _ deptname = @ "pod ";
Salary * Sy = [nsentitydescription insertnewobjectforentityforname: @ "salary" inmanagedobjectcontext: Self. managedobjectcontext];
Sy. sy_level = @ "D ";
Sy. sy_scale = [nsnumber numberwithdouble: 0.1];
Salary * sy2 = [nsentitydescription insertnewobjectforentityforname: @ "salary" inmanagedobjectcontext: Self. managedobjectcontext];
Sy2.sy _ Level = @ "C ";
Sy2.sy _ scale = [nsnumber numberwithdouble: 0.15];
Salary * sy3 = [nsentitydescription insertnewobjectforentityforname: @ "salary" inmanagedobjectcontext: Self. managedobjectcontext];
Sy3.sy _ Level = @ "B ";
Sy3.sy _ scale = [nsnumber numberwithdouble: 0.4];
Salary * sy4 = [nsentitydescription insertnewobjectforentityforname: @ "salary" inmanagedobjectcontext: Self. managedobjectcontext];
Sy4.sy _ Level = @ "";
Sy4.sy _ scale = [nsnumber numberwithdouble: 0.8];
Post * PT = [nsentitydescription insertnewobjectforentityforname: @ "Post" inmanagedobjectcontext: Self. managedobjectcontext];
Pt.pt _ name = @ "administrative specialist ";
PT. Dept = dept;
Post * pt2 = [nsentitydescription insertnewobjectforentityforname: @ "Post" inmanagedobjectcontext: Self. managedobjectcontext];
Pt2.salary = sy3;
Pt2.pt _ name = @ "Personnel Manager ";
Pt2.dept = dept;
Post * pt3 = [nsentitydescription insertnewobjectforentityforname: @ "Post" inmanagedobjectcontext: Self. managedobjectcontext];
Pt3.pt _ name = @ "Development Engineer ";
Pt3.dept = dept2;
Pt3.salary = sy2;
PT = [nsentitydescription insertnewobjectforentityforname: @ "Post" inmanagedobjectcontext: Self. managedobjectcontext];
Pt.pt _ name = @ "architect ";
PT. Dept = dept2;
PT = [nsentitydescription insertnewobjectforentityforname: @ "Post" inmanagedobjectcontext: Self. managedobjectcontext];
Pt.pt _ name = @ "Project Manager ";
PT. Dept = dept2;
Post * pt6 = [nsentitydescription insertnewobjectforentityforname: @ "Post" inmanagedobjectcontext: Self. managedobjectcontext];
Pt6.pt _ name = @ "Test Engineer ";
Pt6.dept = dept2;
Pt6.salary = sy;
PT = [nsentitydescription insertnewobjectforentityforname: @ "Post" inmanagedobjectcontext: Self. managedobjectcontext];
Pt.pt _ name = @ "sales representative ";
PT. Dept = dept3;
PT = [nsentitydescription insertnewobjectforentityforname: @ "Post" inmanagedobjectcontext: Self. managedobjectcontext];
Pt.pt _ name = @ "Sales Manager ";
PT. Dept = dept3;
Post * pt9 = [nsentitydescription insertnewobjectforentityforname: @ "Post" inmanagedobjectcontext: Self. managedobjectcontext];
Pt9.pt _ name = @ "Key Account Manager ";
Pt9.dept = dept3;
Pt9.salary = sy4;
Bank * BK = [nsentitydescription insertnewobjectforentityforname: @ "bank" inmanagedobjectcontext: Self. managedobjectcontext];
BK. bk_name = @ "";
BK. bk_address = @ "Guangzhou ";
Bank * BK2 = [nsentitydescription insertnewobjectforentityforname: @ "bank" inmanagedobjectcontext: Self. managedobjectcontext];
Bk2.bk _ name = @ "PUFA ";
Bk2.bk _ address = @ "Shanghai ";
Bank * bk3 = [nsentitydescription insertnewobjectforentityforname: @ "bank" inmanagedobjectcontext: Self. managedobjectcontext];
Bk3.bk _ name = @ "ICBC ";
Bk3.bk _ address = @ "Shenzhen ";
Employee * em = [nsentitydescription insertnewobjectforentityforname: @ "employee" inmanagedobjectcontext: Self. managedobjectcontext];
Em. em_age = [nsnumber numberwithint: 20];
Em. em_name = @ "James ";
Em. em_sex = [nsnumber numberwithint: 1];
Em_bankcardid = @ "46326587439043 ";
Em. Dept = dept2;
Em. Post = pt3;
Em. Bank = BK2;
Em = [nsentitydescription insertnewobjectforentityforname: @ "employee" inmanagedobjectcontext: Self. managedobjectcontext];
Em. em_age = [nsnumber numberwithint: 18];
Em. em_name = @ "Li Si ";
Em. em_sex = [nsnumber numberwithint: 2];
Em_bankcardid = @ "32565443246567 ";
Em. Dept = dept;
Em. Post = pt2;
Em. Bank = bk3;
Em = [nsentitydescription insertnewobjectforentityforname: @ "employee" inmanagedobjectcontext: Self. managedobjectcontext];
Em. em_age = [nsnumber numberwithint: 26];
Em. em_name = @ "Ouyang ";
Em. em_sex = [nsnumber numberwithint: 2];
Em_bankcardid = @ "14354654656767 ";
Em. Dept = dept3;
Em. Post = pt9;
Em. Bank = bk3;
Em = [nsentitydescription insertnewobjectforentityforname: @ "employee" inmanagedobjectcontext: Self. managedobjectcontext];
Em. em_age = [nsnumber numberwithint: 22];
Em. em_name = @ "Ouyang ";
Em. em_sex = [nsnumber numberwithint: 2];
Em_bankcardid = @ "9873429837433 ";
Em. Dept = dept2;
Em. Post = pt6;
Em. Bank = bk3;
[Self. managedobjectcontext save: Nil];
}
Data in the database:
1. query the salary level of Michael Jacob in the Development Department
Nsentitydescription * emety = [nsentitydescription entityforname: @ "employee" inmanagedobjectcontext: Self. managedobjectcontext];
Nsfetchrequest * frq = [[nsfetchrequest alloc] init] autorelease];
[Frq setentity: emety];
Nspredicate * CDT = [nspredicate predicatewithformat: @ "em_name = % @", @ "James"];
[Frq setpredicate: CDT];
Nsarray * objs = [self. managedobjectcontext executefetchrequest: frq error: Nil];
// Nslog (@ "% I", [objs count]);
Nslog (@ "% @", (employee *) [objs objectatindex: 0]). Post. Salary. sy_level );
2. query the salary grade and account bank of the O & M Department named Ouyang
Nsentitydescription * entity = [nsentitydescription entityforname: @ "employee" inmanagedobjectcontext: Self. managedobjectcontext];
Nsfetchrequest * fetch = [[nsfetchrequest alloc] init] autorelease];
[Fetch setentity: entity];
Nspredicate * qcmd = [nspredicate predicatewithformat: @ "em_name = % @", @ "Ouyang"];
[Fetch setpredicate: qcmd];
Nsarray * obs = [self. managedobjectcontext executefetchrequest: Fetch error: Nil];
Nspredicate * filter = [nspredicate predicatewithformat: @ "dept. dp_deptname = % @", @ "pod"];
Nsarray * ret = [obs filteredarrayusingpredicate: Filter]; // filter data from the array.
Nslog (@ "% @", (employee *) [RET objectatindex: 0]). Post. Salary. sy_level );
Multi-table queries mainly involve establishing correlation relationships between tables, and fully using the nspredicate query condition for filtering.