mysql基础教程03、基本配置属性+基本语法

01、数据库基本操作语句

--查看数据库:
    --show databases;

--创建数据库:
    --create database 数据库名 charset = utf8;

--切换数据库|开始使用某一个数据
    --use 数据库名

--删除数据库:
    --drop database 数据库名;

--查看当前数据库状态:
    --select database();

02、数据表基本操作语句

--查看数据表
    --show tables;

--创建数据表:
    --create table 数据表名 (
        字段名称01  数据类型01  可选字段约束条件01,
        字段名称02  数据类型02  可选字段约束条件02,
        字段名称03  数据类型03  可选字段约束条件03,
        ...  ...  ...
    );

    --创建数据表示例:
        --create table students(
	        id int unsigned primary key auto_increment not null,
	        `name` varchar(20) not null,
	        age tinyint unsigned default 0,
	        height decimal(5, 2),
	        gender enum('男', '女', '中性')
        );

        --enum的枚举字段由单引号括起来
--修改表:
    --给数据表添加字段:
        --alter table 数据表名 add 字段名 数据类型 数据约束;
        --示例:
            --alter table students add birthday datetime;  增加数据表students的字段dirthday

    --修改数据表字段类型和约束[这个不能修改字段名]:
        --alter table 数据表名 modify 字段名 数据类型 数据约束
        --示例:
            --alter table students modify birthday date not null;

    --修改数据字段名称、类型和约束:
        --alter table 数据表名 change 旧字段名 新字段名 数据类型 数据约束;
        --示例:
            --alter table students change birthday birth data null

    --删除数据字段:
        --alter table 数据表名 drop 字段名;
        --示例:
            --alter table students drop birthday;

--查看表结构:
    --desc 数据表名;

--查看创建表的sql语句
    --show create table 数据表名;
    --示例:
        --show create table students;

--查看创库的sql语句
    --show create database 数据库名;
    --示例:
        --show create database python;

--删除表:
    --drop 数据表名;
    --示例:
        --drop students;

03、数据表中数据操作 -- 增删改查 curd

--select 查询:
    --全列查询:
        --select * from 表名 where 查询条件;
        --示例:
            select * from students where id = 6;

    --选择列查询:
        --select 列1, 列2, 列3, ... from 表名 where 查询条件;
        --示例:
            select name, age from students where id = 8;
--insert 插入:
    --
    --
    --
    --
        INSERT INTO students(`name`, age, height, gender, birthday, is_deleted) VALUES("二狗", 45, 178.00, '男', "1998-09-18", 0), ("岳山", 21, 168.00, '女', "1988-09-18", 0);
--update 更新:
    --update 表名 set 列1=值1,列2=值2... where 条件
    --示例:
        --update students set age = 18, gender = '女' where id = 6;
--删除数据:
    --物理删除:
        --delete from 表名 where 查询条件
        --示例:
            --deleted from students where id = 10;

    --逻辑删除:
        --逻辑删除指设定id_del字段。默认为0表示未被删除
            --alter table students add is_deleted bit default 0;
        --示例:
            --update table studentd set is_deleted = 1 where id = 8;  设置id为8的那条数据为删除状态

04、as 和 distinct

--as:取别名
    --select 列1 as 别名1, 列2 as 别名2, ... from 表名 where 查询条件
    --示例:
        SELECT `name` AS 姓名, age AS 年龄 FROM students;

    --select 也可以给表取别名,但是不建议这么做

--distinct: 去重,可以多个条件一起使用来去重
    --select distinct 列1, 列2, ... from 表名 where 查询条件;
    --示例:
        --select distinct name, age from students;

05、比较运算符

--比较运算符主要如下:
    = -- 等于
    != / <> -- 不等于
    > -- 大于
    < -- 小于
    >= -- 大于等于
    <= -- 小于等于

06、逻辑运算符

--逻辑运算符的分类如下:
    and -- 同时成立
    or -- 一个成立即可
    not -- 所有条件取反

    --示例:
        --SELECT * FROM students WHERE id > 0 AND NOT age = 35 ; 这里not可以用于限制一个条件也可以用于限制所有条件,也可以用()圈起来再取反
        --select * from students where id >3 or age = 35;

07、范围查询

--连续范围: between ... and ...
    --多用 < > 进行替代
    --select 列名 from 表名 where 字段 between A and B;
    --示例:
        select * from students where id between 1 and 10;

--非连续范围: in
    --select * from 表名 where 字段 in (可能的值01, 可能的值02, 可能的值03, ...)
    --示例:
        --select * from students where id in (3, 7, 9);

08、模糊查询

--模糊查询关键字段
    like -- 表示模糊查询
    % -- 表示多个可替代字符
    _ -- 表示一个可替代字符

--示例:
    --SELECT * FROM students WHERE `name` LIKE "曹_";
    --SELECT * FROM students WHERE `name` LIKE "曹%";

09、空判断

--记住一点:
    null != 空字符串

--判断为null
    --判断为空使用: is null

--判断为!null
    --判断非空使用: is not null

--示例:
    --SELECT * FROM students WHERE birthday IS NULL;
    --SELECT * FROM students WHERE birthday IS not NULL

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值