create database bankdb;
use bankdb;
##drop table user_info
create table user_info(
user_id int auto_increment primary key,
user_name nvarchar(30),
user_*** nvarchar(4)
);##drop table card_type
create table card_type(
usertype_id int auto_increment primary key,
usertype_name nvarchar(30)
);##drop table user_card
create table user_card(
usercard_id int auto_increment primary key,
usercard_number nvarchar(30),
usercard_pwd int,
usercard_remark text,
cardtype_id int,#外來鍵user_type
user_id int,#外來鍵user_info
foreign key(cardtype_id) references card_type(usertype_id) on delete cascade,##建立外來鍵關係語句
foreign key(user_id) references user_info(user_id) on delete cascade ##建立外來鍵關係語句
)type=innodb;
insert into user_card values (null,'500226198705088574',123456,'備註',1,1);
insert into user_card values (null,'500226199995088574',123456,'備註',2,1);
insert into user_card values (null,'500226155505088574',123456,'備註',3,1);
insert into user_card values (null,'500226198705088574',123456,'備註',1,2);
insert into user_card values (null,'500226777705088574',123456,'備註',1,3);
insert into user_card values (null,'500226192115088574',123456,'備註',1,4);
insert into user_card values (null,'500226097050488574',123456,'備註',1,6);
insert into user_card values (null,'500226777705088574',123456,'備註',2,3);
insert into user_card values (null,'500226192115088574',123456,'備註',3,4);
insert into user_card values (null,'500226097050488574',123456,'備註',1,8);
insert into user_info values (null,'張三0','男');
insert into user_info values (null,'張三1','女');
insert into user_info values (null,'張三2','男');
insert into user_info values (null,'張三3','男');
insert into user_info values (null,'張三4','男');
insert into user_info values (null,'張三5','男');
insert into user_info values (null,'張三6','女');
insert into user_info values (null,'張三7','女');
insert into card_type values (null,'信貸卡');
insert into card_type values (null,'借記卡');
insert into card_type values (null,'消費卡');
select * from user_info;
select * from card_type;
select * from user_card;
######查詢卡:信貸卡的使用者資訊記錄#######
##方法一:
select user_info.user_id,user_info.user_name,user_info.user_***
from user_info,user_card,card_type
where user_info.user_id=user_card.user_id
and user_card.cardtype_id=card_type.usertype_id
and card_type.usertype_name='信貸卡';
select * from user_info,user_card,card_type
##方法二
select * from user_info where user_id in(
select user_id from user_card where cardtype_id =(
select usertype_id from card_type where usertype_name='信貸卡')
);##左連線(以user_info為顯示基礎,user_card沒有符合條件則以null填充顯示)
select distinct * from user_info left join user_card on user_info.user_id=user_card.user_id
##右連線(與上面相反)
select distinct * from user_info right join user_card on user_info.user_id=user_card.user_id
##內連線(on等同於where)
select distinct * from user_info inner join user_card on user_info.user_id=user_card.user_id
Mysql 建立表外來鍵
我自己的步驟 1.定義表,可以指定主鍵 create table sc sno char 9 courseno char 10 grade smallint,primary key sno,courseno 2.插入資料 insert into sc values 31101 01 89 inser...
mysql建立外來鍵報錯 Mysql表建立外來鍵報錯
資料庫表a create table task desc tab id int 11 primary key not null comment 自增主鍵 auto increment,taskname varchar 200 not null comment 任務名字 sqlname varchar...
MYSQL資料表建立外來鍵
mysql建立關聯表可以理解為是兩個表之間有個外來鍵關係,但這兩個表必須滿足三個條件 1.兩個表必須是innodb資料引擎 2.使用在外鍵關係的域必須為索引型 index 3.使用在外鍵關係的域必須與資料型別相似 例如 1 建立s user表 create table s user u id int...