一、数据 insert into T_Customers(id,name,age) values(1,'tom',21); INSERT INTO T_Customers(id,name,age)VALUES(2,'mike',24); INSERT INTO T_Customers(id,name,age)VALUES(3,'jack',30); insert into T_Customers(id,name,age)VALUES(4,'tom',25); insert into T_Customers(id,name,age)VALUES(5,'linda',null);
insert into T_Ordertypes(ID,NAME)VALUES(1,'现货订单'); insert into T_Ordertypes(id,name)VALUES(2,'预定订单'); insert into T_Ordertypes(id,name)VALUES(3,'预定订单'); insert into T_Ordertypes(id,name)VALUES(4,'内部');
//晕,把T_Orders写成T_Ordertypes了,出现了 error : Unknown column 'customerID' in 'field list'的错误 insert into T_Orders(id,number,price,customerID,TYPEID)VALUES(1,'K001',100,1,1); INSERT INTO T_Orders(ID,NUMBER,PRICE,CUSTOMERID,TYPEID)VALUES(1,'K002',200,1,1); INSERT into T_Orders(id,number,price,customerid,typeid)VALUES(3,'k003',300,1,2); insert into T_Orders(id,number,price,customerid,typeid)VALUES(4,'n002',100,2,2); insert into T_Orders(id,number,price,customerid,typeid)VALUES(5,'n003',500,3,4); insert into T_Orders(id,number,price,customerid,typeid)VALUES(6,'t001',300,4,3); insert into T_Orders(id,number,price,customerid,typeid)VALUES(7,'t002',100,null,1);
二、join 1、多表检索最常用的技术就是表连接。 2、sql中使用join关键字来使用表连接。 3、主流的有cross join 、inner join、outter join。 4、outter join又分为left join(等值连接)、right join
三、连接的语法 1、select o.number,o.price,c.name,c.age from t_orders o left join t_customers c on o.cutomersid=c.id 2、添加where语句select o.number,o.price,c.name,c.age from t_orders o left join t_customers c on o.customersid=c.id where o.price>=150 3、可以join多张表 select o.number 订单号,o.price 价格,c.name 客户姓名,c.age 客户年龄,t.name 订单类型 from t_order o left join t_customerid c on o.cumsterid=c.id left join t_ordertypes t on o.typeid=t.id
四、外键约束 对于初学者,都把外键加上,熟练后可以通过程序进行数据合法性控制。 1、t_orders指向t_customers,如果t_customers里面删除or更新一行记录,可以回出现错误,如何处理t_orders指向t_customers外间的行 2、外键约束建立在外键字段‘***id’的表上(t_orders) 3、新建or修改标的时候,‘外键’-‘添加外键’。