mysql数据库-初始化sql建库建表-关联查询投影问题

2023-04-25,,

下面是一个简易商城的几张表的创建方式

drop database if exists shop ;
create database shop CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
-- 授权
-- 将数据库shop的所有操作权限授予root用户
';
use shop;

-- 用户表
create table t_user(
    id ) primary key auto_increment,
    username ) comment '用户名',
    passwd ) comment '密码',
    nickname ) comment '昵称',
    type ) comment '用户类型'
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 收货地址表
create table t_address(
    id ) primary key auto_increment,
    name ) comment '收件人',
    phone ),
    postcode ),
    detail ) comment '详细地址',
    ),
    constraint foreign key(user_id) references t_user(id)
);
-- 订单表
create table t_orders(
    id ) primary key auto_increment,
    buy_date datetime comment '下单时间',
    pay_date datetime comment '付款时间',
    confirm_date datetime comment '确认时间',
    state ),
    ),
    addr_id ),
    constraint foreign key(user_id) references t_user(id),
    constraint foreign key(addr_id) references t_address(id)
);
-- 商品类别表
create table t_category(
    id ) primary key auto_increment,
    name )
);
-- 商品表
create table t_goods(
    id ) primary key auto_increment,
    name ) comment '商品名',
    intro text comment '商品简介',
    price double,
    img ) comment '图片地址',
    stock ) comment '库存',
    category_id ),
    constraint foreign key(category_id) references t_category(id)
);
-- 商品订单多对多关联表
create table t_goods_orders(
    id ) primary key auto_increment,
    goods_id ),
    orders_id ),
    constraint foreign key(goods_id) references t_goods(id),
    constraint foreign key(orders_id) references t_orders(id)
);
-- 购物车不存数据库

#注意 -- (双长划) 注释风格要求在两个长划后至少有一个空格! 

导入到mysql数据库  source sql路径;

-- 查询地址的同时,将用户信息也查询出来
-- 查询出来发现,投影部分有两个id,需要额外处理 java语言中的sql语句
 String sql="select *,t1.id as a_id,t2.id as u_id from t_address t1 left join t_user t2 on(t1.user_id=t2.id) where user_id=? ";
        

mysql数据库-初始化sql建库建表-关联查询投影问题的相关教程结束。

《mysql数据库-初始化sql建库建表-关联查询投影问题.doc》

下载本文的Word格式文档,以方便收藏与打印。