mysql drop database if exists_总结-MySQL

SQL语法

-- DDL(数据定义语言)

-- DML(数据操作语言)

-- DCL(数据控制语言)

-- 注意:SQL中大小写不敏感(大小写代表的是一样的);每条语句必须以分号结束

-- 一.DDL - 主要是提供数据库和表的创建、删除和修改

-- 1.创建数据库: create database 数据库名

create database school; -- 直接创建数据库

CREATE DATABASE IF NOT EXISTS school; -- 当数据库不存在时创建数据库

CREATE DATABASE IF NOT EXISTS school DEFAULT charset utf8; -- 创建数据库的时候指定编码方式

-- 2.删除数据库: DROP DATABASE 数据库

DROP DATABASE school; -- 直接删除指定数据库

DROP DATABASE IF EXISTS school; -- 如果指定的数据库存在就删除数据库

-- 3.使用数据库:use 数据库名;

use school;

-- 新建表 create table 表名(字段名1 类型1 , 字段名2 类型2,...);

-- 注意:a.表名一般需要加前缀't'或'tb',

-- b.字段用来确定表中要存储哪些数据,字段名随便命名但是不能是关键字

-- c.数据类型必须是MySQL支持的数据类型

-- 常用数据类型: int, char(size), carchar(size), text, bit, data

create table if not exists tb_student(

stu_id int,

stu_name varchar(20),

stu_gender bit,

stu_birth date

);

-- create table 表名(字段名1 类型1 约束1, 字段名2 类型2 约束2,...);

-- 常用的约束: not null - 不能为空,default - 设置默认值,unique - 值唯一,primary key - 主键约束

-- 主键约束: 表中能够唯一标识一条记录的字段(通过主键值能够找到表中唯一一条记录)

-- 注意:auto_increment只针对主键有效,并且主键的类型必须是数字整型

create table if not exists tb_student(

stu_id int not null auto_increment,

stu_name varchar(20) not null,

stu_gender bit default 1,

stu_birth date,

primary key(stu_id) -- 设置stu_id为主键(设置一个字段为主键会约束这个字段的值为唯一)

);

-- 删除表: drop table if exists 表名;

drop table if exists tb_student;

-- 清空表中的记录: TRUNCATE TABLE 表名;

TRUNCATE TABLE tb_student;

-- 修改表:

-- 添加字段: alter table 表名 add column 字段名 字段类型 约束;

alter table tb_student add column stu_score float(4,1) default 0;

-- 删除字段: alter table 表名 drop column 字段名

alter table tb_student drop column stu_score;

-- 二、DML(数据操作语言) - 主要针对数据库中数据的增、删、改、查

-- 1.增(添加数据/记录)

-- 1.1插入数据/记录: insert into 表名 value(字段1, 字段2, 字段3,...) - 依次给指定表中的字段赋值

insert into tb_student values(2, '张三', 0, '2013-9-20');

-- 1.2指定字段插入数据: insert into 表名(字段名1, 字段名2,...) value(字段1, 字段2,...) - 以指定的字段顺序给字段复制

insert into tb_student(stu_name, stu_gender) values('lisi', 1); -- 插入单条数据

insert into tb_student(stu_name, stu_gender) values

('小米', 1),

('李四', 0),

('王五', 1); -- 同时插入多条数据

-- 值的问题: sql中的值是数字的值直接写,是字符串的值需要用引号引起来,bit类型的值只有0或1,时间可以用内容是满足时间格式字符串也可以是通过时间函数获取的值

-- 时间函数: now() - 当前时间

now(); -- 当前时间

year(now()); -- 当前年

date(now()); -- 当前日期

-- 2.删 (删除数据/记录)

-- 2.1 delete from 表名; - 删除表中所有

delete from tb_student;

-- 2.2 delete from 表名 where 条件语句; - 删除满足条件的记录(行)

-- MySQL中的条件语句: =(判断是否相等)、<>(判断不等于)、>(判断大于)、=、<=

delete from tb_student where stu_id=10;

delete from ta_student where sti_name='zhangsan' or stu_id>8;

-- 3.改 (修改数据/记录)

-- update 表名 set 字段1=值1, 字段2=值2,...; - 将指定表中所有行的指定列/字段的值赋值为新值

update tb_student set stu_birth='1999-10-1', stu_gender=1;

-- update 表名 set 字段1=值1, 字段2=值2,... where 条件语句; - 将表中满足条件的行的指定字段的值赋值为新值

uodate tb_student set stu_gender = 0 where stu_name = '小米';

-- 通配符(只针对字符串有效)

-- 通配符%: 任意个数的任意字符

uodate tb_student set stu_birth = 0 where stu_name like '小%'; -- 修改名字是以‘小’开头的行的stu_birth的值

-- 通配符,表示一个任意字符

uodate tb_student set stu_birth = 0 where stu_name like '小_'; -- 修改名字只有两个字符,并且第一个字是小的stu_birth的值

-- 4.查(获取数据/记录)

-- 4.1直接查询

-- select * from 表名 - 获取指定表中所有行和列(所有的数据)

select * from tb_student;

-- select 字段名1,字段名2,... from 表名 - 获取指定表中所有行的指定列

select stu_name from tb_student;

-- select * from tb_student where 条件语句 - 获取指定表中满足条件的行的所有列的数据

select stu_id,stu_name from tb_student where stu_name like '小%';

-- 4.2列重命名

-- 注意: 直接查询所有无法对字段名进行重命名

-- select 字段名1 as 新字段名1, 字段名2 as 新字段名2,... from 表名;

select stu_id as '学号', stu_name as '姓名' from tb_student; -- 对查询结果中的stu_id、stu_name重命名

-- 注意:这里的as可以省略,但是最好别省

-- 4.3对查询的结果重新赋值(一般针对布尔数据)

-- select if(字段相关的条件语句,值1,值2) from tb_student - 查询指定字段,并且判断对应的值是0还是1,如果是1结果为值1,否则为值2

-- MySQL写法: if(字段, 值1, 值2)

select stu_name,if(stu_gender,'男','女') as '性别' from tb_student;

-- 通用写法: case 字段 when 值 then 新值1 else 新值2 end

select stu_name,case stu_gender when 1 then '男' else '女' end as '性别' from tb_student;

-- 注意: 这儿的if是MySQL专有的

-- 4.4对列合并

-- select concat(字段1, 字段2,...) from 表名;

select concat(stu_name,stu_id) from tb_student;

select concat(stu_name,':',stu_id) from tb_student; -- 定制合并格式-- 注意: 数字和字符串都可以合并,但是bit类型的值不可以合并

-- 4.5模糊查询 - 查询时通过like条件来指定查询对象

-- sql中支持逻辑运算符and(逻辑与运算)、or(逻辑或运算)和not(逻辑非)

select * from tb_student where stu_name like '%米%' and stu_id < 110;

-- 4.6排序

-- select 字段1,字段2,... from 表名 order by 字段; -- 对查询结果根据字段升序排列

-- select 字段1,字段2,... from 表名 order by 字段 desc; -- 对查询结果根据字段降序排列

select * from tb_student order by stu_id; -- 根据stu_id升序排列

select * from tb_student order by stu_id desc; -- 根据stu_id降序排列

-- select 字段1,字段2,... from 表名 order by 字段1 asc, 字段 desc; -- 对查询结果根据字段1升序、字段2降序联合排列,在前面的字段的优先级更高

select * from tb_student order by stu_gender asc, stu_id desc;

-- 4.7限制

-- select * from 表名 limit N; - 获取查询结果的前N条记录

select * from tb_student limit 3; -- 获取查询结果的前3条记录

-- select * from 表名 limit M offset N - 对查询结果跳过N条数据, 取M条数据出来

select * from tb_student limit 3 offset 4;

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值