建立数据库
CREATE DATABASE db1 CHARACTER set utf8;
查看数据库
show databases;
删除数据库
DROP DATABASE db_name
使用数据库
USE db_name
登陆MySql用户
登陆远程mysql server的方式mysql -u user_name -p
mysql -h IP地址 -u 用户名 -p
设置用户编码集
set names utf8;
创建表
CREATE TABLE table1 (name varchar(100),sex varchar(10), age int, class varchar(100));
CREATE TABLE table1 (name varchar(100),sex varchar(10), age int, class varchar(100));
显示表
show tables
看表结构
desc table_name;
插入数据
INSERT INTO table1 (name, sex, age, class) VALUES ('张三', '男', 20, 'c++就业班');
INSERT INTO table2 (name, age) VALUES ('李德华', 40); // ID是自动生成、
查询语句
SELECT * FROM table1;
SELECT NAME FROM table1;
通过建立脚本来插入数据
1、先通过txt写出添加语句如下
INSERT INTO table1 (name, sex, age, class) VALUES('小王', '女', 22, 'C++班');
INSERT INTO table1 (name, sex, age, class) VALUES('小李', '男', 25, 'Jave班');
INSERT INTO table1 (name, sex, age, class) VALUES('小张', '男', 25, 'Ios班');
INSERT INTO table1 (name, sex, age, class) VALUES('小红', '女', 21, 'C++班');
INSERT INTO table1 (name, sex, age, class) VALUES('小黑', '女', 35, 'Linux班');
INSERT INTO table1 (name, sex, age, class) VALUES('小明', '女', 23, 'C++班');
保存为my.sql,保存的编码格式为utf8
2、将my.sql上传到Linux上去
3、登陆MySql用户
mysql -u dbuser1 -p
使用数据库
use db1
设置用户登陆编码格式
set names utf8;// 这个每次登陆都需要进行设置
运行脚本
source my.sql
查看验证
select * from table1;
特殊查询语句
SELECT * FROM table1 LIMIT m,n; // m代表从第几行开始,n代表显示多少行
带有条件的查询
查询年龄大于21岁的同学
SELECT * FROM table1 WHERE age > 21;
只查找C++班
SELECT * FROM table1 WHERE class = 'C++班';
查C++班而且年龄大于22
SELECT * FROM table1 WHERE class = 'C++班' AND age > 22;
查年C++班龄等于25的同学
SELECT * FROM table1 WHERE class = 'C++班' AND age = 25;
查C++班年龄不等于25的同学
SELECT * FROM table1 WHERE class = 'C++班' AND age <> 25;
查找C++班或者Ios班
SELECT * FROM table1 WHERE class = 'C++班' OR class = 'Ios班';
通过‘%’统配符匹配指定模式,找出班级里小字开头的记录
SELECT * FROM table1 WHERE name like '小%';
查询在集合里面的记录
SELECT * FROM table1 WHERE age in (20, 25);
等同于SELECT * FROM table1 WHERE age = 20 or age = 25;
查询不在集合里面的
SELECT * FROM table1 WHERE age NOT IN (20, 25);
SELECT * FROM table1 WHERE name not like '小%';
查询不等于
SELECT * FROM table1 WHERE age <> 25;
修改查询结果的列别名
select name 姓名, sex 性别, age 年龄 from table1;
修改查询结果的表别名
select a.name, a.sex from table1 a;
聚合函数
SUM([DISTINCT] FIELDNAME)求指定列之和[DISTINCT] 选项表示剔除重复记录
例如:select sum(age) from table1;
select sum(distinct age) from table1;// 过滤掉重复的值
MAX([DISTINCT] FIELDNAME)求指定列最大值
例如:select max(age) from table1;
select name from table1 where age = (select max(age) from table1);
下面的写法是错误的
错误: select name, max(age) from table1;
MIN([DISTINCT] FIELDNAME)求指定列最小值
例如:select min(age) from table1;
select name from table1 where age = (select min(age) from table1);
COUNT([DISTINCT] FIELDNAME)求有多少条记录,指定列记录总数
select count(*) from table1;
select count(distinct age) from table1;// 有多少条年龄不重复的记录
select count(name) from table1 where name like '小%';// 查找有多少条姓小的记录
select count(name) from table1 where name not like '小%';// 查找有多少条不姓小的记录
AVG([DISTINCT] FIELDNAME) 指定列平均值
select avg(age) from table1;// 查找平均年龄
select avg(age) from table1 where sex = '男';// 查找男同学平均年龄
聚合函数往往和GROUP BY结合用
select count(class) from table1 group by class;// 按班级分组求每组中的记录
select max(age) from table1 group by class;// 求每个班的年龄最大的记录
ORDE BY fieldname [DESC]
多表查询select * from table1 order by age;// 从小到大排列
select * from table1 order by age desc;// 从大到小排列
select * from table1 order by age, name;// 年龄相同时候按照名称排
select * from table1 order by age, name desc;// 年龄相同时候按照名称从大到小排
select * from table1, table2;// 这个简单的将两个表联合到一块,一般不用,这里只做练习看一下
select * from table1, table2 where table1.age = table2.age;// 查找表一和表二的年龄相同的记录
select * from table1, table3 where table1.class = table3.class and table3.teacher = '阿凡提';// 联合查找表一和表二,阿凡提老师的学生
select max(c.age) from table1 c, table3 d where c.class = d.class and d.teacher = '阿凡提';// 查找阿凡提老师班里面学生年龄最大的是多少
//查找苍老师班里面年龄最大同学的名字
select a.name from table1 a, table3 b where a.class = b.class and b.teacher = '苍老师'
and a.age = (select max(c.age) from table1 c, table3 d where c.class = d.class and d.teacher = '苍老师');
索引 CREATE INDEX在表上创建一个简单的索引。允许使用重复的值CREATE INDEX index_name ON table_name (column_name)
注释:对于SELECT后面的WHERE子句中用到的字段,原则是必须建立相应的索引。但是索引不要乱建,要精准,不能多,因为索引虽然可以提高SELECT
语句的速度,但代价是减低了INSERT,UPDATE和DELETE语句的速度。
注释:“column_name”规定需要索引的列
CREATE INDEX table1_age ON table1 (age);
在select语句中where查询用到哪个字段,这个字段就必须建立索引
唯一索引CREATE UNIQUE INDEX,其他和上面一样
唯一索引的查询效率高于普通索引
建立表的时候PRIMARY KEY (ID))语句相当于为ID字段建立了一个唯一索引
修改数据库语句,SET字句指定要修改的字段和所赋的值,WHERE子句指定要修改的行,如果没有WHERE子句,代表修改所有行
UPDATE TABLENAME SET CAL1 = VAL1, CAL2 = VAL2,....... WHERE CONDITIONAL
例:update table1 set sex = '男' where name = '小黑';
update table1 set sex = '女', age = 100 where name = '饭岛爱';
删除数据语句
delete from table1 where name = '小红';
/usr/include是GCC查找头文件的默认路径
mysql编程首先要做的工作
1、头文件#include <mysql/mysql.h>
2、makefile文件,链接的时候添加-lmysqlclient选项,意思是要链接到libmysql.so这个库文件
数据库client与server之间也是采用TCP协议
1、初始化client
2、建立连接
3、client想server发送SQL语句,server将执行SQL语句的结果返回给client
4、断开连接
写网络程序的原则,第一次写的时候,一定要拿本机做实验
"localhost" = "127.0.0.1"
编写mysql程序,成功连接到server之后,第一个执行的语句是设置字符集编码SET NAMES utf8;
练习题
有表结构如下:
CREATE TABLE students
(name varchar(20),/*姓名*/
age int,/*年龄*/
sex varchar(10),/*性别*/
classid int);/*与classes表的classid对应*/
CREATE TABLE classes
(classid int,/*班级ID,与students表中的classid对应*/
name varchar(20),/*班级名称*/
teacher varchar(20));/*班级老师*/
INSERT INTO students (name, age, sex, classid) VALUES ('刘德华', 25, '男', 1);
INSERT INTO students (name, age, sex, classid) VALUES ('张惠妹', 30, '女', 1);
INSERT INTO students (name, age, sex, classid) VALUES ('马艳丽', 24, '女', 2);
INSERT INTO students (name, age, sex, classid) VALUES ('苍井空', 26, '女', 2);
INSERT INTO students (name, age, sex, classid) VALUES ('萧敬腾', 21, '男', 1);
INSERT INTO students (name, age, sex, classid) VALUES ('罗志祥', 22, '男', 3);
INSERT INTO students (name, age, sex, classid) VALUES ('饭岛爱', 23, '女', 3);
INSERT INTO students (name, age, sex, classid) VALUES ('周润发', 25, '男', 3);
INSERT INTO students (name, age, sex, classid) VALUES ('章子怡', 20, '女', 2);
INSERT INTO students (name, age, sex, classid) VALUES ('陈冠希', 22, '男', 1);
INSERT INTO classes (classid, name, teacher) VALUES (1, 'C++就业班', '王老师');
INSERT INTO classes (classid, name, teacher) VALUES (2, 'IOS就业班', '李老师');
INSERT INTO classes (classid, name, teacher) VALUES (3, 'PHP就业班', '张老师');
1、用SELECT语句,查询出大于平均年龄的男同学的姓名,年龄,所在班级编号,老师名称。
select a.name, a.age, a.classid, b.teacher from students a, classes b where
a.age > (select avg(age) from students where sex = '男') and a.classid = b.classid;
2、用SELECT语句,查询人数最多班的所有女同学的姓名,年龄,所在班级编号,老师名称。
select a.name, a.age, a.classid, b.teacher from students a, classes b
where a.classid = b.classid and a.sex = '女' and a.classid =
(select classid from (select count(*) count, classid from students group by classid) c order by count desc limit 0, 1);
下面是别人总结的,直接拿过来
tar 解包的命令
tar xvf 包文件名称
tar 打包的命令
tar cvf 要打包的文件名称
rpm卸载命令
rpm -e 包名称
rpm -e 包名称 --nodeps 强行卸载,不检查包的依赖关系
rpm安装包命令
rpm -ivh 包名称
字符集
1、首先操作系统的字符集为utf8
查看操作系统字符集命令
locale
2、创建数据库的时候使用CHARACTER SET utf8;指定字符集为utf8
3、设置mysql client字符集
SET NAMES utf8;
4、CRT设置为utf8
SQL语言当中字符串用单引号。
SQL语句重点学习的是SELECT,这个是在面试笔试的时候主要考试内容。
查询年龄大于21岁的同学
SELECT * FROM table1 WHERE age > 21;
查询C++班所有同学
SELECT * FROM table1 WHERE class = 'C++班';
查询C++班所有同学并且年龄大于22
SELECT * FROM table1 WHERE class = 'C++班' AND age > 22;
查询所有姓王的同学
SELECT * FROM table1 WHERE name LIKE '王%';
windows中加入path环境变量
C:\mysql\lib;C:\mysql\bin
mysql端口号3306,要在linux中将3306端口加入到防火墙的信任端口列表中
登录远程mysql server的方式
mysql -h IP地址 -u 用户名 -p
对于SELECT 语句中的逻辑判断操作符一定要灵活使用,熟练
DISTINCT代表过滤重复的值
聚合函数往往是与group by字句配合使用的
查找苍老师班里面年龄最大同学的名字
select a.name from table1 a, table3 b where a.class = b.class and b.teacher = '苍老师'
and a.age = (select max(c.age) from table1 c, table3 d where c.class = d.class and d.teacher = '苍老师');
在select语句中where查询用到哪个字段,这个字段就必须建立索引
唯一索引的查询效率高于普通索引
建立表的时候PRIMARY KEY (ID))语句相当于为ID字段建立了一个唯一索引
本文详细介绍了MySQL的基本操作,包括数据库和表的创建、数据的增删改查、索引的使用及多表查询等,并提供了丰富的实例。

被折叠的 条评论
为什么被折叠?



