sql常用命令之增删改查
Posted 落日峡谷
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql常用命令之增删改查相关的知识,希望对你有一定的参考价值。
1. 显示数据库 show databases;
show databases;
2. 显示当前数据库、用户和版本
select current_database(); # hive select database(); #mysql select user(); #显示当前用户 select version(); #版本
3. 创建/删除数据库
create database db_name; drop database db_name;#删除数据库
4. 选择某个数据库
use db_name;
5. 显示当前数据库下的表
show tables;
6. 创建表 create table
create table if not exists my_test( id INT UNSIGNED AUTO_INCREMENT, name_people VARCHAR(40) NOT NULL, submission_time DATETIME, PRIMARY KEY(id) )ENGINE=InnoDB DEFAULT CHARSET=utf8;
7. 描述表结构DESC或者DESCRIBE、show columns from
DESC my_test;
SHOW COLUMNS FROM my_test; #显示表结构
8. 插入数据 insert into
INSERT my_test( id, name_people, submission_time ) VALUES (1, \'Mary\', NOW());
9 .查询 select
select * from my_test;
select name_people from my_test;
select name_peole from my_test where id=1;
10. 删除一条记录 delete
delete from my_test where id=1;
11. 删除表中的所有记录,但是保存原来的表结构
delete from my_test;
12. 更新表的某一列 update
UPDATE my_test SET name_people = \'陛下\' WHERE id=1
13. 更新多列数据,用 \',\' 逗号隔开
UPDATE my_test SET name_people = \'将军\', submission_time = NOW() WHERE id=1
14. 增加新列 alter,然后update
ALTER table my_test add name_adress VARCHAR(50) not null; update my_test set name_adress=\'北京\';
15. 复制另一张表的数据和表结构,来新建表 create,使用as关键词
CREATE TABLE my_test_copy AS SELECT * FROM my_test; SELECT * from my_test_copy;
16. 删除表
drop table table_name;
17. 增加主键,需要先删除原来的主键,否则会报错:[Err] 1068 - Multiple primary key defined
#增加主键 alter table my_test add phone_number VARCHAR(30) not null; update my_test set phone_number=123456789; alter table my_test add primary key (phone_number); #[Err] 1068 - Multiple primary key defined,需要先删除原来的主键
18. 删除主键,删除主键出现:[Err] 1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
alter table my_test drop primary key; #一般的error:[Err] 1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key # 表示自增列只能有1列,且这列必须为key,也就是建表的时候,如果是自增列,必须用primary key标识 # 例如: create table if not exists my_test( id INT UNSIGNED AUTO_INCREMENT, name_people VARCHAR(40) NOT NULL, submission_time DATETIME, PRIMARY KEY(id) )ENGINE=InnoDB DEFAULT CHARSET=utf8; #出现错误的原因是:主键是自增属性,不能直接删除
19. 建立一个新表,但是主键不设置为自增属性,然后就可以删除主键
create table if not exists my_test_key( id INT UNSIGNED, name_people VARCHAR(40) NOT NULL, submission_time DATETIME, PRIMARY KEY(id) )ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT my_test_key( id, name_people, submission_time ) VALUES (1, \'Tom\', NOW()); show keys from my_test_key; #显示主键 alter table my_test_key drop primary key; show keys from my_test_key; #显示主键
20. 建立索引 create index
create index index_one on my_test_key(submission_time);
21. 建立唯一索引 create unique index
create unique index index_two on my_test_key(name_people);
22. 显示索引 show index
show index from my_test_key; #显示索引,my_test_key属于表名
23. 显示主键 show keys
show keys from my_test_key; #显示主键,my_test_key属于表名
24. 删除索引 drop index
drop index index_one on my_test_key; alter table my_test_key drop index index_two;
#欢迎交流
以上是关于sql常用命令之增删改查的主要内容,如果未能解决你的问题,请参考以下文章