zoukankan      html  css  js  c++  java
  • mysql基础语法大全

    向来比较懒,能动嘴的坚决不动手,后来发现还是绝知此事要躬行,总结记录下mysql的知识点吧,凑合着看。

    1 DDL数据定义语言

    1-1 创建数据库

    创建数据库db1

    create database db1;
    

    判断数据库是否存在 ,不存在则创建数据库

    create database if not exists db1;
    

    创建数据库db1并指定字符集为gbk

    create database db1 default character set gbk;
    

    1-2 查看数据库

    查看所有数据库

    show databases;
    

    查看某个数据库的定义信息

    show create database travel;
    

    修改数据库默认的字符集

    alter database db1 character set gbk;
    

    删除数据库

    drop database db1;
    

    查看正在使用的数据库

    select database();
    

    切换数据库

    use db1;
    

    1-3 DDL操作表结构

    查看有哪些表

    show tables;
    

    查看student表结构

    desc student;
    

    查看表的sql语句、

    show create table student;
    

    快速创建一个表结构相同的表

    create table t1 like student;
    

    删除表

    drop table student;
    

    添加一个新字段

    alter table student add city varchar(20);
    

    修改一个字段

    alter table student modify city varchar(10);
    

    修改列名

    alter table student change city  country varchar(5);
    

    删除列

    alter table student drop country;
    

    修改表名

    rename table student to stu;
    

    2 DML

    2-1 增加

    插入部分数据

    insert into student (id,name,age) values (1,'nezha',20)
    

    插入全部数据(无需指定字段名)

    insert into student  values (1,'nezha',20)
    

    2-2 更新

    把id=2学生age改为12

    update student set age =12 where id = 2;
    

    2-3 删除

    删除ID=2的学生

    delete from student where id = 2;
    

    3 DQL数据查询语言

    查询所有

    select * from student;
    

    使用字段别名进行查询

    select name as 姓名  from  student;
    

    使用表的别名进行查询

    select * from student as s1;
    

    查询去掉重复值

    select distinct city from student;
    

    查询年龄不等于20学生

    select * from student where age <>20;
    
    select * from student where age !=20;
    

    查询ID是1,3,5的学生

    select * from student where id in (1,3,5);
    

    查询成绩大于20,小于30学生

    select name from student where score between 20 and 30;
    

    查询姓马的学生

    select name from student where name like '马%'
    

    查询姓名有马字学生

    select name from student where name like '%马%'
    

    通配符

    通配符 说明
    % 匹配任意多个字符
    _ 匹配一个字符
  • 相关阅读:
    剑指Offer-49.把字符串转换成整数(C++/Java)
    codeforces Gym 100338H High Speed Trains (递推,高精度)
    codeforces Gym 100338E Numbers (贪心,实现)
    codeforces Gym 100338C Important Roads (重建最短路图)
    HDU 4347 The Closest M Points (kdTree)
    UVA 10817
    HDU 4348 I
    HDU 4341 Gold miner (分组背包)
    UVA 1218
    UVA 1220 Party at Hali-Bula (树形DP)
  • 原文地址:https://www.cnblogs.com/hellosiyu/p/12482385.html
Copyright © 2011-2022 走看看