尚硅谷之MySQL基础

第5章 简单的SQL示例

1、示例小demo

查看当前的MySQL服务器中有哪些数据库

mysql> SHOW DATABASES;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| test               |

+--------------------+

4 rows in set (0.05 sec)

使用test数据库

mysql> USE test;

Database changed

创建表格

mysql> CREATE TABLE t_stu(

    ->  sid INT,

    ->  sname VARCHAR(100),

    ->  gender CHAR

    -> );

Query OK, 0 rows affected (0.14 sec)

查看表结构

mysql> DESC t_stu;

+--------+--------------+------+-----+---------+-------+

| Field  | Type         | Null | Key | Default | Extra |

+--------+--------------+------+-----+---------+-------+

| sid    | int(11)      | YES  |     | NULL    |       |

| sname  | varchar(100) | YES  |     | NULL    |       |

| gender | char(1)      | YES  |     | NULL    |       |

+--------+--------------+------+-----+---------+-------+

3 rows in set (0.03 sec)

插入记录

mysql> INSERT INTO t_stu VALUES(1,'张三','男');

Query OK, 1 row affected (0.06 sec)

 

mysql> INSERT INTO t_stu VALUES(2,'李四','男');

Query OK, 1 row affected (0.09 sec)

 

mysql> INSERT INTO t_stu VALUES(3,'王五','男');

Query OK, 1 row affected (0.06 sec)

查看记录

mysql> SELECT * FROM t_stu;

+------+-------+--------+

| sid  | sname | gender |

+------+-------+--------+

|    1 | 张三      | 男       |

|    2 | 李四      | 男       |

|    3 | 王五      | 男       |

+------+-------+--------+

3 rows in set (0.00 sec)

修改记录

mysql> UPDATE t_stu SET sname = '张三丰' WHERE sid = 1;

Query OK, 1 row affected (0.08 sec)

Rows matched: 1  Changed: 1  Warnings: 0

 

mysql> SELECT * FROM t_stu;

+------+--------+--------+

| sid  | sname  | gender |

+------+--------+--------+

|    1 | 张三丰       | 男       |

|    2 | 李四       | 男       |

|    3 | 王五       | 男       |

+------+--------+--------+

3 rows in set (0.00 sec)

删除记录

mysql> DELETE FROM t_stu WHERE sid = 1;

Query OK, 1 row affected (0.08 sec)

 

mysql> SELECT * FROM t_stu;

+------+-------+--------+

| sid  | sname | gender |

+------+-------+--------+

|    2 | 李四      | 男       |

|    3 | 王五      | 男       |

+------+-------+--------+

2 rows in set (0.00 sec)