MySQL相关指令: 数据库排序规则:常用utf8_general_ci
1cmd连接数据库:
1.版本查看:mysqladmin --version
2.连接root:mysql –u root –p,输入密码
2查询数据库:show databases;
3查询数据表:show tables;
4创建数据库:create database 库名;
5删除数据库:drop database 库名;
6选择数据库:use 库名;
7显示当前数据库和版本信息以及连接用户名:select version(),database(),,user();
8创建数据表:create table 表名;
常用示例:create table 表名( id int(4) not null primary key auto_increment, name char(20) not null, sex int(4) not null default '0', degree double(16,2));
9删除数据表:drop table 表名;
10插入数据:INSERT INTO 表名 (class_id, name, gender, score) VALUES
(1, '大宝', 'M', 87), (2, '二宝', 'M', 81);
11更新数据:UPDATE 表名 SET name=’小牛’,gender=女 WHERE id>=5 AND id<=7;
12删除数据:DELETE FROM 表名 WHERE id=5;
13增加修改数据:ALTER TABLE 表名 ADD 字段名 字段类型;
ALTER TABLE表名CHANGE旧字段名 新字段名 字段类型;
14删除字段:ALTER TABLE表名DROP字段名;
15查询数据:
a.查询表中所有数据:SELECT * FROM 表名;
b.排序:SELECT id, name/字段名FROM表名ORDER BY score DESC/倒序大-小, gender;先score,score相同在 genter
c.待条件排序:
SELECT id, name, gender, score
FROM students
WHERE class_id = 1
ORDER BY score DESC;
e.各个班级的学生人数:SELECT class_id, COUNT(*) num FROM students GROUP BY class_id;
f.多表查询:
SELECT
s.id sid,s.name,s.gender,s.score,c.id cid,c.name cname
FROM students s, classes c
WHERE s.gender = 'M' AND c.id = 1;
g.连表查询:
SELECT s.id, s.name, s.class_id, c.name class_name, s.gender, s.score
FROM students s
INNER JOIN/ RIGHT OUTER JOIN/ LEFT OUTER JOIN/ FULL OUTER JOIN classes c
ON s.class_id = c.id;