SQL语句有以下几点需要注意的:

1.SQL语句可以单行,或者多行书写,重要的是以分号;结尾
2.MySQL是不区分大小写的,一般关键字大写,自己定义的一些表名之类的用小写
3.注释方法有三种:
(1)单行注释:-- 特别注意:在--后面要先加上一个空格,再输入注释的语句
(2)单行注释:#这种注释方法在#的后面就不用加空格,直接输入注释的语句
(3)多行注释:/* 注释 */

对于SQL的语句主要有四种分类:

1.DDL:数据定义语言

用来定义数据库对象:数据库,表,列等
关键字:

create,drop,alter

2.DML:数据操作语言

对数据库中的表进行 增删改
关键字:

insert,delete,update

3.DQL:数据查询语言

查询数据库中表的记录
关键字:

select,where

4.DCL:数据控制语言

定义数据库访问权限和安全级别、创建用户
关键字:

GRANT,REVOKE

DDL

1.操作数据库
(1)create 创建
创建数据库db1,默认字符集是utf-8

create database db1;

判断是否存在同名数据库(和db1同名):如果存在,不创建;不存在则创建。

create database if not exists db1;

设定数据库的字符集为gbk

create database db3 character set gbk;

(2)retrieve 查询
查看所有数据库的名称:

show databases;

查看mysql数据库的字符集(mysql数据库的创建语句):

show create database mysql;

(3)update 修改
修改数据库的字符集

alter database 数据库名称 character set 字符集名称;

(4)delete 删除
删除数据库

drop database 数据库名称;

判断数据库是否存在,存在则删除。

drop database if exists 数据库名称;

(5)使用数据库
查询当前正在使用的数据库

select database();

使用数据库

use 数据库名称;

2.操作表
(1)create 创建

create table 表名(
列名1 数据类型1,
列名2 数据类型2,
...
列名n 数据类型n
);

*注意最后一列不要加逗号

数据库的数据类型

图片说明
例子如下:
图片说明

create table student(
id int,
name varchar(32),
age int,
score double(4,1),
birthday date,
insert_time timestamp
);

(2)retrieve 查询
查询某个数据库中所有表的名称

show tables;

查询表结构

desc 表名;

(3)update 修改
修改表名

alter table 表名 rename to 新的表名;

修改表的字符集

show create table student;
alter table student character set utf8;

添加一列

alter table 表名 add 列名 数据类型;

修改列名称 类型

alter table stu(表名) change gender(列名) sex(新列名) varchar(20)(新数据类型);
alter table stu modify sex varchar(10)

删除列

alter table stu(表名) drop sex(列名)

(4)delete 删除

drop table if exists 表名;

(5)表的复制

create table stu(表名) like students(基表);

DML

1.添加数据
insert into 表名(列名)values(对应数值)

insert into stu(id,name,age) values (1,'张无忌',18);

*注意:列名和值要一一对应
列名可省略
除了数字类型都要加引号
2.删除数据
delete from 表名 where 条件;

delete from stu where id=1;

删除表,然后在创建一个一摸一样的空表

truncate table 表名;

3.修改数据
updata 表名 set 列名1=值1,...where 条件

updata stu set age=117 where id=3;

如果不加条件,修改所有内容

DQL

select * from 表名

select 字段列表 from 表名列 表 where 条件列表 group by 分组字段 having 分组之后的条件 oreder by 排序 limit 分页限定
2.基础查询
去除重复的结果集

select distinct address from student;

计算math和english之和
ifnull为空则为0
as 总分 起别名

select name,math,english,math+ifnull(english,0) as 总分 from student;

3.条件查询
1.where语句后跟条件
2.运算符

,<,<=,>=,=,<>
between and
in(集合)
like
is null
and 且
or 或
not 非

select * from student where age>20;
select * from student where age<20;
select * from student where age>=20;
select * from student where age<=20;
select * from student where age=20;

select * from student where age!=20;
select * from student where age<>20;

select * from student where age>20 and age<=30;
select * from student where age between 20 and 30;

select * from student age=22 or age =25;
select * from student age in(22,25);

select * from student where english is null;

模糊查询like
1.占位符号
_:单个任意字符
%:多个任意字符

select * from student where name like '马%';
select * from student where name like '_化%';
select * from student where name like '___';(查询姓名三个字的)
select * from student where name like '%德%';