mysql客户端工具
一、登录数据库
在此就不多叙述了,mysql安装教程
二、输入查询
这是一个简单的命令,要求服务器告诉它的版本号和当前日期。在mysql>提示输入如下命令并按回车键:
查询mysql版本号,当前日期 select version(),current_date;
* mysql> SELECT
-> USER()
-> ,
-> CURRENT_DATE;
+---------------+--------------+
| USER() | CURRENT_DATE |
+---------------+--------------+
| jon@localhost | 2005-10-11 |
+---------------+--------------+
如果你决定不想执行正在输入过程中的一个命令,输入/c取消它:
* mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
三、创建并使用数据库
使用SHOW语句找出服务器上当前存在什么数据库:
显示数据库 show databases;
* mysql> use test
Database changed
创建数据库并使用 create database student; use student
* mysql> use student
Database changed
数据库只需要创建一次,但是必须在每次启动mysql会话时在使用前先选择它。你可以根据上面的例子执行一个USE语句来实现。还可以在调用mysql时,通过命令行选择数据库,只需要在提供连接参数之后指定数据库名称。例如:
* mysql> show tables;
Empty set (0.00 sec)
***
创建表
使用一个CREATE TABLE语句指定你的数据库表的布局:
* mysql> describe student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| birth | date | YES | | NULL | |
| death | date | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
插入一条数据
mysql> select * from student;
+------+------+------+------------+-------+
| name | age | sex | birth | death |
+------+------+------+------------+-------+
| hsy | 18 | f | 1999-09-09 | NULL |
+------+------+------+------------+-------+
1 row in set (0.00 sec)
选择特殊行
mysql> select * from student where birth>'1999-1-1'and sex='f';
+------+------+------+------------+-------+
| name | age | sex | birth | death |
+------+------+------+------------+-------+
| hsy | 18 | f | 1999-09-09 | NULL |
+------+------+------+------------+-------+
1 row in set (0.00 sec)
选择特殊列
mysql> select distinct name from student;
+------+
| name |
+------+
| hsy |
+------+
1 row in set (0.00 sec)
分类行
mysql> select name ,birth, curdate(),
-> (year(curdate())-year(birth))-(right(curdate(),5)(birth,5)) as age
-> from student;
+------+------------+------------+------+
| name | birth | curdate() | age |
+------+------------+------------+------+
| hsy | 1999-09-09 | 2017-06-07 | 17 |
| hsy | 1999-09-09 | 2017-06-07 | 17 |
| hsy | 1989-09-09 | 2017-06-07 | 27 |
| hh | 1994-03-04 | 2017-06-07 | 23 |
| hh | 1994-03-04 | 2017-06-07 | 23 |
| hh | 1994-03-04 | 2017-06-07 | 23 |
| hh | 1994-03-04 | 2017-06-07 | 23 |
| hh | 1997-03-04 | 2017-06-07 | 20 |
| hh | 1997-03-04 | 2017-06-07 | 20 |
| hrr | 1997-03-04 | 2017-06-07 | 20 |