温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL 基本语句

发布时间:2020-06-04 12:43:04 来源:网络 阅读:580 作者:tank_99 栏目:数据库

MySQL 基本语句


一.数据定义语言(DDL)

二.数据操作语言(DML)

三.数据查询语言(DRL)

四.事务控制语言(TCL)



一.数据定义语言(DDL:Data Definition Language)

(修改表、库结构。 如create、drop、alter)


1.创建数据库(create database语句) create database 库名

2.删除数据库(drop database语句) drop database 库名

3.创建表(create table语句) create table 表名

4.删除表(drop table语句)  drop table 表名 

5.修改表结构(alter table语句)


(1).修改表名 

语法1. alter table 表名 rename 新表名

例:alter table student rename stu;

语法2. rename table 表名 to 新表名

例:rename table student to stu;


(2).添加列 

alter table 表名 add 列名 类型;

例:alter TABLE student ADD sage int;


(3).删除列

alter table 表名 drop 列名

例:alter table student drop sname;


(4).修改列类型

语法1. alter table 表名 modify 列名 目标类型

例:alter table student modify sid varchar(10);

语法2. alter table 表名 change 列名 列名 目标类型

alter table student change address address char(50);


(5).同时修改列名和列类型

alter table 表名 change 列名 新列名 目标类型

alter table student change address add char(40);



注意:

1. 数据库不区分大小写

2. desc 表名 查看该表的结构

3. drop table语句会删除该表的所有记录及表结构

4. 新增主键:alter table (表名) add primary key (字段名)


二.数据操作语言(DML:Data Manipulation Language)

(对数据进行操作。如insert、update、delete)


1.添加数据(insert into...语句)

语法1. insert into 表名(列名1, 列名2, 列名3...)values(列名1值,列名2值, 列名3值.)

例:insert into stu(sid,sname,sage)values(1,’张三’,22);

语法1. insert into 表名 values(列名1值,列名2值, 列名3值.)

例:insert into stu values(1,’李四’,22);

注意:

1. 语法1可以有选择的添加某些列的值,但语法2需添加所有列且按顺序添加值。

2. 添加当前时间可以用now()函数。


2.修改数据(update...set语句)

update 表名 set 列名1=修改的值,列名2=修改的值 where 列名='值';

例:update stu set sage=23,sname='李五' where sid=1;

注意:where表示条件,如果没有where的话将修改所有数据。


3.删除数据(delete from...语句)

(1). 删除所有记录 delete from 表名 

(2). 删除id=1的的记录 delete from 表名 where id=1;



三.数据查询语言(DRL:Data Retrieval Language)

(查询 索引数据。select语句)


1.查询全部数据

Select * from 表名;

例:Select * from stu;


2.根据条件查询指定的数据

Select * from 表名 where 列名1=值 and 列名2=值....

例:Select * from stu where sid=9 and ssex='女';


3.查询数据,返回指定的列

Select 列名1,列名2 from stu;

例:Select sid,sname from stu;


4.给指定返回列取别名(小名)

语法1. Select 列名 别名,列名2 别名2... from 表名;

例:Select sid id,sname name from stu;

语法2. Select 列名 as 别名,列名2 as 别名2... from 表名;

例:Select sid as id,sname as name from stu;


5.在条件中使用比较运算符 

SELECT * FROM 表名 where 字段 > < >= <= !=或<>

例:select * from stu where xsnianling !=18;


6.多条件的查询:

AND OR NOT

例:select * from stu where xsnianling <=21 and xsxingbie='女';

例:select * from stu where xsnianling <21 or xsxingbie='女';

例:select * from stu where xsnianling not in(18,21,25);


7.对空值的查询:is null  对应列是否null查询

例:select * from stu where xsxueli is not null;

例:select * from stu where xsxueli is null;


8.BETWEEN A AND B  在A和B之间,包含AB的值

例:select * from stu where xsnianling BETWEEN 18 and 21;


9.IN

例:select * from stu where xsnianling in(18,21,25);


10.模糊查询 LIKE

%:指代不明确值的位置或长度

_:指代明确值的位置或已知字符串长度

例:select * from stu where xsxingming like '_灵%'


11.查询中使用算术表达式:+ - * /

例:select xsxuehao+xsnianling from stu where xsxingming like '_灵%'


12.处理重复值:DISTINCT   排除重复展示,只展示一次

例:select DISTINCT xsxingbie from stu;


13.查询返回限定行数:LIMIT 

Limit 10 取查询数据的前10位

Limit 10,10  从查询数据的第11位开始,向后取10位数据展示,不满足10位也不会报错


14.通过查询复制表

create table stu1  select * from stu;

--只复制结构

例:create table stu2  select * from stu where 1=2;

--复制旧表的数据到新表(假设两个表结构一样) 

例:insert into stu2 select from stu;

--复制旧表的数据到新表(假设两个表结构不一样) 

例:insert into stu2(字段1,字段2,…….) SELECT 字段1,字段2,…… FROM stu;


15.分组 group by 

例:select ssex,COUNT(*) from stu GROUP BY ssex

注意:分组使用的时候group by字段一定要在select后面出现,如果使用了group by,select 后面就不要出现 * 


16.排序 order by 字段名:字段名就是我们需要排序的字段

order by  xsnianling  升序  ASC 默认

order by  xsnianling desc  降序


17.多个排序条件,当第一个条件相同时,以第二个条件排序

例:select * from stu order by age desc,createDate desc;


18.虚拟表 

在没有表被引用的情况下,允许您指定dual作为一个假的表名

例:select 1+1 from dual;



四.事务控制语言(TCL:Transaction Control Language)

(如commit、rollback语句)


事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit)。

在关系数据库中,一个事务可以是一条SQL语句,一组SQL语句或者整个程序


事务应该具有四个属性:

原子性(atomicity)、一致性(consistency)、隔离性(isolation)、持续性(durability)


设置默认事务提交方式

set autocommit=false  设置事务提交方式为”手动提交“

set autocommit=true  设置事务提交方式为”自动提交“


事务就是对数据库的多步操作,要么一起成功,要么一起失败

commit --手动提交事务

rollback --回滚事务

savepoint point2 --保存还原点

rollback to point2 --回滚到point2还原点



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI