数据库的基本用法

1.创建数据库

create database 数据库名字 ;

设置字符集和排序规则

default character set utf8mb4;

2.使用数据库

use 数据库名;

3. 删除数据库

drop database if exists 数据库名;

4.删除表

drop table if exists 表名;

5.创建表

use 数据库名;

create table 表名

(

stu_id int primary key auto_increment,
stu_name varchar(20) not null,
stu_age int not null,
stu_gender varchar(1) not null,
stu_address varchar(255)

);

6.修改表

添加一条属性

alter table student add COLUMN stu_phone varchar(11);

删除属性

alter table student drop column stu_phone;

7. 添加表内属性

单行插入

insert into 表名(列名,列名,列名) values(值,值,值);

多行插入

insert into 表名(列名,列名,列名) values(值,值,值),(值,值,值),(值,值,值);

8. update 语法

update 表名 set 列=值,列=值,列=值 where 加条件

9. delete 语句

delete from 表名 where 条件;

清空表(效率高)

truncate table 表名;

10. 基本查询

所有的行和列

select*from 加表名

部分的列

select 列名,列名 from 表名

给列设置别名

select 列名 姓名,列名 年龄 from 表名;

单条件查 select * from student where stu_age > 25;

多条件查

条件1 OR 条件2

select * from student where stu_address = '武汉' OR stu_address = '上海';

条件1 AND 条件2

select * from student where stu_age >= 25 and stu_age <= 27;

IN(值,值...)

select * from student where stu_address in ('武汉','上海');

between 值1 and 值2

select * from student where stu_age between 25 and 27;

对条件取反

NOT 条件

模糊查询

-- 查询姓张的学生

select * from student where stu_name like '张%'; -- 查询地址中有海字的 select * from student where stu_address like '%海%';

空值查询

-- 查找地址为null的

select * from student where stu_address is null; -- 查找地址不为null的 select * from student where stu_address is not null;

排序

order by 列

升序 asc 默认

降序 desc

-- 按年龄排序

select * from student order by stu_age asc; select * from student order by stu_age desc; select * from student where stu_gender = '男' order by stu_age; -- 多列排序 select * from student order by stu_age,stu_id desc;

分页查询

对返回结果的行数进行限制

limit 开始位置, 长度

limit 长度

-- 年龄最大的2个学生

select * from student order by stu_age desc limit 2; -- 分页查询学生,每次显示5个学生 select * from student limit 0,5; select * from student limit 5,5; select * from student limit 10,5; select * from student limit 15,5;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值