推特 阿里云技术文档正文

SQL 基本操作_快速入门_分布式关系型数据库 DRDS

admin 阿里云技术文档 2020-02-11 193 0
阿里云服务器优惠

SQL 基本操作

查看数据库

使用 SHOW DATABASES 语句查看数据库

  1. show databases;

创建、查看和删除表

使用 CREATE TABLE 语句创建表

  1. -- 拆分表
  2. CREATE TABLE multi_db_single_tbl(
  3. id int auto_increment,
  4. name varchar(30),
  5. primary key(id)
  6. ) dbpartition by hash(id);
  7. -- 单表
  8. CREATE TABLE single_tbl(
  9. id int,
  10. name varchar(30),
  11. primary key(id)
  12. );

使用 SHOW CREATE 语句查看建表语句

  1. show create table multi_db_single_tbl;
  2. show create table single_tbl;

使用 DROP TABLE 语句删除表

  1. drop table multi_db_single_tbl;
  2. drop table single_tbl;

使用 SHOW TABLES 语句查看数据库中的所有表

  1. show tables;

加列、减列、修改数据类型

使用 ALTER TABLE 语句加列、修改列、减列

  1. alter table multi_db_single_tbl add column textcol text;
  2. alter table multi_db_single_tbl modify column textcol varchar(40);
  3. alter table multi_db_single_tbl drop column textcol;

创建、查看和删除本地索引

创建本地索引

  1. create index idx_name on multi_db_single_tbl (name);

or

  1. alter table multi_db_single_tbl add index idx_name(name);

查看本地索引

  1. show index from multi_db_single_tbl;

删除本地索引

  1. drop index idx_name on multi_db_single_tbl;

or

  1. alter table multi_db_single_tbl drop index idx_name;

增删改查数据

使用 INSERT 语句向表内插入数据

  1. insert into multi_db_single_tbl (name) values ('test_name');
  2. insert into multi_db_single_tbl (name) values ('test_name');
  3. insert into multi_db_single_tbl (name) values ('test_name'),('test_namexx');

使用 SELECT 语句检索表内数据。例如:

  1. select * from multi_db_single_tbl;

使用 UPDATE 语句修改表内数据。例如:

  1. update multi_db_single_tbl set name='zzz' where id in (100001,100002,100003,100004);

使用 DELETE 语句删除表内数据:

  1. delete from multi_db_single_tbl where id = 100002;

创建、授权和删除用户

使用 CREATE USER 语句创建一个用户 drdsuser,密码为 123456

  1. create user drdsuser@'%' identified by '123456';

授权用户 drdsuser 可检索数据库 sample_db 内的表

  1. grant insert,update,delete,select on sample_db.* to drdsuser@'%';

查询用户 drdsuser 的权限

  1. show grants for drdsuser@'%'

删除用户 drdsuser

  1. drop user drdsuser@'%'
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

评论

-----