DOS下进入
1、通过程序中的mySQL的:MySQL 5.6 Command Line Client直接进入mySQL的命令行;
2、或者通过WIn+R--》输入cmd,然后C:等一层一层找到安装MYSQL的的文件夹运行mysql.exe可执行文件来运行mySQL
输入mySQL的连界密码,然后回车即可进入。
直接输入:?会提示好多命令的方式。
①首先 新建一个数据库、 新建表 及 设置主键:
create database testdatabase;(创建数据库)
use testdatabase; (使用该数据库)
mysql> create table dept
-> (
-> deptno int primary key,(设置主键)
-> dename varchar(14),
-> loc varchar(13)
-> );
提示:Query OK, 0 rows affected (1.64 sec)说明创建数据库成功;
②通过运行sql文件来新建表及设置外键:
在桌面新建数据库sql文件:emp.sql 如下:
create table emp
(
empno int primary key,
ename varchar(10),
job varchar(10),
mgr int,
hiredate datetime,
sal double,
comm double,
deptno int,
foreign key (deptno) references dept(deptno) (设置外键)
);
然后在命令行输入:
mysql> \. C:\Users\BLMZ\Desktop\emp.sql
提示:
Query OK, 0 rows affected (0.21 sec)
③查看表、查看表结构
mysql> show tables;(查看表)
+-------------------+
| Tables_in_mybatis |
+-------------------+
| dept |
| emp |
| t_user |
+-------------------+
3 rows in set (0.00 sec)
mysql> desc emp;(查看表结构)
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| empno | int(11) | NO | PRI | NULL | |
| ename | varchar(10) | YES | | NULL | |
| job | varchar(10) | YES | | NULL | |
| mgr | int(11) | YES | | NULL | |
| hiredate | datetime | YES | | NULL | |
| sal | double | YES | | NULL | |
| comm | double | YES | | NULL | |
| deptno | int(11) | YES | MUL | NULL | |
+----------+-------------+------+-----+---------+-------+
8 rows in set (0.10 sec)
④查看数据及分页查看数据(分页技术)
mysql> select * from dept;
+--------+--------+------+
| deptno | dename | loc |
+--------+--------+------+
| 1 | b | c |
| 10 | b | c |
| 12 | b | c |
| 14 | b | c |
| 16 | b | c |
| 20 | b | c |
| 160 | b | c |
+--------+--------+------+
7 rows in set (0.00 sec)
mysql> select * from dept order by deptno desc limit 3, 3;
查看:倒序从倒数第三条开始的三条数据
+--------+--------+------+
| deptno | dename | loc |
+--------+--------+------+
| 14 | b | c |
| 12 | b | c |
| 10 | b | c |
+--------+--------+------+
3 rows in set (0.00 sec)
mysql> select * from dept order by deptno asc limit 3, 3;
+--------+--------+------+
| deptno | dename | loc |
+--------+--------+------+
| 14 | b | c |
| 16 | b | c |
| 20 | b | c |
+--------+--------+------+
3 rows in set (0.00 sec)
查看:正序从第3条开始的3条数据
⑤删除数据及 设置id自增
delete * from dept where id=16;
mysql> create table article
-> (
-> id int primary key auto_increment,(自动递增)
-> title varchar(10)
-> );
Query OK, 0 rows affected (0.14 sec)
mysql> insert into article values(null,‘a‘);(插入数据的时候设置为null)不设置值得去设置为null;
Query OK, 1 row affected (0.03 sec)
mysql> select * from article;
+----+-------+
| id | title |
+----+-------+
| 1 | a |
+----+-------+
1 row in set (0.00 sec)