• Mysql基础语法


    mysql

    SELECT VERSION(), CURRENT_DATE, now();
    select user();

    create database pets;
    show databases;

    use pets;

    CREATE TABLE cats
    (
    id INT unsigned NOT NULL AUTO_INCREMENT, # Unique ID for the record
    name VARCHAR(150) NOT NULL, # Name of the cat
    owner VARCHAR(150) NOT NULL, # Owner of the cat
    birth DATE NOT NULL, # Birthday of the cat
    PRIMARY KEY (id) # Make the id the primary key
    );
    show tables;

    describe cats; desc

    INSERT INTO cats ( name, owner, birth) VALUES
    ( 'Sandy', 'Lennon', '2015-01-03' ),
    ( 'Cookie', 'Casey', '2013-11-13' ),
    ( 'Charlie', 'River', '2016-05-21' );

    SELECT * FROM cats;

    增加/删除/修改列:

    alter table cats add gender char(1) [after name];
    alter table cats drop gender;

    alter table cats change name name_new char(30);

    alter table cats modify name char(10);

    alter table cats alter age set default 10;

    alter table cats alter age drop default;

    alter table cats rename to cats_t;

    查看用户权限:

    SHOW GRANTS FOR 'admin'@'localhost';

    日期计算
    http://dev.mysql.com/doc/refman/5.7/en/date-calculations.html

    自定义变量
    mysql> SELECT @min_price:=MIN(price),@max_price:=MAX(price) FROM shop;
    mysql> SELECT * FROM shop WHERE price=@min_price OR price=@max_price;

    自增列:
    CREATE TABLE animals (
    id MEDIUMINT NOT NULL AUTO_INCREMENT,
    name CHAR(30) NOT NULL,
    PRIMARY KEY (id)
    );
    INSERT INTO animals (name) VALUES ('dog'),('cat'),('penguin'),('lax'),('whale'),('ostrich');

    更新列:

    update user set age =age+1 where id=1;

  • 相关阅读:
    HDU1041
    HDU1005
    HDU1231
    MYSQL入门总结
    oracle性能问题排查~记一个单实例的问题
    mysql案例~关于mysql的配置文件个人见解
    数据恢复系列~恢复方案制定
    mysql架构解读~mysql的多源复制
    mysql 案例~select引起的性能问题
    遭遇Bad version number in .class file
  • 原文地址:https://www.cnblogs.com/luangeng/p/5958026.html
Copyright © 2020-2023  润新知