mysql基本操作及使用

mysql基本使用:
连接到mysql

如果MySQL是使用RPM包安装的就使用服务的启动方式

启动mysql服务器
[root@bogon wens]# service mysqld start
如果是在启动状态需要重启
[root@bogon wens]# service mysqld restart
关闭mysql服务器
service mysqld stop
查看服务器是否启动
[root@bogon wens]# ps -ef | grep mysqld
mysql      3261      1  0 20:48 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql      3427   3261  1 20:48 ?        00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root       3476   2798  0 20:48 pts/0    00:00:00 grep --color=auto mysqld
//代表启动成功
登陆服务器
[root@bogon wens]# mysql -u root -pls

Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.42 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

客户端和服务端都是进程

退出
mysql> quit
Bye
  • mysql 代表客户端的命令
  • -u 后面跟连接的数据库用户
  • -p 表示输入密码等

Enter passoprd 下的提示:

  • 命令结束符 使用/g 或者 ;
  • 客户端的连接ID, 记录了数据库的连接次数,每次一个新连接都会加一
  • 服务器的版本
  • \c 清除当前输入 \h 为帮助

sql分类

  1. DDL 语句: 数据定义语言,这些语句定义了不同的数据段,数据库,表, 列,索引等数据对象的定义 使用挂关键字有create drop alter等
  2. DML 语句: 数据操作语句,主要用于数据的增删改查修改的数据库记录的操作 关键字有 insert delete update select等
  3. DCL 语句: 数据控制语句,用于控制不同数段据的直接访问和访问权限的问题 关键字grant revoke commit

DDL语句

创建库

CRECTE DATABASE dbname

mysql> create database msg;
Query OK, 1 row affected (0.00 sec)
--分号结尾
查看已存在的库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| company1           |
| company2           |
| k1                 |
| msg2               |
| mysql              |
| performance_schema |
+--------------------+
7 rows in set (0.36 sec)

以下几个库是在安装MySQL时自动创建好的

  • information_schema : 主要存储了一些形同的数据库对象信息,比如用户表信息,列信息,权限信息,字符集信息,分区信息等,所维护的其他数据库的信息。
  • mysql : 存储了用户的权限信息等关于数据库用户的信息
  • performance_schema: 用于收集数据库服务器的性能参数,运行记录
  • test : 用户可以进行测试的数据库,是一个空的数据库
使用库

选中一个要操作的数据库

USE dbname

mysql> use msg
Database changed
查看当前库
select database();
查看数据库的存储引擎
SHOW ENGKINES\G;
// myisam vs innodb
查看帮助文档
mysql> help create database;
Name: 'CREATE DATABASE'
Description:
Syntax:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
    [create_specification] ...

create_specification:
    [DEFAULT] CHARACTER SET [=] charset_name
  | [DEFAULT] COLLATE [=] collation_name

CREATE DATABASE creates a database with the given name. To use this
statement, you need the CREATE privilege for the database. CREATE
SCHEMA is a synonym for CREATE DATABASE.

URL: http://dev.mysql.com/doc/refman/5.6/en/create-database.html

删除数据库

drop database dbname;

mysql> drop database k1;
Query OK, 1 row affected (0.09 sec)
创建表

CREATE TABLE tablename( )

create table teacher(
    name varchar(32),--姓名
    age int 
)CHEAST utf8;

查看库中存在的表
mysql> show tables;
+---------------+
| Tables_in_msg |
+---------------+
| teacher       |
+---------------+
1 row in set (0.00 sec)

删除表

DROP TABLE tablename

mysql> drop table t1;
Query OK, 0 rows affected (0.06 sec)
修改表
修改表的类型

ALTER TABLE tablename MONDIFY[CONLUMN] column_definition[FIRST | AFTER col_name]

mysql> desc teacher;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(32) | YES  |     | NULL    |       |
| age   | int(11)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)


mysql> alter table teacher modify name varchar(20);
Query OK, 1 row affected (0.32 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> desc teacher;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(20) | YES  |     | NULL    |       |
| age   | int(11)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

增加表的字段

ALTER TABLE tablename ADD [COLUMN] column_definition [FIRST | AFTER col_name]

  • [FIRST | AFTER col_name] 表示字段的位置,ADD默认将字段添加在最后的位置
mysql> alter table teacher add column jobid int(4);
Query OK, 0 rows affected (0.14 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc teacher;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(20) | YES  |     | NULL    |       |
| age   | int(11)     | YES  |     | NULL    |       |
| jobid | int(4)      | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

删除表的字段

ALTER TABLE tablename DROP [COLUMN] col_name

mysql> alter table teacher drop age;
Query OK, 0 rows affected (0.40 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc teacher;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(20) | YES  |     | NULL    |       |
| jobid | int(4)      | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
更改字段的名称

ALTER TABLE tablename CHANGE [COLUMN] old_col_name column_definition
[FIRST|AFTER col_name]

//同时修改字段的类型
mysql> alter table teacher change name myname varchar(21);
Query OK, 1 row affected (0.11 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> desc teacher;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| myname | varchar(21) | YES  |     | NULL    |       |
| jobid  | int(4)      | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

  • change 与 modify 不会修改字段的内容
将字段按顺序排列

将新字段插在myname 后面

mysql> alter table teacher add newage int(5) after myname;
Query OK, 0 rows affected (0.35 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc teacher;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| myname | varchar(21) | YES  |     | NULL    |       |
| newage | int(5)      | YES  |     | NULL    |       |
| jobid  | int(4)      | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

添加字段将其放在最前面

mysql> alter table teacher add test int(2) first;
Query OK, 0 rows affected (0.39 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc teacher;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| test   | int(2)      | YES  |     | NULL    |       |
| myname | varchar(21) | YES  |     | NULL    |       |
| newage | int(5)      | YES  |     | NULL    |       |
| jobid  | int(4)      | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

修改当前字段的位置

mysql> alter table teacher modify jobid int(4) first;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc teacher;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| jobid  | int(4)      | YES  |     | NULL    |       |
| test   | int(2)      | YES  |     | NULL    |       |
| myname | varchar(21) | YES  |     | NULL    |       |
| newage | int(5)      | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

修改表名

ALTER TABLE tablename RENAME [TO] new_tablename

mysql> alter table teacher rename new_teacher;
Query OK, 0 rows affected (0.01 sec)


mysql> desc new_teacher;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| jobid  | int(4)      | YES  |     | NULL    |       |
| test   | int(2)      | YES  |     | NULL    |       |
| myname | varchar(21) | YES  |     | NULL    |       |
| newage | int(5)      | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

DML 语句

向表中插入数据

INSERT INTO tablename (field1,field2,……fieldn) VALUES(value1,value2,……valuesn);

  • 可以指定字段名
mysql> insert into new_teacher(jobid, test, myname, newage) values(3, 5, 'xiao hong', 9);
Query OK, 1 row affected (0.00 sec)
  • 也可以不指定字段名
mysql> insert into new_teacher values(4, 2, 'xia ming', 9);
Query OK, 1 row affected (0.28 sec)
  • 只对某些字段显示插入
mysql> insert into new_teacher(jobid,myname) values(6, 'xiao zhang');
Query OK, 1 row affected (0.00 sec)
  • 可以一次插入多条数据
  • 中间用,分隔开
mysql> insert into new_teacher values(2, 5, 'haha', 8), 
    -> (4, 7, 'hehe', 9),
    -> (9, 5, 'heihei', 3);
Query OK, 3 rows affected (0.04 sec)
Records: 3  Duplicates: 0  Warnings: 0

  • 查看插入结果
mysql> select * from new_teacher;
+-------+------+------------+--------+
| jobid | test | myname     | newage |
+-------+------+------------+--------+
|  NULL | NULL | ahsj       |   NULL |
|     4 |    2 | xia ming   |      9 |
|     3 |    5 | xiao hong  |      9 |
|     6 | NULL | xiao zhang |   NULL |
|     2 |    5 | haha       |      8 |
|     4 |    7 | hehe       |      9 |
|     9 |    5 | heihei     |      3 |
+-------+------+------------+--------+
7 rows in set (0.00 sec)

更新记录

使用UPDATE,命令更新记录值

  • 更新一个表的值

UPDATE tablename SET field1=value1,field2.=value2,……fieldn=valuen [WHERE CONDITION]

mysql> update new_teacher set newage = 100 where myname = 'haha';
Query OK, 1 row affected (0.06 sec)
Rows matched: 1  Changed: 1  Warnings: 0


mysql> select * from new_teacher;
+-------+------+------------+--------+
| jobid | test | myname     | newage |
+-------+------+------------+--------+
|  NULL | NULL | ahsj       |   NULL |
|     4 |    2 | xia ming   |      9 |
|     3 |    5 | xiao hong  |      9 |
|     6 | NULL | xiao zhang |   NULL |
|     2 |    5 | haha       |    100 |
|     4 |    7 | hehe       |      9 |
|     9 |    5 | heihei     |      3 |
+-------+------+------------+--------+
7 rows in set (0.00 sec)

  • 多个表中的值进行同时更新

UPDATE t1,t2…tn set t1.field1=expr1,tn.fieldn=exprn [WHERE CONDITION]

mysql> select * from teacher1;
+------+------+-------+
| name | age  | jobid |
+------+------+-------+
| haha |   14 |   124 |
| hehe |   21 |     6 |
+------+------+-------+
2 rows in set (0.00 sec)

mysql> select * from teacher2;
+------------+------+-------+
| name       | age  | jobid |
+------------+------+-------+
| xiao zhang |   13 |     4 |
| liu han    |  100 |     6 |
+------------+------+-------+
2 rows in set (0.00 sec)

mysql> update teacher1, teacher2 set teacher1.name = 'liu han' , teacher2.age = teacher1.age where teacher1.jobid = teacher2.jobid;
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> select * from teacher1;
+---------+------+-------+
| name    | age  | jobid |
+---------+------+-------+
| haha    |   14 |   124 |
| liu han |   21 |     6 |
+---------+------+-------+
2 rows in set (0.00 sec)

mysql> select * from teacher2;
+------------+------+-------+
| name       | age  | jobid |
+------------+------+-------+
| xiao zhang |   13 |     4 |
| liu han    |   21 |     6 |
+------------+------+-------+
2 rows in set (0.00 sec)

删除记录

DELETE FROM tablename [WHERE CONDITION]

  • 根据字段删除
mysql> delete from teacher1 where name = 'haha';
Query OK, 1 row affected (0.00 sec)

mysql> select * from teacher1;
+---------+------+-------+
| name    | age  | jobid |
+---------+------+-------+
| liu han |   21 |     6 |
+---------+------+-------+
1 row in set (0.00 sec)

  • 可以删除多个表的信息

DELETE t1,t2…tn FROM t1,t2…tn [WHERE CONDITION]

  • 可以使用别名,但前后必须要一致都要使用别名
mysql> delete a, b  from teacher1 a, teacher2 b where a.jobid = b.jobid ;
Query OK, 2 rows affected (0.09 sec)

mysql> select * from teacher1;
+---------+------+-------+
| name    | age  | jobid |
+---------+------+-------+
| liu han |   21 |     6 |
+---------+------+-------+
1 row in set (0.00 sec)

mysql> select * from teacher2;
+------------+------+-------+
| name       | age  | jobid |
+------------+------+-------+
| xiao zhang |   13 |     4 |
| liu han    |   21 |     6 |
+------------+------+-------+
2 rows in set (0.28 sec)



mysql> select * from teacher1;
Empty set (0.00 sec)

mysql> select * from teacher2;
+------------+------+-------+
| name       | age  | jobid |
+------------+------+-------+
| xiao zhang |   13 |     4 |
+------------+------+-------+
1 row in set (0.00 sec)
  • 如果不加where 会将表中的所有信息都删除完
[root@bogon mysql]# vim /etc/my.cnf
//查看MySQL创建的文件
datadir=/var/lib/mysql   //目录文件

你可能感兴趣的:(mysql)