SQL 基本操作
查看数据库
使用 SHOW DATABASES 语句查看数据库
show databases;
创建、查看和删除表
使用 CREATE TABLE 语句创建表
-- 拆分表
CREATE TABLE multi_db_single_tbl(
id int auto_increment,
name varchar(30),
primary key(id)
) dbpartition by hash(id);
-- 单表
CREATE TABLE single_tbl(
id int,
name varchar(30),
primary key(id)
);
使用 SHOW CREATE 语句查看建表语句
show create table multi_db_single_tbl;
show create table single_tbl;
使用 DROP TABLE 语句删除表
drop table multi_db_single_tbl;
drop table single_tbl;
使用 SHOW TABLES 语句查看数据库中的所有表
show tables;
加列、减列、修改数据类型
使用 ALTER TABLE 语句加列、修改列、减列
alter table multi_db_single_tbl add column textcol text;
alter table multi_db_single_tbl modify column textcol varchar(40);
alter table multi_db_single_tbl drop column textcol;
创建、查看和删除本地索引
创建本地索引
create index idx_name on multi_db_single_tbl (name);
or
alter table multi_db_single_tbl add index idx_name(name);
查看本地索引
show index from multi_db_single_tbl;
删除本地索引
drop index idx_name on multi_db_single_tbl;
or
alter table multi_db_single_tbl drop index idx_name;
增删改查数据
使用 INSERT 语句向表内插入数据
insert into multi_db_single_tbl (name) values ('test_name');
insert into multi_db_single_tbl (name) values ('test_name');
insert into multi_db_single_tbl (name) values ('test_name'),('test_namexx');
使用 SELECT 语句检索表内数据。例如:
select * from multi_db_single_tbl;
使用 UPDATE 语句修改表内数据。例如:
update multi_db_single_tbl set name='zzz' where id in (100001,100002,100003,100004);
使用 DELETE 语句删除表内数据:
delete from multi_db_single_tbl where id = 100002;
创建、授权和删除用户
使用 CREATE USER 语句创建一个用户 drdsuser,密码为 123456
create user drdsuser@'%' identified by '123456';
授权用户 drdsuser 可检索数据库 sample_db 内的表
grant insert,update,delete,select on sample_db.* to drdsuser@'%';
查询用户 drdsuser 的权限
show grants for drdsuser@'%'
删除用户 drdsuser
drop user drdsuser@'%'
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。
评论