数据库
use test2;
show tables;
create table student2(
id int,
name varchar(10),
sex varchar(10)
);
ALTER TABLE student ADD sex int;
INSERT INTO student (sex) VALUES ('1');
insert into student(id) values(1),(2),(3);
insert into student2 values(4,'呆呆1',19),(5,'hedfdshe',18),(6,'pdd',13);
delete from student where id=3;
update student set name='yuanshen' where id=5;
select id,name,age from student ;
select * from student where id=6 ;
select sex,avg(id),sum(id) from student2 group by sex ;
select * from student2 where sex=18 order by id desc ;
select * from student2 limit 2,3;
select sex,count(*) from student2 where sex > 10 group by sex order by sex ;
DDL
层级关系 库>>表>>数据
注释 -- 我是注释 # 我是注释 /* 多行注释 */
库操作
显示有哪些数据库 SHOW DATABASES;
使用数据库 USE 数据库名称;
创建数据库 CREATE DATABASE 数据库名称 [CHARSET UTS-8];
删除数据库 DROP DATABASE 数据库名称;
查看当前使用的数据库SELECT DATABASE();
表操作
首先使用库,才能以此为基础操作表
use 库名;
显示表 show tables;
创建表
create table student1(
id int,
name varchar(10),
age int
);
删除表 drop table student;
DML数据操作
where 条件判断\]; 可写可不写,不写默认全部 字符用单引号引起来 插入 语法 INSERT 表\[列1,列2,。。。\] VALUES(值1,值2,。。。) insert **into** student (id,name,age) **values**(5,'呆呆',19),(6,'hehe',18) 删除 语法 DELETE FROM 表名称 \[WHERE 条件判断\]; = \< \> \<= \>= != **delete** **from** student **where** id=3; delete from student;删除整张表的内容 更新 语法 UPDATE 表名 SET 列=值 \[WHILE 条件判断\]; ##### DQL 数据查询 select 字段列表\|\* from 表 查询所有列 select id,name,age from student ; select \* from student ; 查询指定列 select \* **from** student **where** id=6 ; ##### 分组聚合 sum求和 avg求平均值 min最小值 max最大值 count求数量 select 字段\|聚合函数 from 表 \[where 条件\] group by 列; 可以写多个聚合函数 select gender,avg(age) from student group by gender; ##### 排序和分页 对查询到的结果进行排序(按照id,以asc升序排列) **select** \* **from** student2 **where** sex=18 **order** **by** id **asc** ; 分页 对查询的结果限制只输出从第2行以后开始,往后输出3行 **select** \* **from** student2 **limit** 2,3; limit在语句最后 #### ####