面试复习

MySQL常用语句

2020-05-10  本文已影响0人  云三木

一、数据库连接,用户

1.连接mysql

本机连接: mysql -u 用户名 -p 密码
远程连接: mysql -h 主机ip -u 用户名 -p 密码

2.修改密码

mysqladmin -u 用户名 -p 原密码 password 新密码
忘记密码修改方法:https://blog.csdn.net/weidong_y/article/details/80493743

3.添加新用户

添加一个拥有所有权限的用户:
grant select,insert,update,delete,create,drop,alter on *.* to [email=用户名@”%]用户名@”%[/email]” Identified by “密码”;
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER ON 数据库名.* TO 数据库名@localhost IDENTIFIED BY '密码';
限制用户只能在本机localhost登陆:
grant select,insert,update,delete on mydb.* to [email=用户名@localhost]用户名@localhost[/email] identified by “密码”

二、数据库操作

1.创建数据库

创建数据库
create database <数据库名>

设置密码
set password for '数据库名'@'localhost' = '密码';

分配用户
grant select,insert,update,delete,create,drop,alter on 数据库名.* to 数据库名@localhost Identified by '密码';

2.显示数据库

show databases; 

3.删除数据库

drop database 数据库名;
drop database if exists 数据库名;

4.连接数据库

use 数据库名;

5.当前所使用的数据库信息

select database(); -- 查询当前所使用的数据库
select version(); -- 显示MYSQL的版本
select now(); -- 显示当前时间
select dayofmonth(current_date); -- 显示日
select month(current_date); -- 显示月
select year(current_date); -- 显示年
select "welecome to my blog!";  -- 显示字符串
select ((4 * 4) / 10 ) + 25; -- 当计算器用
select CONCAT(字段1, " ", 字段2)  AS Name  from 数据表  where title = '条件'; -- 串接字符串

6.备份数据库

导出文件默认是存在mysql\bin目录下
mysqldump -u 用户名 -p 数据库名 > 导出的文件名
mysqldump -u root -p123456 test > outfile_name.sql;

导出一个表
mysqldump -u 用户名 -p 数据库名 表名> 导出的文件名
mysqldump -u root -p test test > outfile_name.sql;

导出一个数据库结构
mysqldump -u 用户名 -p -d –add-drop-table 数据库名 > outfile_name.sql;
-d   ---  没有数据 
–add-drop-table  ---  在每个create语句之前增加一个drop table
例:mysqldump -u root -p -d –add-drop-table test > outfile_name.sql;

三、数据表操作

1.显示数据表

show tables;

desc `表名`; -- 查看表结构

2.创建数据表

create table <表名> ( <字段名1> <类型1> [,..<字段名n> <类型n>]);
例:
create table test(
> id int(4) not null primary key auto_increment,
> name char(20) not null,
> sex int(4) not null default '0';

3.删除表

drop table <表名>;

4.插入数据

insert into <表名> ( <字段名1>[,..<字段名n > ]) values ( 值1 )[, ( 值n )]\
例:
insert into test(id,name,sex) values(1,'Tom',1),(2,'Joan',0), (2,'Wang', 1);

5.更新数据

update test name='Mary' where id=1;

6.查询数据

select * from test order by id limit 0,2;

7.删除数据

delete from test where id=1;

8.添加字段

alter table 表名 add 字段名  属性;
例:alter table test add passtest int(4) default '0';

9.修改字段

alert table 表名  change 旧字段 新字段 属性;
例:alter table test change passtest  new_passtest  int(2) default '1';

10.删除字段

alert table 表名 drop 字段名;
例:alert table test drop name;

11.添加索引

添加普通索引
alter table 表名 add index 索引名 (字段名1[,字段名2 …]);
例:alter table test add index test_name (name);

添加主键索引
alter table 表名 add primary key (字段名);
例: alter table test add primary key(id);

添加唯一索引
alter table 表名 add unique 索引名 (字段名);
例:alter table test add unique test_name2(sex);

12.删除索引

删除普通索引,唯一索引
alter table 表名 drop index 索引名;
例:alter table test drop index test_name;

删除主键索引
alter table 表名 drop primary  key;
例:alter table test drop primary  key;

13.修改表名

rename table 表名 to 新表名;
例:rename table test to new_test;

当你执行 RENAME 时,你不能有任何锁定的表或活动的事务。你同样也必须有对原初表的 ALTER 和 DROP 权限,以及对新表的 CREATE 和 INSERT 权限。
RENAME TABLE 在 MySQL 3.23.23 中被加入。
上一篇 下一篇

猜你喜欢

热点阅读