网络安全之数据库基础篇(基础入门)-二,操作数据表

时间:2024-04-30 07:08:44
1,查看当前库中有那些表

show tables;

2,查看表的结构

desc user;

3,查看数据库中表的字符集

show table status from db1 like 'user';

4,修改表的字符集

alter table user character set utf8;

5,修改表,列的数据类型

格式:

alter table 表名 modify 列名 修改为的类型

例如:

alter table user modify score float;

6,修改列名

格式:

alter table 表名 chname 要修改的列名 修改为的列名 数据类型;

例如:

alter table user chname id num int;

7,删除表中的一列

格式:

alter table 表名 drop 列名;

例如:

alter table user drop name;

8,创建数据表

格式:

create table 表名(

列名 数据类型 约束,

列名 数据类型 约束,

....

列名 数据类型 约束

);

例如:

create table user(

id int primaty key, //id列为int类型和主键约束(唯一)

name varchar(20), //name列字符类型

age int,

gender varchar(20)

);

9,修改表名

格式:

alter table 旧表名 rename to 新表名;

例如:

alter table user rename to student //将user表名字改为student

10,为表添加一列

格式:

alter table 表名 add 要添加的列名 数据类型;

例如:

alter table user add score int;

11,为表格新增数据

格式1(全部添加):

insert into 表名 values(值1,值2,....);

例如:

insert into user values(1"goud",19,"male");

要注意的是列名要与values中的值一一对应

格式2(指定列添加数据):

insert into 表名(列名1,列名2....) values(值1,值2);

例如:

insert into user(age,gender) values(19,"male");

格式3(批量添加数据):

insert into 表名 values(值1,值2,...),(值1,值2,...);

例如:

insert into user values(1,"goud",19,"male"),(2,"lis",18,"famale");

12,修改表中的数据

格式:

update 表名 set 修改为的值1,修改为的值2,... where 条件;

例如:

update user set age=99 where name='lis';

update user set id=5,age=100 where name="goud";

13,删除表中的数据

格式:

delete from 表名 where 条件;

例如删除名字是lis这一行:

delete from user where name='lis';

14,查询表中的数据

格式1(查询所有列):

select * from 表名;

例如:

select * from user;

格式2(查询指定列):

select 列名1,列名2,... from 表名;

例如:

select id,name from user;

格式3(去重查询):

select distinct 列名1,列名2,... from 表名;

例如:

select distinct name from user;

格式4(筛选的同时计算列的值):

select name,score+10 from user;

表中的内容并不会加10,只是显示出来的值加了10

格式5(如果内容是null):

select name,ifnull(score,0)+10 from user;

如果score的内容是null就利用ifnull 将其转换为0再进行运算

15,条件查询

格式:

select 列名 from 表名 where 条件;

例如(查询name是lisi的全部数据):

select * from user where name='lisi';

格式2(and):

select 列名 from 表名 where 条件1 and 条件2;

例如(查询age是30到40之间的name):

select name from user where age>30 and age<40;

格式3(or):

select 列名 from 表名 where 条件1 or 条件2;

例如(查询age大于30或age小于10的name):

select name from user where age>30 or age<10;

格式4(in):

select 列名 form 表名 where 列名 in(要满足的数据);

例如(查询age是in中数据的name):

select name from user where age in(10,20,30);

格式5(模糊查询):

‘-’:代表一个任意字符

select * from user where name like '小_';

格式6(模糊查询):

‘%’:代表任意字符

select * from user where name like '%红%' //查询出name有红的所有数据

16,聚合函数查询:

格式:

select 函数名(列名) from user [where 条件];

例如:

select count(*) from user; #user表中有多少列

select max(score) from user; #求user表中score的最大值

select min(score) from user; #求user表中score的最小值

select sum(score) from user; #求user表中score的总数

select avg(score) from user; #求user表中score的平均值

select sum(id) from user where score=100; #求user表中score等于100的id总数

17,排序查询:

格式:

select 列名 from 表名 [where 条件] order by 列名1 排序方式1,列名2 排序方式2;

例如(asc降序排序):

select * from user order by score asc; #user表中的score降序排序

例如(desc升序排序):

select * from user where age<20 order by score desc; #user表中的score升序排序

例如(desc和asc联合使用):

select * from user order by score asc,id desc; #user表中的score降序排如果相同就按id升序排序

18,分组查询

格式:

select 列名

from 表名 [where 条件]

group by 要分组的列名

[having 过滤出来的条件]

[order by 排序列名 排序方式];

1,例如:

select class,sum(score)

from user

group by class; //按班级分组,算出每组总分

2,例如:

select class,sum(score)

from user

where score>80

group by class; //通过班级分组,筛选出成绩大于80的人的总分。

3,例如

select class,sum(score) total

from user

where score>80

group by class

having total>800

//对计算成绩大于80的人的总分并按班级分组筛选大于800的class(total是别名)

4,例如

select class,sum(score) total

from user

where score>80

group by class

having total>800

order by total desc; #降序排序

//对计算成绩大于80的人的总分并按班级分组筛选出大于800的class(total是别名)

19,分页查询

格式:

select 列名 from 表名

[where 条件]

[group by 要分组的列名]

[having 要满足的条件]

[order by 要排序的列名 升或减序]

limit 当前的页数,显示的条数(当前的页数=(页数-1)*显示的条数)

例如:

select * from user limit 0,3; #第一页

select * from user limit 3,3; #第二页

20,主键约束

创建一个用户表(编号,姓名,年龄)编号设为主键

create table user(

id int primary key,

name varchar(30),

age int

);

alter table user drop primary key; //建表后删除主键

alter table user modify id int primary key; //建表后添加主键

21,主键自增约束

创建一个用户表(编号,姓名,年龄)编号设为主键自增

create table user(

id int primary key auto_increment,

name varchar(30),

age int

);

alter table user modify id int; //建表后删除主键自增约束

alter table user modify id int auto_increment; //建表后添加主键自增约束

22,唯一约束

创建一个用户表(编号,姓名,年龄)编号设为唯一

create table user(

id int unique,

name varchar(30),

age int

);

alter table user drop index id; //建表后删除唯一约束

alter table user modify id int unique; //建表后添加唯一约束

23,非空约束

创建一个用户表(编号,姓名,年龄)编号设为主键,并设name为非空

create table user(

id int primary key

name varchar(30) not null,

age int

);

alter table user modify nmae varchar(30); //建表后删除非空约束

alter table user modify name varchar(30) not null; //建表后添加非空约束

24,外键约束

格式:

constraint 外键名 foreign key (本表外键列名)references 主表名(主表主键名)

创建一个用户表(编号,姓名,年龄)编号设为主键

create table user(

id int primary key auto_increment,

name varchar(30) not null,

age int

);

创建一个订单表(id,编号,外键列)id设为主键,

create table orderlist(

id int primary key not auto_increment,

number varchar(30) not null,

uid int,

constraint out_k foreign key (uid) references user(id)

);

alter table orderlist drop foreign key out_k; //建表后删除外键约束

alter table orderlist add constraint out_k foreign key (uid) references user(id); //建表后添加外键约束

25,外键级联

格式1(添加级联更新):

alter table 表名 add constraint 外键名 foreign key (本表外键列名) references 主表名(主键列名)

on update cascade;

格式2(添加级联删除):

alter table 表名 add constraint 外键名 foreign key (本表外键列名) references 主表名 (主键列名)

on delete cascade;

格式3(同时添加级联更新和级联删除):

alter table 表名 add constraint 外键名 foreign key (本表外键列名) references 主表名 (主键列名)

on update cascade on delete cascade;

例如:

alter table orderlist add constraint out_k foreign key (uid) references user(id)

on update cascade on delete cascade;

26,表关系一对一

用户表

create table user( id int primary key auto_increment, NAME varchar(30), age int );

订单表

create table orderlist( id int primary key auto_increment, product varchar(20), uid int, constraint out_key foreign key (uid) references user(id) );

27,表关系一对多

用户表--订单表--价格表

用户表

create table user( id int primary key auto_increment, NAME varchar(30), age int );

订单表

create table orderlist( id int primary key auto_increment, product varchar(20), uid int, constraint out_key foreign key (uid) references user(id) );

价格表

create table price( id int primary key auto_increment, money varchar(20), pid int, constraint out_price foreign key (pid) references orderlist(id) );

28,表关系多对多

创建三个表一student,course,center

student表:

create table student(
id int primary key auto_increment,
NAME varchar(20),
class varchar(20)
);

course表:

create table course(
id int primary key auto_increment,
book varchar(20)
)

center表:

create table center(
id int primary key auto_increment,
studentid int,
courseid int,
constraint out_kk1 foreign key (studentid) references student(id),
constraint out_kk2 foreign key (courseid) references course(id)
);

student表中插入数据:

insert into student values(1,'zhansan','100'),(2,'lis','150'),(3,'wanwu','200')

course表中的数据:

insert into course values(1,'math'),(2,'chinese'),(3,'physics')

center表中插入数据:

insert into center(studentid,courseid) values(1,1),(1,2),(2,1),(3,2)

查看lis选了什么书
SELECT c.book  
FROM student s  
JOIN center ct ON s.id = ct.studentid  
JOIN course c ON ct.courseid = c.id  
WHERE s.NAME = 'lis';

29,内连接查询

这里就用上面哪个表了哈,查看学生信息对应的课程信息

select * from student inner join course on student.id=course.id

30,外连接查询

【1】左外连接(left outer join):返回左表的全部记录和右表满足条件的记录

例如查询学生所对应的课程:
SELECT
student.*,
course.book
FROM
student 
LEFT OUTER JOIN
course 
ON
course.id=student.id;

【2】右外连接(right outer join):返回右表的全部记录和左表满足条件的记录(这里不做演示了,一样的格式)

【3】全外连接(right outer join):返回两表的全部记录,没有匹配的项用null补齐

31,多表子查询

用户表

create table user( id int primary key auto_increment, NAME varchar(30), age int );

订单表

create table orderlist( id int primary key auto_increment, product varchar(20), uid int, constraint out_key foreign key (uid) references user(id) );

查看张三和李四的订单信息

select  * from orderlist where uid in (1,2)

select id from user where name in ('张三','李四');

select * from orderlist where uid in(select id from user where name in ('张 三','李四'))