数据库(黑马)

数据库

复制代码
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在语句最后 #### ####

相关推荐
Arbori_2621515 分钟前
oracle update 原理
数据库·oracle
hxung1 小时前
Spring @Transactional 注解
数据库·spring·oracle
追逐时光者2 小时前
精选 4 款免费且实用的数据库管理工具,程序员必备!
数据库
生命有所坚持而生存可以随遇而安2 小时前
MySQL5.7数据库部署和安装
数据库·adb
未来的资深Java架构师2 小时前
MySQL报Lock wait timeout exceeded; try restarting transaction
数据库·mysql
kikyo哎哟喂2 小时前
MongoDB常见面试题总结(上)
数据库·mongodb
孙同学_2 小时前
【MySQL】002.MySQL数据库基础
数据库·mysql
码农101号3 小时前
数据库视图讲解(view)
数据库·sql
喝醉酒的小白3 小时前
SQL Server:数据库镜像端点检查
数据库
Acxymy3 小时前
SQL学习笔记七
笔记·sql·学习