什么是DML?对常用的一些SQL命令举例说明!

对mysql数据库中的数据进行操作的命令分为DDL和DML两种:

  • DDL命令:用于定义数据,管理数据库的组件,比如管理数据库,表,索引,视图,用户,存储过程等。命令有:CREATE、ALTER、DROP
  • DML命令:用于操纵数据,管理表中的数据,比如对数据进行增、删、改、查。命令有:INSERT, DELETE, UPDATE, SELECT

下面对一些常用的SQL操作举出实例:

[root@localhost ~]# mysql -uroot -p112233  #登录数据库

MariaDB [(none)]> SHOW DATABASES;  #查看数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| testdb             |
| wordpress          |
+--------------------+

MariaDB [mysd]> CREATE DATABASE hidb;  #创建数据库hidb

MariaDB [(none)]> use hidb;  #连接数据库

MariaDB [hidb]> CREATE TABLE tbl1 (id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE KEY,name CHAR(30) NOT NULL);
#创建一个tbl1表,两个字段,字段id长度为SMALLINT,没有符号,不能为空,自动增长,唯一键;定义字段name,长度30类型CHAR,不能为空

MariaDB [hidb]> ALTER TABLE tbl1 ADD gender ENUM('F','M') after id;
#往表tbl1添加枚举字段gender,添加在id后面;

MariaDB [hidb]> DESC tbl1;  #查看字段
+--------+----------------------+------+-----+---------+----------------+
| Field  | Type                 | Null | Key | Default | Extra          |
+--------+----------------------+------+-----+---------+----------------+
| id     | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| gender | enum('F','M')        | YES  |     | NULL    |                |
| name   | char(30)             | NO   |     | NULL    |                |
+--------+----------------------+------+-----+---------+----------------+

MariaDB [hidb]> ALTER TABLE tbl1 DROP gender;
#删除表tbl1中字段gender

MariaDB [hidb]> DESC tbl1;  #查看字段,gender被删除
+-------+----------------------+------+-----+---------+----------------+
| Field | Type                 | Null | Key | Default | Extra          |
+-------+----------------------+------+-----+---------+----------------+
| id    | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | char(30)             | NO   |     | NULL    |                |
+-------+----------------------+------+-----+---------+----------------+

MariaDB [hidb]> INSERT INTO tbl1 VALUES (1,'hualong'),(2,'tom'),(3,'jerry');
#增加三行数据

MariaDB [hidb]> SELECT * FROM tbl1;  #查看数据
+----+---------+
| id | name    |
+----+---------+
|  1 | hualong |
|  2 | tom     |
|  3 | jerry   |
+----+---------+

MariaDB [hidb]> UPDATE tbl1 SET name='TOM' WHERE id=2;
#修改id为的行中的name值

MariaDB [hidb]> SELECT * FROM tbl1;  #查看修改后的数据
+----+---------+
| id | name    |
+----+---------+
|  1 | hualong |
|  2 | TOM     |
|  3 | jerry   |
+----+---------+

MariaDB [hidb]> DELETE FROM tbl1 WHERE id=2;
#删除id为2的那一行

MariaDB [hidb]> SELECT * FROM tbl1;  #查看删除后的数据
+----+---------+
| id | name    |
+----+---------+
|  1 | hualong |
|  3 | jerry   |
+----+---------+

MariaDB [hidb]> DROP TABLE tbl1;  #删除hidb的表tbl1

MariaDB [hidb]> DROP DATABASE hidb;  #删除数据库hidb
MariaDB [hidb]> LOCK TABLE tbl1 read;
#在本终端上,锁住表tbl1为只读,其他终端操作这个表时,可以读取,但是运行写或加锁会阻塞

MariaDB [hidb]> UNLOCK TABLES;
#本终端解除表锁定

重新修改mysql登录密码

MariaDB [hidb]> SET PASSWORD FOR 'root'@'localhost'=PASSWORD('aabbcc');
#修改掉root的登录密码

[root@localhost ~]# systemctl stop mariadb  #停止mysql服务

[root@localhost ~]# vim /etc/my.cnf.d/server.cnf 
[mysqld]  #添加两句,登录mysql时跳过验证
skip-grant-tables
skip-networking

[root@localhost ~]# systemctl daemon-reload  #重新载入systemd

[root@localhost ~]# systemctl start mariadb

[root@localhost ~]# mysql  #输入命令直接登录数据库
MariaDB [(none)]> 

MariaDB [(none)]> UPDATE mysql.user SET password=PASSWORD('112233') WHERE user='root';
#重新设置管理员密码

[root@localhost ~]# systemctl stop mariadb
[root@localhost ~]# vim /etc/my.cnf.d/server.cnf 
#删除之前添加的两行
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start mariadb

[root@localhost ~]# mysql -uroot -p112233
#此时输入修改后的密码成功登录

你可能感兴趣的:(什么是DML?对常用的一些SQL命令举例说明!)