97 lines
2.2 KiB
SQL
97 lines
2.2 KiB
SQL
show DATABASES;
|
||
|
||
drop database nengdie;
|
||
|
||
create database nengdie;
|
||
|
||
show CREATE DATABASE nengdie;
|
||
|
||
use nengdie;
|
||
|
||
# can not rename database
|
||
# alter nengdie rename nengdie woshinibaba;
|
||
|
||
# delete a table
|
||
drop table user_info;
|
||
|
||
# create a table
|
||
create table user_info (
|
||
user_id TINYINT(3) PRIMARY KEY,
|
||
user_name VARCHAR(30) not null,
|
||
email_address VARCHAR(50),
|
||
age SMALLINT(4) not null,
|
||
gender VARCHAR(5) DEFAULT 'male'
|
||
);
|
||
|
||
# read all tables
|
||
show tables;
|
||
|
||
# read a table
|
||
describe user_info;
|
||
show create table user_info;
|
||
|
||
# modify a table
|
||
|
||
## change table name
|
||
alter table user_info rename user_information;
|
||
show tables;
|
||
alter table nengdie.user_information rename user_info;
|
||
show tables;
|
||
|
||
## add a column
|
||
alter table user_info add column favoriteID tinyint after age;
|
||
describe user_info;
|
||
|
||
## change column position
|
||
alter table user_info modify favoriteID smallint after gender;
|
||
|
||
## change column name
|
||
alter table user_info change favoriteID fav_id tinyint;
|
||
|
||
## modify column data format
|
||
alter table user_info modify gender varchar(10);
|
||
|
||
## delete a column
|
||
alter table user_info drop fav_id;
|
||
alter table user_info add fav_id tinyint after age;
|
||
|
||
## modify column CONSTRAINTS
|
||
alter table user_info modify user_id tinyint auto_increment;
|
||
|
||
# DML insert records into a table user_info
|
||
insert into user_info( nengdie.user_info.user_name, nengdie.user_info.email, nengdie.user_info.age, nengdie.user_info.gender)
|
||
values
|
||
('wangdada','zeaslity@qq.com',15,'male'),
|
||
('cxc','cxc@163.com',12,'male'),
|
||
('nengdie','nengdie@qq.com',30,'male'),
|
||
('hong','hong@qq.com',25,'female'),
|
||
('zeaslity','zeaslity@qq.com',18,'male'),
|
||
('zey','zety@qq.com',18,'male'),
|
||
('die','die@qq.com',15,'male');
|
||
|
||
SELECT * FROM user_info;
|
||
|
||
describe user_info;
|
||
describe fav_thing;
|
||
|
||
## 内连接,只输出符合限定条件的内容
|
||
select user_name,age,gender from user_info
|
||
inner join
|
||
fav_thing as t on user_id = t.fav_user_id
|
||
order by age;
|
||
|
||
## left outer join 左外连接 讲用户的喜好表 fav_thing与user表连接,查看用户对应喜好的事情
|
||
select * from user_info
|
||
left join
|
||
fav_thing as t on user_id =t.fav_user_id
|
||
order by age;
|
||
|
||
## 全外连接
|
||
select *
|
||
from user_info
|
||
full join fav_thing;
|
||
|
||
# 交叉连接
|
||
select * from user_info
|
||
cross join fav_thing;
|