zoukankan      html  css  js  c++  java
  • mysql SQL语句

    初学mysql,总结下mysql语法语句
     
    1、mysql接口自带命令
     
    1.1、h 或 help 或 ?
    1.2、G
    1.3、T 或 tee
    1.4、c 或 CTRL+c
    1.5、s 或 status
    1.6、. 或 source
    1.7、u 或use
    help contents (可详细展开)
    show create database test1;可以查看建库语句和字符集
     
    2、SQL语句种类介绍
    SQL92 SQL99:
    DDL
    DML
    DCL
    DQL
     
    3、DDL:数据定义语言
    3.1 介绍:
    逻辑结构:
    库(库名字、库的属性)、表(表名、表的列(列名字、列属性、约束)、表其他属性、表数据)
    DDL 用来定义:数据库的元数据
     
    3.2 DDL语句——库定义
    3.2.1 创建:
    CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
        [create_specification] ...
     
    CREATE DATABASE jiaguren CHARSET utf8;
     
    开发规范:
    1、库名字,使用小写字母。
    2、创建库时,一并指定字符集。
     
    查询创建的数据库:
    show databases;
    show create database test;
     
    3.2.2 删除:
    drop database test;
     
    3.2.3 修改:
    alter database test1 charset utf8;
     
     
    3.3 DDL语句——表定义
     
    3.3.1 创建
    create table 
    help create table
     
    USE jiaguren;
    CREATE TABLE student(
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '学员序号',
    NAME VARCHAR(20) NOT NULL  COMMENT '学员姓名',
    age TINYINT UNSIGNED   NOT NULL COMMENT '学员年龄',
    gender ENUM('m','f') NOT NULL DEFAULT 'm' COMMENT '学员性别',
    ruxuedate DATETIME NOT NULL DEFAULT NOW() COMMENT '入学时间'
    )ENGINE=INNODB CHARSET utf8;
     
    查看创建的表:
    SHOW TABLES;
    show create table student;
    desc student;
     
    扩展:
    create table stu like student;
    create table stu1 select * from student;
     
     
    3.3.2 删除定义
    drop table stu1;
     
     
    3.3.3 修改
    (1)修改表名字
    alter table student rename to stu;
    (2)在表中添加一列telnum char(11);
    alter table stu add telnum char(11);
    (3)在name列后加入新列 qq varchar(20) not null unique
    alter table stu add qq varchar(20) not null unique after name;
    (4)在表的第一列加入新列classid int not null
    alter table stu add classid int not null  first;
    (5)删除表中的classid列
    alter table stu drop classid;
    (6)修改表中telnum 列的数据类型为bigint
    alter table stu change   telnum telnum bigint;
     
    (7)删除表的所有数据行
    truncate  table stu;
     
     
    3.4 DML语句
    操作表中的数据行
     
    3.4.1 增(insert)
    insert into stu values(1,'zs','12345',20,'m',now(),110);
    insert into stu(name,qq,age,telnum) values('ls','23456',21,119);
    insert into stu(name,qq,age,telnum) values('lss','223456',29,118),('lsss','2234516',129,1222);
     
    select * from stu;
     
    3.4.2 删
    delete from stu;
    delete from stu where name = 'lsss';
    delete from stu where name like 'ls%';
     
    3.4.3 改
    update stu set age=40 where name='zs';
     
    扩展:屏蔽delete,伪删除,使用update替代delete
    alter table stu add state enum('1','0') not null default '1';
    update  stu set state='0' where name='lsss'; 
    替代:
    delete from stu where name='lsss';
    在将来业务中:
    select * from stu where state='1';
     
     
    3.5 DQL:
    3.5.1 数据行的查询(select)
    (1) from 
    (3) join 
    (2) on 
    (4) where 
    (5) group by(开始使用select中的别名,后面的语句中都可以使用)
    (6) having 
    (7) order by
    (8) limit
     
     
    -- 1、全表查询
    SELECT * FROM city;
     
    -- 2、部分数据查询
    SELECT NAME ,population  FROM city;
     
    -- 3、where子句的时候--->过滤查询
    --- 3.1 等值查询
    SELECT * FROM city WHERE countrycode='CHN';
     
    --- 3.2 不等值查询(> < >= <= ,<>) 
    SELECT * FROM city WHERE countrycode='CHN' AND population>5000000;
    --- 3.3 连接符(AND,OR)
    --- AND,需要保证前后条件都满足
    --- OR, 只需要满足其中一个条件即可
       
    SELECT * FROM city WHERE countrycode='CHN' OR countrycode='USA';
    SELECT * FROM city WHERE countrycode IN ('CHN','USA');
     
    ---> 一般会改写为 UNION
    SELECT * FROM city WHERE countrycode='CHN'
    UNION
    SELECT * FROM city WHERE countrycode='USA';
     
    --- 3.4 BETWEEN AND 
     
    SELECT * FROM city WHERE population BETWEEN  1000000 AND 2000000 ;
     
     
    -- 4、order by + limit
     
    SELECT * FROM city WHERE countrycode='USA' ORDER BY population DESC;
    SELECT * FROM city WHERE countrycode='USA' ORDER BY population DESC LIMIT 10; (排序后只显示前10个)
     
    SELECT * FROM city WHERE countrycode='USA' ORDER BY population DESC LIMIT 10,10;(排序后跳过10行再显示10个,就是11-20行)
    SELECT * FROM city WHERE countrycode='USA' ORDER BY population DESC LIMIT 10 OFFSET 10;(排序后跳过10行再显示10行,同上)
     
     
     
    元数据查询
    mysqlshow
    show
    information_schema
     
  • 相关阅读:
    线性代数学习之初等矩阵和矩阵的可逆性
    线性代数学习之线性系统
    容器远程访问vnc--CentOS 6.8安装和配置VNC
    docker安装-单机/多机安装
    docker aufs存储驱动文件系统
    基于PowerCli自动部署和配置vmvare虚拟机
    使用Kubespray在ubuntu上自动部署K8s1.9.0集群
    python应用-pycharm新建模板默认添加shebang编码作者时间等信息
    python开发基础作业01:模拟登陆系统
    git 添加码云远程仓库和上传到码云的命令
  • 原文地址:https://www.cnblogs.com/nadian-li/p/13050941.html
Copyright © 2011-2022 走看看