Mysql basic data operations (add, delete, change, search)

First, connect to the database

1. 游客登录(不一定能登入),登入了啥都不能干
>:mysql

2.账号密码登录
>:mysql -u root -p
再次输入密码,没有任何提示,没有密码直接回车

3.连接指定服务器的mysql
>:mysql -h ip地址 -P端口号 -u账号 -p
回车后敲入密码
eg:
>:mysql -hlocalhost -P3306 -uroot -p
    
4.退出数据库
>:quit
>:exit

Second, the user to view information

1.查看当前登录的用户
mysql>: select user();

2.root权限下可以查看所有用户信息:
mysql>: select * from mysql.user;
mysql>: select * from mysql.user \G
mysql>: select user,password,host from mysql.user;
    
3.root登录下,删除游客(操作后需要重启mysql服务)
mysql>: delete from mysql.user where user='';

4.root登录下,修改密码(操作后要重启mysql服务)
mysql>: update mysql.user set password=password('12345678') where user='用户名' host='localhost';

5.没有登录的状态下,去修改密码
>: mysqladmin -u用户名 -p旧密码 -h域名 password "新密码"
eg>: mysqladmin -uroot -p12345678 -hlocalhost password "root"

6.root登录下,创建用户
mysql>: create user'用户名'@'%' identified by '密码';
eg>: create user'cxk'@'localhost' identified by '123456';
    %:代表匹配所有主机,设置成‘localhost’,代表只能本地访问,例如root账户默认为‘localhost’

7.root登录下,授予用户权限:
mysql>: grant all on *.* to tomjoy@localhost identified by '123456';
    注: all代表所有的权限, *.*代表所有的库都支持该权限,也可以指定库,比如只允许        tset库有这些权限,就写成 test.*

Third, the basic operation of the database

1.查看已有数据库
mysql>: show databases;

2.选择某个数据库
mysql>:use 数据库名

3.查看当前所在数据库
mysql>: select database();

4.创建数据库
mysql>: create database 数据库名 [charset=编码格式]; 中括号里编码格式可以省略
eg>: create database cxk;
eg>: create database kobe charset=utf8;

5.查看创建数据库的详细内容
mysql>:show create database 数据库名;

6.删除数据库
mysql>:drop database 数据库名;

Fourth, the basic operation of the table

前提: 先选择要操作的数据库   use 库名;

1.查看已有表
mysql>:show tables;

2.创建表
mysql>:create table 表名(字段们);
eg>: create  table student(name varchar(16),age int);
eg>: create  table teacher(name varchar(16),age int);

3.查看创建表的sql
mysql>:show create table 表名;
eg>: show create table student;
    
4.查看创建表的结构
mysql>:desc 表名;

5.删除表
mysql>:drop table 表名
eg>: drop table teacher;

Fifth, the basic operation for recording

1.查看某个数据库中的某个表的所有记录,如果在对应数据库中,可以直接找表
mysql>:select * from [数据库名.]表名;
eg>: select * from student;
    注: *代表查询所有字段

2.给表的所有字段插入数据  
mysql>:insert [into] [数据库名].表名 values(值1,...,值n);
eg>:如果给有name和age字段的student表插入数据
1条>: insert into student values('cxk',38);
多条>: insert into student values('悟空',78),('八戒',69);
指定库>: insert stu.student values('周',38),('琦',48);

3.根据条件修改指定内容
mysql>:update [数据库名.]表名 set 字段1=新值1,字段n=新值n where 字段=旧值;
eg>: update student set name='渣锴',age='100' where name = '琦'; 
注: 1)可以只修改部分字段  2) 没有条件下,所有记录都会被更新
eg>: update student set age='38' where age='100';

4.根据条件删除记录
mysql>: delete from [数据库名.]表名 where 条件;
eg>: delete from student where name='周';
eg>: delete from student where age<38;

Guess you like

Origin www.cnblogs.com/guapitomjoy/p/11569208.html