mysql数据库学习(二)--表操作

一、表操作

以下内容都是自己学习的时候看过的一些知识,作为笔记记录一下吧,大部分都是所看文章的内容。

1.创建表

前面的基础篇笔记是相当于搭建了一个方便管理的文件夹树根,下面要学习的是一些关于表的知识,相当于树上的叶子吧!

create table 表名(
列名 类型 是否可以为空,
列名 类型 是否可以为空
)ENGINE=InnoDB DEFAULT CHARSET=utf8; 下面这两个变量是:
ENGINE :存储引擎
charset:字符集
        是否可空,null表示空,非字符串
not null - 不可空
null - 可空

是否可以为空

        默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值
create table tb1(
nid int not null defalut 2,
num int not null
)

默认值

自增,如果为某列设置自增列,插入数据时无需设置此列,默认将自增(表中只能有一个自增列)
create table tb1(
nid int not null auto_increment primary key,
num int null
)

create table tb1(
nid int not null auto_increment,
num int null,
index(nid)
)
注意:1、对于自增列,必须是索引(含主键)。
2、对于自增可以设置步长和起始值
show session variables like 'auto_inc%';
set session auto_increment_increment=2;
set session auto_increment_offset=10; shwo global variables like 'auto_inc%';
set global auto_increment_increment=2;
set global auto_increment_offset=10;

自增

        主键,一种特殊的唯一索引,不允许有空值,如果主键使用单个列,则它的值必须唯一,如果是多列,则其组合必须唯一。
create table tb1(
nid int not null auto_increment primary key,
num int null
)

create table tb1(
nid int not null,
num int not null,
primary key(nid,num)
)

主键

        外键,一个特殊的索引,只能是指定内容
creat table color(
nid int not null primary key,
name char(16) not null
) create table fruit(
nid int not null primary key,
smt char(32) null ,
color_id int not null,
constraint fk_cc foreign key (color_id) references color(nid)
)

外键

2.删除表

drop table 表名

3.清空表

# 表还存在,表内容清空

delete from 表名   速度慢,能回滚。
truncate table 表名 速度快,不能回滚。

4.修改表

添加列:alter table 表名 add 列名 类型
删除列:alter table 表名 drop column 列名
修改列:
alter table 表名 modify column 列名 类型; -- 类型
alter table 表名 change 原列名 新列名 类型; -- 列名,类型 添加主键:
alter table 表名 add primary key(列名);
删除主键:
alter table 表名 drop primary key;
alter table 表名 modify 列名 int, drop primary key; 添加外键:alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);
删除外键:alter table 表名 drop foreign key 外键名称 修改默认值:ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;
删除默认值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;

实例:

show tables; #展示当前库下的所有表名
SELECT * from user_info; #查看表的信息
alter table user_info add class int(4) #为表添加一新的一列,默认添加到最后面。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbIAAACdCAIAAABenGocAAAVmElEQVR4nO2dPWsbSxuG9yed4hhi/ZF0cRHY/5A64CaFtn+btwzuAkZFMAEXAZ1CRkQYYjYYFzmJjcGYEDDhLeYttB/zuTtaz8czo/vChbwfmnnunbk1s7PSUzxG4urqipHk6uoqliYSqAnYif+9eLEPfwGULK7iEdsA9UQUBIDnEN2wwvwFULKI7UIAADdEN6wwfwGUhC0CkAkhjSM8sEUAwM7AFl1R/BcAkAWdccSuiBdCRmccLX779u3q/k8Gf9++fQvw8bIrcWtFUxNSp*p5KLoqBCpOYRMMWvQJbJE6KEsEWXQFbjANskTgpSgRbdAVsMQ6wReKkKBFs0RWwxTjAFomTokSwRVfAFuPQ16qeHxaH81o5YlFqNzsundXzw6JcKEeYaiVgUUWfUXjFS7PxrMaOtrgodVc+LrBF2CJjDLZIFNhiFGCLsMVB4tqiEf542OKOwBbHgC3CFgeBLUYFthiFhGzxw8vixZu3r4uiKIrir7eXzfZ/3v1VNLx8zx35vtn+8v2fq/fKWfcfXrZncRsD2WI9P2wL7xrEomw3bVtsPT/s9i7KwlPLESfRmsr0FfJaelN8X25Tlb5W3AGHh4dC3RZlcTifl+KZ5ihk9cWJOjEDFftnF9HhfN7Vk1embzqyGMZrKje85zNmi1KJnC3qaq5u08fnlLRssShefWit8MWbf/5c3V++efXu4/aA96+L4vV/uiP/fvfxvjXE/qzugO3pf67uL9/83flpEFvUDIz4vtjvbraGGa8JxYoGGcoWC37811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GCLAAAgAFsEAAAB2CIAAAjAFgEAQAC2CAAAArBFAAAQgC0CAIAAbBEAAARgiwAAIABbBAAAAdgiAAAIwBYBAEAAtggAAAKwRQAAEIAtAgCAwP8Bv5NLqHWKf3QAAAAASUVORK5CYII=" alt="" />

--插入指定列的后面。
mysql> alter table user_info add firstname int(4) after name; --插入名为qq列在第一。
mysql> alter table user_info add qq varchar(15) first;

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAocAAACYCAIAAAAZadTSAAAcLUlEQVR4nO2dz2vkOrbH64+awE39I7ObLAb8P8z6QTZvkdq/zSwv2Q2EWjyKC1k01CZN0UVDNw4hi3tvJwyEogkUw1v4LfxLkiVbdqTjc+TvhywqKrusc3SOvpYs26ui5nQ6nU6n79+/n3jw/fv3giVjXfR/v/yCP+tfpMgBAAC5rBqxKf+HKg8CVYYqAwBAJFaq2HBjRuntYawVs4sf278YMQMAAKLRVHlGqUuYRoTmrggX4BAAAHAxoMqzD6c++EfoSSesKsMBOAQAAFxAlaPDqjIcgEMAAMCFXZX/WTO7rH7w758MYFUZDsAhAADgwqnK5YeeYc3j4+P3f/+Hyd/j46NaN1ajsVGVMQxJkv6goq/PIPPWiqdPWJGGi8RZIa7CUoAqRweqbABVFnR0EaThInFWiKuwFKDK0YEqG0CVBR1dBGm4SJwV4iosBahydKDKBlBlQUcXQRouEmeFuApLAaocHaiyAVRZ0NFFkIaLxFkhrsJSgCpH5wOqnN9crlarVXZzc7m6vMkjVZAawaqcOxpim8VrH8Un+c3lKtt2tnDVSsOjijGtiEqUsCH3Rq8V28zW8vPCM1sTAKocncmq3PbBXt2uGKDKE48OVXYAVZ4FntmaAFDl6ExW5UndgqPj5oRgVXYxryo7UbeHKo8EqjwEz2xNAKhydKDKBlDlqUeHKtuBKs8Cz2xNgFCq/K+/rhT+8t//O1AeT5W32eqyqXabVtUl2tVqpfdT26o82xbFNis3UJKxLlpNz9BpqtweeJVt9bnsbLvN6grpZik7Xd7kpYE3HaPmc8Wg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EmeFuApLwanKjx6cAAAALJU5NCt9po+VAQAAABAWqDIAAADABagyAAAAwAWoMgAAAMAFqDIAAADABagyAAAAwAWoMgAAAMAFqDIAAADABagyAAAAwAWoMgAAAMAFqDIAAADABagyAAAAwAWoMgAAAMCF/wetEPTXqS4aNAAAAABJRU5ErkJggg==" alt="" />

#更改表名字,rename法。
--语法: rename table 原表名 to 新表名;
--更改user_infor表为user。
mysql> rename table user_info to user; #删除表
--语法:drop table <表名>;
--删除表名为user表。
drop table user;

二、表内容操作

1、增

语法:insert into 表 (列名,列名...) values (值,值,值...)

mysql数据库学习(二)--表操作
# 插入单条数据
insert into 表 (列名,列名...) values (值,值,值...)
# 插入多条数据
insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...)
# 插入另一条语句的查询结果
insert into 表 (列名,列名...) select 列名,列名... from 表
mysql数据库学习(二)--表操作

2、删

语法:delete from 表

delete from 表;
delete from 表 where id=1;

3、改

语法:update 表 set name = 'nick' where id>1

update 表 set name = 'nick' where id>1

4、查

语法:select * from 表

select * from 表
select * from 表 where id > 1
select nid,name,gender as gg from 表 where id > 1 # as 做别名

5、条件

语法:select * from 表 where id > 1

    select * from 表 where id > 1 and name != 'nick' and num = 12;    # 多个条件
select * from 表 where id between 5 and 16; # id在5到16之间
select * from 表 where id in (11,22,33); # id在元祖中
select * from 表 where id not in (11,22,33); # id不在元祖中
select * from 表 where id in (select nid from 表); # id在查询结果中

6、通配符

语法:select * from 表 where name like '_n%'

    select * from 表 where name like 'ni%'  # ni开头的所有(多个字符串)
select * from 表 where name like 's_' # s开头的所有(一个字符)

7、限制

语法:select * from 表 limit 9,5;

    select * from 表 limit 5;            # 前5行
select * from 表 limit 9,5; # 从第9行开始的5行
select * from 表 limit 5 offset 9 # 从第9行开始的5行

8、排序

语法:select * from 表 order by 列1 desc,列2 asc

    select * from 表 order by 列 asc             # 根据 “列” 从小到大排列
select * from 表 order by 列 desc # 根据 “列” 从大到小排列
select * from 表 order by 列1 desc,列2 asc # 根据 “列1” 从大到小排列,如果相同则按列2从小到大排序

9、分组

语法:select num from 表 group by num

mysql数据库学习(二)--表操作
    select num from 表 group by num           # 根据num分组
select num,nid from 表 group by num,nid # 根据num和nid分组
select num,nid from 表 where nid > 10 group by num,nid order nid desc
select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid # 内置函数
select num from 表 group by num having max(id) > 10 # 前面计算的结果交由后面处理 注:group by 必须在where之后,order by之前
mysql数据库学习(二)--表操作
mysql数据库学习(二)--表操作
count(*)、count(1) # 表示个数
sum(score)   # 表示和
max(score)   # 表示最大数
min(score)   # 表示最小数 having # 要用前面处理结果是用having。
mysql数据库学习(二)--表操作

10、连表

语法:inner join . onleft join . onright join . on

mysql数据库学习(二)--表操作
    无对应关系则不显示
select A.num, A.name, B.name
from A,B
Where A.nid = B.nid 无对应关系则不显示
select A.num, A.name, B.name
from A inner join B
on A.nid = B.nid A表所有显示,如果B中无对应关系,则值为null
select A.num, A.name, B.name
from A left join B
on A.nid = B.nid B表所有显示,如果B中无对应关系,则值为null
select A.num, A.name, B.name
from A right join B
on A.nid = B.nid
mysql数据库学习(二)--表操作

11、组合

语法:unionunion all

mysql数据库学习(二)--表操作
    组合,自动处理重合
select nickname
from A
union
select name
from B 组合,不处理重合
select nickname
from A
union all
select name
from B
mysql数据库学习(二)--表操作
上一篇:Python自动化运维之12、面向对象进阶


下一篇:spo0lsv病毒分析