MySQL常用命令

MySQL创建用户并添加权限的方式:

1.进入mysql数据库

mysql -u root mysql

2.在mysql界面添加用户及权限

create user 'pyjsh'@'localhost' identified by 'pyjsh';
grant all on *.* to pyjsh@'%' identified by 'pyjsh';
grant all on *.* to pyjsh@'localhost' identified by 'pyjsh';
flush privileges;

查询数据库

show databases;

创建数据库

create database dbname;

删除数据库

drop database dbname;

创建数据库表并设置自增长主键

MariaDB [dyf_da]> create table test_tb
    -> (
    -> id int(11) not null AUTO_INCREMENT primary key,
    -> total int(11),
    -> name varchar(256));
Query OK, 0 rows affected (0.021 sec)

查看数据表结构

desc test_tb;
show columns from test_tb; 和上条命令查询结果一致

向数据表中插入列

alter table test_tb add column age varchar(256);

修改列的类型

alter table test_tb modify age int(11);

插入一条新数据

insert into test_tb(total,name,age) values(34,'tina',23);

修改一条数据记录

update test_tb set total=15,name='andy',age=22 where id=1;

查询记录数量

 select count(*) from test_tb; 

按关键字查询

select * from test_tb where name like '%and%';

数据表重命名

alter table test_tb rename to tb01;

删除数据表中的数据

delete from tb01 where id=2;

清空数据表:清空后主键id会从1开始递增

truncate table tb01;

复制一个表

create table tb2 select * from tb01;

删除数据表

drop table tb01;

删除数据库

drop database dyf_da;
posted @ 2019-08-20 23:13  温茶又折花  阅读(128)  评论(0编辑  收藏  举报