MYSQL基础教程

数据库相关语法

if not exsitsif exists 表示判断是否存在此表,可加可不加
创建数据库语法: create database [if not exists] 数据库名;

删除数据库语法:drop database [if exists] 数据库名;

查看数据库:show databases;

使用数据库:use 数据库名;

修改表名:alter table 旧表名 rename as 新表名;

表中添加字段:alter table 表名 add 字段名 字段类型;

删除字段: alter table 表名 drop 字段名;

结构化查询语句分类

DDL语句:GREATE,ALTER,DROP

DML语句:INSERT,DELETE,UPDATE

DQL语句:SELECT

使用SQL语句创建数据库

create table if not exists 表名(

     列名1  数据类型  约束,

     列名2  数据类型  约束,

     ....

     列名n  数据类型  约束

);

DML语句

添加单行语句语法:insert into 表名(列名1列名2…列名n) values(值1,值2…值n);

添加多行语句语法:insert into 表名(列名1列名2…列名n) values(值1,值2…值n),(值1,值2…值n)…(值1,值2…值n);

删除表中数据语法: delete from 表名 where 条件;

清空表中数据语法: truncate 表名;

删除表语法:drop table 表名;

修改表中数据语法::update 表名 set 列名 = 修改数据 where 条件;

修改表中多行数据语法::update 表名 set 列名 = 修改数据1, set 列名 = 修改数据2… set 列名n = 修改数据n where 条件;

DQL语句 SELECT

查询的基本与法结构:select 列名1,列名2…列名n from 表名 where 条件;

常用的关键字:

1、between 值1 and 值2:表示所查询的内容在值1和值2之间。

用法:
select 列名1,列名2…列名n from 表名 where between 值1 and 值2;

2、and: 条件1 and 条件2 所查询内容同时满足条件一与条件二。

用法:
select 列名1,列名2…列名n from 表名 where 条件1 and 条件2;

3、or : 条件1 or 条件2 所查询内容满足条件1与条件2任意一个即可。

用法:
select 列名1,列名2…列名n from 表名 where 条件1 or 条件2;

4、distinct: distinct 列名1,列名2…列名n;去除所选列中重复的部分。

用法:
select distinct 列名1,列名2…列名n from 表名 where between 值1 and 值2;

5、as: 给数据库或者列起别名

用法:
select 别名.列名,别名.列名 from 表名 AS 别名;
select 列名1 AS 别名1,列名2 AS 别名2 from 表名;

6、like模糊查询
与“%”一起使用,表示匹配0或任意多个字符。
与“_”一起使用,表示匹配单个字符。

%用法:
查询班中姓王学生的所有信息。
select * from student where studentName = ‘王%’;
查询班中名字中带有王字的学生的所有信息。
select * from student where studentName = ‘%王%’;
查询班中以王字结尾的学生的所有信息。
select * from student where studentName = ‘%王’;

_用法
查询班中两个字姓名姓王的学生的信息。
select * from student where studentName = ‘王_’;
查询班中三个字姓名姓王的学生的信息。
select * from student where studentName = ‘王__’;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值