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

    1.增加用户

    添加一个用户对所有数据库有增删改查的功能  用命令grant(英文授权的意思)

    格式为:grant 权限 on 数据库.* to 新增用户@登录主机 identfied by "密码";

    例如增加用户 paddy 密码123456 只在本地对所有数据库具有增删改查的权限

    grant select,insert,update,delete on *.* to paddy@localhost identified by "123456";

    如果用户要在任何主机上登录数据库,只需要将localhost修改为"%",如果为指定的IP登录,就修改为"ip地址"     

    例如:grant select,insert,update,delete on *.* to 'paddy'@"127.0.0.1" identified by "123456";

    2.显示、创建、删除、选择

    show databases;

    create database  paddy; 

    drop database paddy;

    use  paddy;

    #判断是否存在数据库paddy,有的话先删除
    drop database if exists paddy;

    3.表 table

    查表、

    show tables;

    #创建表
    create table student(
    id int auto_increment primary key,(id自增长的意思)
    name varchar(50),
    sex varchar(20),
    date varchar(50),
    content varchar(100)
    )default charset=utf8;

    删除表

    dorp table student;

    查看表的结构
    describe student;  #可以简写为desc student;

    插入数据

    insert into student values(null,'aa','男','1988-10-2','......');

    查询表中数据

    select * from student;

    查询表中某项数据

    select name from student;

    修改表中某处数据

    update  表名  set 列名和修改的字段 地点

    update student set nane="paddy" where id=4;

    删除数据;

    delete  from student where id=4;

    且 and

    格式  select * from 表名 where 列名 区间 and 列名 区间;

    查询出生在1980-1-10和1985-1-10之间的人

    select * from student where date>'1988-1-2' and date<'1988-12-1';

    或 or

    select * from student where data>'1988-1-01'or date<'1965-01-22';

    between

    select * from student where date between '1988-1-2' and '1988-12-1';

    in 查询集合内的数据

    select * from student where  id in {1,5,9};

    表按升序排序 asc 表按降序排序 desc

    格式:select * from 表名 order by asc;

    select * from student order by asc;

    select * from student order by desc;

    查询第n条到第M条的数据,不包含n

    格式:select * from 表名 limit  m,m;

    select * from student limit 2,5; 显示3-5条数据

    统计表中总数 count(*)

    select count(*) from student;

    例如统计id总数

    select count(id) from student;

    修改表的名字

    格式 alter table 旧的表名 rename to 新的表名;

    alter table a rename to b;

     

    #修改表的名字
    #格式:alter table tbl_name rename to new_name
    alter table test rename to test_rename;

    #向表中增加一个字段(列)
    #格式:alter table tablename add columnname type;/alter table tablename add(columnname type);
    alter table test add columnname varchar(20);

    #修改表中某个字段的名字
    alter table tablename change columnname newcolumnname type; #修改一个表的字段名
    alter table test change name uname varchar(50);

    select * from test;

    #表position 增加列test
    alter table position add(test char(10));
    #表position 修改列test
    alter table position modify test char(20) not null;
    #表position 修改列test 默认值
    alter table position alter test set default 'system';
    #表position 去掉test 默认值
    alter table position alter test drop default;
    #表position 去掉列test
    alter table position drop column test;
    #表depart_pos 删除主键
    alter table depart_pos drop primary key;
    #表depart_pos 增加主键
    alter table depart_pos add primary key PK_depart_pos
    (department_id,position_id);

    #用文本方式将数据装入数据库表中(例如D:/mysql.txt)
    load data local infile "D:/mysql.txt" into table MYTABLE;

    #导入.sql文件命令(例如D:/mysql.sql)
    source d:/mysql.sql; #或者 /. d:/mysql.sql;

     

  • 相关阅读:
    339. Nested List Weight Sum
    41. First Missing Positive
    366. Find Leaves of Binary Tree
    287. Find the Duplicate Number
    130. Surrounded Regions
    ubuntu18.04安装mongodb4.4
    阿里dataX配置使用
    MySQL主从同步简单介绍&配置
    阿里yugong配置使用
    ubuntu编译安装mysql
  • 原文地址:https://www.cnblogs.com/paddygege/p/6363220.html
Copyright © 2011-2022 走看看