1.外键约束
从表外键的值是对主表主键的引用。
从表外键类型,必须与主表主键类型一致。
- 语法:alert table 从表add [constraint][外键名称]foreign key(从表外键字段名)references 主表(主表的主键);
「外键名称〕用于删除外键约束的, 一般建议“fk”结尾
alert table 从表drop foreign key外键名称
alert table product drop foreign key product_ibfk_1;
简写添加外键
alter table product add foreign key(category_id) references category(cid);
整体添加外键约束
alter table product add constraint product_fk foreign key(category_id) references category(cid);
第一种时候显示外键名称
show create table 表名字(product ) ;
2.多表
- 两张表建立联系就创建一张中间表记录两表的主键
1.商品表
create table product(
pid varchar(32) primary key,
pname varchar(40),
price double,
category_id varchar(32)
);
2.订单表
create table orders(
oid varchar(32) primary key,
totalprice double
);
3.订单项表
create table orderitem(
oid varchar(50),
pid varchar(50)
);
alter table orderitem add primary key(oid,pid);
4.订单表和订单项表的主外键关系
alter table orderitem add constraint orderitem_orders_fk foreign key(oid) references orders(oid);
5.商品表和订单项表的主外键关系
alter table orderitem add constraint orderitem_product_fk foreign key(pid) references product(pid);
当两张表没有建立任何关系的时候,那么可以随意删除其中任何一张表中的任何记录,但是一旦把两张表建立了关系(主外键约束)之后,那么不能删除主表中的数据(这些数据内容在从表中有关联关系的数据)
两种删除方法
1.删除约束名
2.删除从表相关信息再删除
使用外键是为了保证数据完整性
一对多,多对多,一对一(使主键相对应)
3.查询
子查询是主表某一查询值作为从表条件
select * from product where category_id=( select cid from category where cname=’化妆品’);
分页查询
分页操作:使用limit(参数1,参数2)
起始位置(参数1)=(第几页-1)*每页显示的条数(参数2)
举例:商品表中有10条记录,现在需要进行分页显示,每页显示3条数据。现在需要查看第二页的数据。那么应该使用的sql语句是:
select * from product limit 3[(2-1)*3],3;
- 多表查询
select distinct *from category c,product p where c.cid=p.category_id(distinct 去重)
1.内连接查询 inner join
1.1隐式内连接
select *from a,b where 条件
select *from category,product where cid=category_id;
或者
select *from category c,product p where c.cid=p.category_id;
1.2显式内连接inner join ----inner可省略
select *from a inner join b on 条件
select * from category inner join product on cid=category_id;
2.外连接查询 outer join一outer可以省略)
左外连接:left outer join
select*from A left outer join B on条件
select * from category left join product on cid=category_id;
右外连接:right outer join
select*from A right outer join B on条件
select * from category right join product on cid=category_id;
4.sql-jdbc
- 添加junit :项目名右键-buildpath-addlibrary
db.properties(没有空格,没有引号)
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/web09?useUnicode=true&characterEncoding=utf8
username=root
psaaword=0616
----------------TextUtils ----------------------
public class TextUtils {
@Test
public void delete(){
Connection con = null;
PreparedStatement pre = null;
try {
con = JdbcTool3.connection();
String sql = "delete from product where pid=?";
pre = con.prepareStatement(sql);
pre.setString(1, "p001");
int row = pre.executeUpdate();
if (row>0) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
JdbcTool3.relese(pre, con, null);
}
}
@Test
public void add() {
Connection con = null;
PreparedStatement pre = null;
try {
con = JdbcTool2.connection();
String sql = "insert into product values(?,?,?,null)";
pre = con.prepareStatement(sql);
pre.setString(1, "p011");
pre.setString(2, "护舒宝");
pre.setDouble(3, 0.11);
int row = pre.executeUpdate();
if (row>0) {
System.out.println("插入成功");
} else {
System.out.println("插入失败");
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
JdbcTool2.relese(pre, con, null);
}
}
@Test
public void findUserById() {
Connection con = null;
PreparedStatement psta = null;
ResultSet rst = null;
try {
con = JdbcTool.connection();
String sql = "select * from product where pname=?";
psta = con.prepareStatement(sql);
psta.setString(1, "联想");
rst = psta.executeQuery();
while (rst.next()) {
System.out.println("111" + rst.getObject("pname"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcTool.relese(psta, con, rst);
}
}
}
------------------JdbcTool2------------
public class JdbcTool2 {
private static String drive;
private static String sql;
private static String username;
private static String passord;
static {
ResourceBundle bundle = ResourceBundle.getBundle("db");此种较简单,没有后缀
drive = bundle.getString("driver");
sql = bundle.getString("url");
username = bundle.getString("username");
passord = bundle.getString("psaaword");
}
public static Connection connection() {
try {
Class.forName(drive);
} catch (Exception e) {
e.printStackTrace();
}
Connection con = null;
try {
con = DriverManager.getConnection(sql, username, passord);
System.out.println("连接成功");
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public static void relese(Statement sta, Connection con, ResultSet res) {
if (res != null) {
try {
res.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (sta != null) {
try {
sta.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
----------propertie获取类加载器classloader的流对象inputstream--------------------------
public class JdbcTool3 {
private static String drive;
private static String sql;
private static String username;
private static String passord;
static {
try {
ClassLoader classLoader = JdbcTool3.class.getClassLoader();
InputStream input = classLoader.getResourceAsStream("db.properties");
Properties properties = new Properties();
properties.load(input);
drive = properties.getProperty("driver");
sql = properties.getProperty("url");
username = properties.getProperty("username");
passord = properties.getProperty("psaaword");
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection connection() {
try {
Class.forName(drive);
} catch (Exception e) {
e.printStackTrace();
}
Connection con = null;
try {
con = DriverManager.getConnection(sql, username, passord);
System.out.println("连接成功");
} catch (Exception e) {
e.printStackTrace();
}
return con;
}