mysql 基础查询添加小结
2018-03-19 20:53
369 查看
安装:
MAC 下 下载地址:https://dev.mysql.com/downloads/安装结束后会弹出一个提示框 里面有初始密码 保存下来 打开终端:
touch .bash_profile(没创建的情况下)
open .bash_profile
添加 : export PATH=$PATH:/usr/local/mysql/bin/
系统偏好设置里打开mysql 服务 重启终端
输入 mysql -uroot -p
输入初始密码
重设密码 set password=password(‘123456’);
结束
第一行代码
查看数据库show databases;
创建数据库
create database + 数据库名;
创建一个编码格式是GBK的数据库
create database + 数据库 + character set GBK;
查询数据库表的信息
show create database +库名;
修改数据库编码格式
alter database + 库名 + character set + 编码格式;
切换数据库
use 切换数据库名;
创建表结构
create table 表名(字段名1 字段类型,字段名2 字段类型,字段名3 字段类型 ….);
添加字段
alter table 表名 add 字段名称 字段类型;
修改字段类型
alter table class modify 字段名 字段类型;
删除字段
alter table class drop 字段名;
查看表结构信息
show create table class;
修改表名
renome table 旧表名 to 新表名;
修改字段名字
alter table 表名 change 旧字段 新字段 字段类型;
查看表结构
desc 表名
插入数据
insert into 表名 (列名1,列名2 …) values(列值1, 列值2 …);
insert into 表名 values(列值1, 列值2 …);
insert into 表名 values(列值1, 列值2 …),(列值1, 列值2 …),(列值1, 列值2 …);
修改操作
update 表名 set 列名=列值 where 列名 = 列值
删除操作
delete from 表名 where 列名 = 列值
查询操作
select 列名 from 表名
过滤重复数据
select distinct 字段 from 表名;
练习
创建stu表 表内有 id name age gendercreate table stu(id int,name varchar(10),age int,gender VARCHAR(10));
添加6个数据
insert into stu val
4000
ues (1,’James’,18,’man’),(2,’Tom’,19,’man’)(3,’Jelly’,23,’female’),(4,’Mike’,15,’man’),(5,’Miku’,null,’female’),
(6,’Charli’,19,’man’);
删除 stu 表
DELETE from stu;
查询 stu 表
select * FROM stu;
查询 id,name
select id,name from stu;
查询年龄大于20岁的(查询只显示年龄和姓名)
select age,name from stu where age > 20;
查询性别为女,并且年龄20的记录
select * from stu where gender = ‘female’ AND age > 20;
查询学号为3,或者姓名为Miku的记录
select * from stu where id=3 or name = ‘Miku’;
查询学号为1,2,3的记录
select * from stu where id in(1,2,3);
查询学号不是1,2,3的记录
select * from stu where id not in(1,2,3);
查询年龄为null的记录
select * from stu where age is null;
查询年龄在20到40之间的学生记录
select * from stu where age BETWEEN 20 and 40;
查询性别非男的学生记录
select * from stu where gender !=’man’;
查询姓名不为null的学生记录
select * from stu where name is not null;
修改年龄
update stu set age = 22 where id = 3;
过滤重复数据
select distinct age from stu;
增加字段 月薪 和 佣金
alter table stu add salary int;
alter table stu add commission int;
update stu set salary = 1000,commission = 500 where id = 1;
update stu set salary = 2000,commission = 100 where id = 2;
update stu set salary = 3000,commission = 200 where id = 3;
update stu set salary = 4000,commission = 300 where id = 4;
update stu set salary = 5000,commission = 400 where id = 5;
查询佣金和工资之和
select name,salary + commission as total from stu;
结果相当于增加了一个新的字段(不会对原表进行修改)
给字段起别名 使用 as 关键字 该关键字可以省略
系统关键字不能当做字段名
相关文章推荐
- C语言MySQL 基础操作(添加、查询和删除)
- jdbc-mysql基础 把查询到的结果集封装成Map的形式
- 通讯录相关---前提基础 查询、添加
- MySQL 基础查询
- myecilpse +TOMCAT+web:jsp向mysql添加数据,查询在jsp页面显示
- MYSQL基础七--单表数据查询
- mysql模糊查询like和regexp小结
- 结合PHP脚本添加和查询MySQL数据的基本教程
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(八)mysql中文查询bug修复
- mysql服务器查询慢原因分析与解决方法小结
- mysql 再查询结果的基础上查询(子查询)
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(八)mysql中文查询bug修复
- MySQL基础笔记(三) 复杂查询
- 【SSH三大框架】Hibernate基础第六篇:多对一关联关系的映射、分析及添加、查询
- mysql基础(五)查询缓存
- mysql服务器查询慢原因分析与解决方法小结
- mysql简单实现查询结果添加序列号
- 关于mysql查询语句基础SQL
- android基础笔记:ContentValues应用(查询、添加手机联系人)
- 通讯录相关---前提基础 查询、添加