Files
shell-scripts/SQL学习/DDL操作集合.sql
2023-05-15 16:49:09 +08:00

97 lines
2.2 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;