目录
一、MysQl基本样式
1、mysql忽略大小写
2、Sql语句以;结束
3、Sql语句固定的句式单词默认大写
二、数据库操作 database
1.创建数据库
CREATE DATABASE 数据库名字 charset=utf8;
2.查看数据库
SHOW DATABASES;
3.选择数据库
此句结尾不要加英文分号 ;
USE 数据库名字
二、表格操作 table
1.查看表
SHOW TABLES;
2.创建表
CREATE TABLE 表名(id INT PRIMARY KEY AUTO_INCREMENT,name CHAR(30),age INT);
随便写个案例:
create table student(id primary key auto_increment,name char(30),age int);
其中:
Create:创建
Table 创建的类型
Student 表的名称
Id 字段名
Int 整型数
Primary key 主键
Auto_increment 自增长
3.删除表
DROP TABLE 表名;
drop table student;
4.查看表结构
DESC 表名;
desc student;
5.查询
(1)查询当前表中的所有字段的数据
select * from student;
(2)查询当前表中的id,name,age 字段的所有数据
select id,name,age from student;
(3)查询当前的id,name,age,字段的所有数据,并以id正序排列
select id,name,age from student order by id;
(4)查询当前表中type值为"t"的id,name,age字段的所有数据,并以id正序排列
select id,name,age from student where type="t" order by id;
(5)查询当前表中的所有数据,并以id倒序排列
select id,name,age from student order by id desc;
(6)查询当前表中从第二条开始,id,name,age字段的两条数据
select id,name,age from student limit 1,2;
举例:
对表名为person的表中字段进行limit正序查询
mysql> select name from person order by id limit 1,2;
结果是:
mysql> select name from person order by id limit 2,2;
结果是:
(7)查询当前表中从第三条开始,id,name,age字段的两条数据
select id,name,age from student limit 2,2;
(8)倒序查询
Select name from persion where type = “s” order by id desc limit 2,1
查询所有类型为s的数据,并以id倒序排列,查2条
mysql> select name from person where type="t" order by id desc;
mysql> select name from person where type="t" order by id desc;
+------+
| name |
+------+
| 小红 |
| 小明 |
+------+
2 rows in set (0.00 sec)
mysql> select name from person where type="t" order by id desc limit 1,2;
+------+
| name |
+------+
| 小明 |
+------+
1 row in set (0.00 sec)