show databases;

drop database user_info;

create database if not exists nengdie;

use nengdie;

show create database nengdie;

drop table if exists user_info;

create table user_info (
    user_id smallint(5) primary key auto_increment,
    age tinyint(3) not null,
    gender varchar(10) default 'male',
    email varchar(20),
    phone_number varchar(20) unicode
);

describe user_info;
show create table user_info;

# modify column data format
alter table user_info modify phone_number varchar(25);

alter table user_info change gender gender varchar(8) default 'male' after phone_number;

alter table user_info rename user_info1;
show tables;
drop table user_info1;

alter table user_info add user_name varchar(30) after user_id;

describe user_info;

# DML
insert into user_info (age, user_name,gender, email, phone_number)
values
       (20,'wdd','male','wdd@qq.cm','123456'),
       (18,'cxc','male','cxc@163.com','13911'),
       (12,'die','male','die@qq.com','234325');

select * from user_info;

delete from user_info where user_id=3;

insert into user_info (age, user_name,gender, email, phone_number)
values
       (12,'die','male','die@qq.com','234325'),
       (18,'fangfang','female','fang@qq.com','123');

update user_info set user_name='fang' where user_id=5;


#DQL
select user_id,user_name from user_info;

select user_name,email from user_info where user_id=4;

select user_id,email from user_info where user_info.user_name like '%c';

update user_info set phone_number=null where user_id=5;
select * from user_info;

select user_name,age,gender from user_info where phone_number is null ;

select user_name, email from user_info where age=18 and user_info.gender='male';

select gender,count(*) as count_num from user_info group by gender;

# DQL high level
## union

select * from user_info where user_info.gender='female'
union
select * from user_info where user_info.user_name like '%c';

drop table if exists fav_thing;
create table fav_thing (
    fav_id tinyint(4) primary key auto_increment,
    fav_thing varchar(150),
    fav_user_id smallint(5) not null,
    constraint fk_fav_user foreign key fav_thing(fav_user_id) references user_info(user_id)
);

describe fav_thing;

insert into fav_thing (fav_thing, fav_user_id)
VALUES
       ('reading',1),
       ('fighting',5);

select * from fav_thing;

## inner join
select * from user_info
inner join
    fav_thing ft on user_info.user_id = ft.fav_user_id;

select * from user_info
inner join fav_thing;

select * from fav_thing
inner join user_info;

## left outer join
## 查询所有的user对应的fav_thing
select * from user_info
left join
    fav_thing ft on user_info.user_id = ft.fav_user_id;

select * from fav_thing
left join user_info ui on ui.user_id = fav_thing.fav_user_id;

select * from user_info
cross join
    fav_thing ft on user_info.user_id = ft.fav_user_id;

select * from fav_thing
cross join user_info ui on ui.user_id = fav_thing.fav_user_id;

## full outer join
select * from user_info
full join fav_thing ft on full.user_id = ft.fav_user_id;

select * from fav_thing
full join user_info ui on ui.user_id = full.fav_user_id;

