MySql入门

基本语法

下面所有的语法都是在终端下操作:

连接mysql

mysql -uroot -p
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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.

其中root为用户名,回车过后会提示输入密码,在安装mysql的时候,会提示保存root账户的密码。

创建数据库

mysql> create database test;
Query OK, 1 row affected (0.01 sec)

查看所有数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

切换到指定数据库

mysql> use test;
Database changed

创建表

mysql> create table table_user(
       id int(11) primary key not null auto_increment,
       username varchar(32) not null,
       password varchar(32) not null
       );
Query OK, 0 rows affected (0.03 sec)

查看当前数据库所有表

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| table_user     |
+----------------+
1 row in set (0.00 sec)

查看表描述

mysql> desc table_user;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| username | varchar(32) | NO   |     |         |                |
| password | varchar(32) | NO   |     |         |                |
+----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

插入数据

mysql> insert into table_user(username,password) values('18824910915','123456'); 
Query OK, 1 row affected (0.00 sec)

因为一开始设定的表id为自增长,所以在设置指定列usernamepassword的值就可以插入成功了。

查询数据

mysql> select * from table_user;
+----+-------------+----------+
| id | username    | password |
+----+-------------+----------+
|  1 | 18824910915 | 123456   |
+----+-------------+----------+
1 row in set (0.00 sec)

更新数据

mysql> update table_user 
       set password = 'qwerty'
       where username = '18824910915'; 
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

删除数据

mysql> delete from table_user 
       where username = '18824910915';
Query OK, 1 row affected (0.00 sec)

删除数据后,下次添加数据表的id字段并不会往前移一位,也就是说再次插入数据后 id = 2

    原文作者:SQL
    原文地址: https://juejin.im/entry/5a323e106fb9a0451543e76f
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞