您好,欢迎来到华佗小知识。
搜索
您的当前位置:首页Linux重新学习

Linux重新学习

来源:华佗小知识

表操作

创建表

create table `表名`(
ID INT(10) unsigned not null auto_increment COMMENT'列名',
NAME VARCHAR(20) default null COMMENT'列名',
primary key (`ID`)
using BTREE)
ENGINE=INNODB auto_increment=5 default CHARSET=UTF8 
row_format = dynamic COMMENT='小说集';

 解释:

unsigned:无符号的,添加在这里代表序号是正整数,无法为负数

not null:不能为空

default null:默认为空

auto_increment:自增长,添加内容,id自动添加序号

comment:注释,列名

using BTREE:使用B树

engine=InnoDB:存储引擎

auto_increment=8:数据库自增到8,新纪录从9开始

default charset = utf8:默认编码集

row_format = dynamic:默认静态存储

举例:

DROP TABLE IF EXISTS `novels collection`;

create table `NOVELS COLLECTION`(
ID INT(10) unsigned not null auto_increment COMMENT'主键',
NAME VARCHAR(20) default null COMMENT'小说名',
primary key (`ID`)
using BTREE)
ENGINE=INNODB auto_increment=5 default CHARSET=UTF8 
row_format = dynamic COMMENT='小说集';

 数据操作

插入数据

insert into 表名(列名) values ('插入的数据1'),('插入的数据2')...

或者如果是自增长可以用null代替

修改表结构语法:

添加字段
alter table 表名 add column 列名 字段类型;
修改字段名
alter table 表名 change column 旧列名 新列名 新字段类型;
修改字段类型
alter table 表名 modify column 新列名 新字段类型;
删除字段
alter table 表名 drop column 列名;
修改表名
alter table 表名 rename to 新表名;

举例:

添加字段
alter table novels_collection add column build varchar(50);
修改字段名
alter table novels_collection change column build much int(10);
修改字段类型
alter table novels_collection modify column much bigint(10);
删除字段
alter table novels_collection drop column much;
修改表名
alter table a_novels_collection rename to aa_novels_collection;

修改数据

插入数据
insert into 表名(字段1,字段2...) values(字段值1,字段值2...)

删除数据
delete from 表名 where 字段名1=字段值1
delete from 表名 删除表里所有数据

查询数据
select * from 表名

更新数据
update 表名 set 字段名1=字段值1 where 字段名2=字段值2
update 表名 set 字段名1=字段值1 修改表里所有数据

举例:

insert into aa_novels_collection(much,build) values ('23','15');

delete from aa_novels_collection where much=23;

select * from aa_novels_collection;

update aa_novels_collection set much=23 where id=10;
update aa_novels_collection set much = 105;

 基本查询

基本查询
select * from 表名;

字段查询
select 字段名 from 表名;

条件查询
select * from 表名 where 条件;

排序
select * from 表名 order by 列名 desc(asc);
desc:降序
asc:升序

分页
select * from 表名 order limit 10 offset 3;
第10页看前三条数据

去重
select distinct gender from 表名;

举例:

添加一条数据
insert into a_NOVELS_COLLECTION(day,build) values ('2022-5-12','2012-10-15');
删除对应数据
delete from a_NOVELS_COLLECTION where much=23;
查询表格
select * from a_NOVELS_COLLECTION;
添加一条数据
update a_NOVELS_COLLECTION set build='2015/1/2' where id=10;
添加整行数据
update a_NOVELS_COLLECTION set day = '2012/10/20';
除了规定数据其他都更新
update a_NOVELS_COLLECTION set day = '2012/05/20' where ID !=10;

联查

内连接 inner join

语法:

select *
from 表1 a inner join 表2 b
on a.id=b.id

左连接 left join

语法:

select *
from 表1 a left join 表2 b
on a.id=b.id

右连接 right join

语法:

select *
from 表1 a right join 表2 b
on a.id=b.id

实战练习:

 新建Course表和Menu表

create table Course(
id int(10) unsigned not null auto_increment,
name varchar(50) default null,
primary key(id));


create table Menu(id int(10) unsigned not null auto_increment,
course_id int(10) not null,
name varchar(50) default null,
primary key(id));

添加数据:

insert into Course(name) values('自学测试'),
('接口测试'),
('UI测试'),
('前端开发'),
('后端开发');


insert into Menu(course_id,name) values(1,'shell脚本第一章'),
(6,'php开发第一章'),
(3,'appium实战第一章'),
(2,'mock实战第一章'),
(7,'产品原型设计第一章');
左连接
select c.id cid,c.name cname,m.name from Course c 
left join Menu m on c.id=m.course_id;

右连接
select m.id,m.name,c.name from Menu m 
right join Course c on m.course_id=c.id;

内连接
select c.id,c.name,m.name from Course c 
inner join 	Menu m on c.id=m.course_id;

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo0.cn 版权所有 湘ICP备2023017654号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务