MySQL入门 基本语法1
来源:互联网 发布:淘宝返利返多少钱 编辑:程序博客网 时间:2024/06/04 00:59
一、MySQL的基本使用
1.1 命令行下的基本操作
mysql -u 用户名 -p 回车 输入密码:xxx 进入数据库 (mysql中所有的命令以分号结束,输入help;查询所有的mysql命令)
1.2 MySQL目录结构
bin: 所有的可执行文件exe MySQLInstanceConfig.exe 服务器配置向导的文件 data: 系统数据库和自带的演示数据库的文件 my.ini 配置好的文件文件
1.3数据库服务器、数据库和表的关系
**
二、SQL语句的四种类型
2.1 DDL (Data Definition Language)
数据定义语言,用于创建表、创建库。如:create/alter/drop
2.2 DML(Data Manipulation Language)
数据操纵语言,对数据进行增删改操作。如:insert/update/delete
2.3 DQL (Data Query Language)
数据查询语言,对数据进行查询操作。如:select/show
2.4 DCL (Data Control Language)
数据控制语言,grant/revoke 对用户进行管理
2.5 MySQL 中的三种注释:
1) – 单行注释 –
2)/* 多行注释 */
3)# 单行注释,不建议这种,只在mysql起作用
**
三、MySQL 管理数据库
//3.1 查看所有数据库show databases;//3.2 创建数据库create database db1;// 3.2.1 指定默认字符集为GBKcreate database db2 default character set gbk;//3.3 查看数据库的创建语句show create database db1;
3.4 校对规则
3.4.1 什么是校对规则
字符集:一个语言的所有字符的编码表,每个字符都对应了一个编码。
校对规则:同一个字符集可以有多个校对规则,不同的校对规则排序方式不一样。
校对规则 作用utf8_general_ci 忽略大小写进行排序,如:a B c D 排序顺序utf8_bin 使用二进制编码进行排序,如:B D a c
3.4.2 查看字符集和校对规则:
查看所有的字符集
显示 GBK 字符集的校对规则
3.4.3 指定数据库的默认字符集为gbk和校对规则gbk_chinese_ci
create database db3 default character set gbk collate gbk_chinese_ci;
3.5 删除数据库
drop database db3;
3.6 修改数据库默认字符集,将db3的字符集改成utf8
alter database db3 default character set utf8;
**
四、表的管理
4.1 查询所有表
use db1; --进入数据库show tables; --显示当前数据库中所有表
4.2 创建表 create
//创建一个学生表create table student( id int, name varchar(20), birthday date);
先写变量名,再写数据类型
4.3 MySQL中列的数据类型
4.4 查看一个表结构
show tables;
1.以sql格式返回
show create table student;
2.以表格格式返回
desc student;
3.复制表
如:创建一个员工表,表结构与学生一样 (like)
create table employee like student;
4.删除表 drop
1)删除一张表:drop table 表名;2)删除多张表:drop table 表1,表2;
5.修改表 alter
1.添加字段:add
//实例1:student表添加一个remark字段,类型char(10)alter table student add remark char(10);//实例2:同时添加两个字段,一个名为 a int,一个名为b intalter table student add a int,add b int;
2.修改字段:modify
//实例:将student表中的remark字段的属性改成varchar(100)alter table student modify remark varchar(100);
3.修改字段名称:change
//实例:将student表中的remark字段名改成intro,类型不变。alter table student change remark intro varchar(200);
4.删除字段:drop
//删除student表中的字段a和字段balter table student drop a,drop b;
5.修改表名:rename
//实例:将学生表student, 改名为college_studentalter table student rename college_student;
**
五、数据的管理与操作
5.1 插入数据
– 5.1.1 插入所有列
//示例:向学生表中插入3条记录-- 向学生表中插入3条记录,在mysql中字符串可以使用单引号和双引号,建议使用单引号。日期也使用单引号insert into student values(1, '关羽', '2000-11-11');insert into student values(2, '张飞', '2002-11-11');insert into student values(3, '刘备', '1900-11-11');-- 一条SQL语句插入三条记录,只有mysql中可以这样写insert into student values(4, '赵云', '2000-11-11'),(5, '孔明', '2000-11-11'),(6, '吕布', '2000-11-11');
注意:
1) 数据类型与列的数据类型匹配
2) 字符串的长度不能超过列中定义的最大长度
3) 位置与列的位置匹配
4) 字符和日期型使用单引号
–5.1.2 插入部分列
insert into student (id,name) values (7, '小乔');
5.2 修改数据
语法:
**
UPDATE: 表名
SET:修改的列和值
WHERE:对符合条件的记录进行修改,如果没有写Where则是修改所有的列
**
-- 修改表结构,添加性别列char(1)alter table student add gender char(1);select * from student;-- 更新student表中所有的性别列,改成男update student set gender='男';-- 修改小乔为女update student set gender='女' where name='小乔';-- 同时修改多列update student set name='白云', birthday = '2010-03-20', gender='女' where name='赵云';
5.3 删除数据
5.3.1 删除表中的所有数据
delete from student;
5.3.2 删除部分行数据
-- 删除id为3的学生delete from student where id=3;
**
5.4 查询数据(重点)
**
5.4.1 查询所有列
示例: 查询所有列select * from student;
5.4.2 查询指定列
示例:多个列之间以逗号分隔-- 查询name和gender这两列select name,gender from student;
5.4.3 查询时指定别名
示例:查询时给列指定别名(AS)-- 显示出来的结果是汉字,as起别名select id as 编号, name as 姓名, birthday as 生日, gender as 性别 from student;-- 起别名as可以省略select id 编号, name 姓名, gender 性别 from student;
5.4.4 合并列查询
1. 需求:添加数学,英语成绩列,并添加成绩
alter table student add math int, add english int;
- 需求:查询每个学生的数学和英语的总成绩
-- 合并查询总成绩 (as可以省略)select *, (math+english) as 总成绩 from student;
5.4.5 去除重复数据(distinct)
需求: 查询学生都来自于哪里
-- 添加城市列alter table student add city varchar(10);select * from student;-- 查询学生来至于哪些城市select distinct city from student;
5.5 条件查询
–5.5.1 where 子句
–5.5.2 比较运算符
-- 查询数学大于85分的学生select * from student where math > 85;-- 查询性别不为男的select * from student where gender <> '男'-- 不建议select * from student where gender != '男'-- 查询数学在80到90之间select * from student where math >=80 and math<=90;-- 使用between.. and,包头又包尾select * from student where math between 80 and 90;-- 查询id是1,2,5的学生select * from student where id=1 or id=2 or id=5;-- 使用in查询select * from student where id in(1,2,5);-- 查询所有姓白学生,%通配符,可以匹配任意字符select * from student where name like '白%';-- 查询包含白字的姓名select * from student where name like '%白%';-- 查询姓白,只有2个字的学生,_ 匹配1个字符select * from student where name like '白_';-- 插入一条记录insert into student (id,name) values (7,'张飞');-- 查询性别为null的学生select * from student where gender is null;
–5.5.3 判空条件
1) 判断是否为空串: gender = ‘’2) 判断是否为空: gender is null3) null和空字符串的区别:null是没有数据,空串是有数据,只是空字符串
- MySQL入门 基本语法1
- MySQL入门(1)--安装,配置,基本语法
- Ruby七天入门(1 基本语法入门)
- MySQL基本语法--函数1
- swift学习1-入门+基本语法
- Javascript入门基本语法
- OC基本语法入门
- Ibatis入门基本语法
- Python入门基本语法
- JSP入门-基本语法
- python入门--基本语法
- Kotlin 入门-基本语法
- javaScript基本语法入门
- Mysql学习历程基本语法(1)
- MySql基本语法参考
- Mysql基本语法
- mysql 基本语法介绍
- Mysql基本语法
- MySQL 简介
- 表结构设计-取值范围导致的踩坑
- mysql集群原理
- mysql——入门(一)
- Eclipse 导入java project乱码问题
- MySQL入门 基本语法1
- linux下Git的安装和配置
- 20170923在线编程之输入年、月、日输出这是一年的第几天
- Struts2学习与使用
- 最新Win7 +Python3.6.0(Anaconda3-4.3.21)+Tensorflow的安装与配置(不用切换python3.5)
- 【BZOJ2809】【APIO2012】Dispatching
- 基于码云git平台协同开发方案
- 让cmd dos 支持utf-8方法
- python面向对象编程(1)