美文网首页
02-25 Mysql 基础语法

02-25 Mysql 基础语法

作者: ychaochaochao | 来源:发表于2019-02-25 17:47 被阅读0次

    -- sql 语法
    -- DDL(数据定义语言)
    -- DML(数据操作语言 )
    -- DCL(数据控制语言)
    -- --注意:sql中大小写不敏感(大写和小写是一样的),每条sql语句以分号结束

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

    -- 0.删除数据库
    drop DATABASE school;; --直接删除指定数据库
    drop DATABASE if EXISTS school;
    -- --如果指定的数据库存在删除数据库

    -- 1.创建数据库
    -- 直接创建数据库
    create database school;
    -- 当指定数据库不存在创建数据库
    CREATE DATABASE if NOT EXISTS school2;
    -- 支持中文
    create database if not exists school default CHARSET utf8;

    -- 2.使用/切换数据库 :use 数据库名
    use school2;

    -- 3.新建表 create table 表名(字段名1 类型1,字段名2 类型2。。。)
    -- 注意:a.表名一般需要加前缀’t'或者‘tb’ b.字段用来确定表中存储哪些数据,字段名随便命名,但不能是关键字。c.数据类型必须是mysql支持的数据类型

    -- 常用数据类型:int 定长字符串char(size) 不定长字符串varchar(size) 无约束字符串text
    create table t_student(stuid int,stuname varchar(20),gender bit,birth date);

    -- create table 表名(字段名1 类型1,字段名2 类型2。。。)
    -- 常用的约束:not null --不能为空 default-- 默认值 unique --值唯一 primary KEY - 主键约束
    -- 主键约束:指的是表中能够唯一标识一条记录的字段(通过主键值能够找到表中唯一一行记录。)
    -- 注意:一个字段可以添加多个约束,多个约束之间用空格隔开
    create table t_student(
    -- auto_increment 自动增长,只针对primary key有效,并且primary key的类型是int
    stuid int not null auto_increment,
    stuname varchar(20) not null,
    gender bit default 1 ,
    birth date,
    -- 设置primary key 为stuid,设置一个字段为主键,其实间接约束了当前字段的值唯一
    primary key(stuid));

    -- 4.删除表:drop table if exists
    -- 清空表:truncate table (不能使用if exists)
    drop table if exists t_student;
    truncate table t_student;

    -- 5.修改表(主要是添加字段和删除字段(列))
    -- 5.1 添加字段(列) alter table 表名 add column 字段名 字段类型 约束
    alter table t_student add column scores float(5,2) default 0;
    -- 5.2 删除字段 alter table 表名 drop column 字段名
    alter table t_student drop column gender;

    -- 二。DML 数据操作语言 -主要针对数据库中的数据增删改查
    -- 1.增(添加数据/记录)
    -- 1.1 插入数据/记录 insert into 表名 values(值1,值2,值3。。。) 依次给指定表中的字段赋值
    insert into t_student values(103,'xiaoming',0,'2010-9-8'),(2,'xiaohua',0,'1994-3-4'),(102,'xiaohong',0,'1998-9-10');

    -- 1.2 插入数据/记录
    -- insert into 表名(字段1,字段2) values(值1,值2) 依次给指定表中的指定字段赋值,约束为not null的字段必须写

    insert into t_student(stuname,birth) values('lisi','1991-10-20');

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

    -- 时间函数:now() -当前时间
    -- select now(); -结果显示当前 年月日 时分秒
    -- select date(now()); -获取当前日期
    -- SELECT year/month/day/hour/minute/second(now);

    -- 2.删(删除数据/记录)
    -- 2.1 delete from 表名; --删除指定表的所有记录
    delete from t_student;

    -- 2.2 delete from 表名 where 条件语句 --删除满足条件的记录
    -- SQL中的条件语句:=(判断是否相等) <>(不等于,和Python中的!=功能一样,> , < ,>= ,<=)
    delete from t_student where stuid=1;
    delete from t_student where stuid<100;

    -- 3.改(修改数据/记录)
    -- update 表名 set 字段1=新值1,字段2=新值2。。;
    -- 将指定表中的指定字段/列的全部值赋值为新值
    update t_student set gender=1;

    -- update 表名 set 字段1=新值1,字段2=新值2。。 where 条件语句
    -- 将指定表中的满足条件的行的值赋值为新值
    update t_student set gender=0 where stuname='xiaohua' or stuname='xiaohong ';
    -- 通配符% : 表示任意个数的任意字符,包括0个
    -- 通配符_ : 表示一个任意字符
    update t_student set birth='2000-01-01' where stuname like 'xiao%';
    -- 通配符_ : 表示一个任意字符
    update t_student set birth='2000-12-12' where stuname like 'lis_';
    -- 注意:通配符只对字符串有效

    -- 4 查(获取数据)
    -- 4.11 select * from 表名 -直接查询,获取指定表中所有数据
    select * from t_student;

    -- 4.12 select 字段名1,字段名2。。。from 表名 -获取指定表中所有行中指定的列
    select stuname,stuid from t_student ;

    -- 4.13 select 字段名1,字段名2。。。 from 表名 where 条件语句
    -- 获取指定表中满足条件的行的指定字段
    select stuname,stuid from t_student where stuname like 'xiao%';

    -- 4.2 列/字段重命名
    -- select 字段1 as 新字段1,字段2 as 新字段2.。。from 表名,as可以省略
    select stuid,stuname,gender as '性别' from t_student;

    -- 4.3 对查询结果赋值(一般针对布尔数据 bit)
    -- select if (字段名,值1,值2) from 表名;
    -- 查询指定字段,并且判断字段和对应的值是0还是1,如果是1取值1,如果是0取值2
    -- 这里的if用法是Mysql专有的,别的数据库不能使用
    select if(gender,'男','女') as '性别' from t_student;
    -- 通用写法
    select case gender when 1 then '男' else '女' end as '性别' from t_student;

    -- 4.4 对列进行合并运算
    -- select concat(字段1,字段2。。。) from 表名;
    -- 注意:bit类型(布尔)的数据不能合并,数字和字母可以合并
    select concat (stuname,':',stuid) as 'name_id' from t_student ;

    -- 4.5 模糊查询
    -- 查询时通过like条件来指定查询对象
    -- sql 支持逻辑运算符and / or / not
    select * from t_student where stuname like '%i%' and not stuid<102;

    -- 4.6 排序(先查询再排序)
    -- select * from 表名 order by 字段
    -- 对查询结果按照指定字段的值进行升序排序
    -- select * from 表名 order by 字段 esc --升序
    -- select * from 表名 order by 字段 desc --降序

    select * from t_student order by gender;
    select * from t_student order by stuid desc;

    -- 先按性别升序排序,再按学号降序排序,写在前面的字段优先级高
    select * from t_student order by gender,stuid desc;

    -- 4.7 限制
    -- select * from 表名 limit N - 获取查询结果中的前n条数据
    select * from t_student limit 3;

    -- select * from 表名 limit M offset N -对查询结果跳过前N条数据往后取M条数据
    select * from t_student limit 3 offset 4; -跳过前4条数据去3条数据

    相关文章

      网友评论

          本文标题:02-25 Mysql 基础语法

          本文链接:https://www.haomeiwen.com/subject/pengyqtx.html