MySQL语言结构1
SQL语言
SQL(Structured Query Language),结构化查询语言。
SQL语言分类
数据库定义语言(DDL)
DDL(data definition language)定义数据库中对象 表 视图 用户 函数 触发器
create 创建
alter 修改
drop 删除
create table teacher
(
tid int,
tname char(20)
);
alter table teacher add age int;
drop table teacher;
数据控制语言(DCL)
DCL(Data Control Langiuage)授权、拒绝用户的访问,撤销授予的权限。
Grant 授权
Deny 拒绝
Revoke 撤销权限
数据操作语言(DML)
DML(Data Manipulation Language)
即增删改查
insert
delete
update
select
数据类型
整型、字符型、布尔型、日期类型等
用户自定义变量
@name
系统变量
全局系统变量
会话系统变量
select "李华";
select 100+200;
select true,false;
-- 用户自定义变量
-- @+变量名表示变量
set @x = 100;
set @y = "lihua";
set @z = true;
select @x,@y,@z;
insert into teacher values(@x,@y,22);-- 插入的值可以是变量
set @a = (select tname from teacher where tid=1);
select @a;
-- 系统变量
-- 查看全局变量
show global variables;
-- 查看会话系统变量
show session variables;
-- 设置并查看某个全局变量(这里以sql_select_limit为例)的值
set @@global.sql_select_limit=2;
select @@global.sql_select_limit;
运算符
优先级
not > and > or
- 运算符
select 1+1,1-1,1*2,10/2;
select* from teacher;
select tid,tname,age+1 from teacher;
select 1=1,1<1,1>1;
select 'a'='A';-- 安装MySQL时设置了不区分大小写
select * from teacher where tname='lihua';
-- 逻辑运算符
insert into teacher values(2,'李白',47);
select * from teacher where tname like '李%' and age>30;
select * from teacher where tname like '李%' && age>30;
select * from teacher where tname like '李%' or tid>10;
select * from teacher where tname like '李%' || tid>10;
select * from teacher where not tname like '李%';
select * from teacher where ! (tname like '李%'); -- 用!必须要括起来,not则非必须。