1.打开数据库:
1.cmd-->mysql -u root -p123456 ;
2.开始--应用程序----mysql---mysql sever 5.7----第二个
2.数据库;
1. 创建
create database 表名;
2.查询
show databases(显示数据库)。
3.创建指定字符
create datebase 表名 character set utf8;
4.查看数据库的字符集
show create datebase 表名
5.删除数据库:
drop database 表名。
drop database if exists 表名。
3.表:
1.指向数据库:
use 库名;
2.创建表名:
create table name(列表1,类型1);
2.1:创建字符集表,有编码格式
create table 表名(列名 数据类型 )character set 字符集。
eg:alter table t1 charater set gdk;
check :show create table t1;
3.查看表:
show tables;
4.删除表
drop table 表名;(没有会报错)
drop table if exists 表名;
5.给表添加数据,会添加一行新的数据
5.1 insert into 表名 values(列1, 列2,......)
5.2: 插入一行数据,对应的列值改变,其余 null
insert into 表名 set 列=值;
5.3 插入数据,对应的列值改变,其余为Null
insert into 表名 (列,列。。。) values(值,值。。)
6.查看表里的具体列
select * from 表名 *表示所有列
select id from 表名
7.表添加1列
alter table 表名 add 列名 数据类型。
8.修改列的数据类型
alter table 表名 modify 列名 新的数据类型
9.删除某一列
alter table 表名 drop 列名
10.改变表的列名
alter table 表名 change column 旧表名 新表名 类型
11. 显示创建表的指令
show create table 表名;
12. 改变数据:
update 表名 set 列名 =表达式 where 条件 注意:如果没有where条件,则对整个数据改变。
13.删除某一行的数据,where对应的条件 注意:如果没有where条件,则将删除表中的数据
delete from 表名 where 条件
条 件
14.删除数据,找回来,Mysql 自动提交,需要设置不自动提交
14.1.设置不自动提交
set autocommit=false
14.2.设置保存点
savepoint aaa;
14.3.删除表里的数据
delete from 表名
14.4.回滚数据
rollback to aaa;
15.筛选 select
15.1 select 列名 .......from 表名 where 条件。
15.2 select distinct 列 from 表名(指定过滤重复的数据)
15.3 列可以参与元素
select 列+数字 from 表名
15.4:select *from 表名 betwent ....and ..... 注意:包括上下边
15.5 select * from 表名 not betwent .....and ..... 注意:不包括边界
15.6 select *from 表名 条件 and 条件
t
15.7 select * from 表名 条件 or 条件
15.8 like 模糊查询 %代表0个或多个任意字符(汉子一个字符),_代表一个字符 select *from 表名 where name like '彭%'_
s e
select *from 表名 where name like '彭_' ; 注意:_代表 彭 后只能有一个字符。
15.8 : is null
select * from 表名 where 列 is null;
select * fome 表名 where 列 is not null;
15.9 降续,升续
select 列1,列2 from 表名 order by 列;
15.10 降续
select 列 from 表名 order by 列 desc.
注意事项: 关键字:order by ,在末尾加 desc.
15.11 别名,as
select m as '别名' 表名 from order by 列。
15.12 in选数据
select * from 表名 where 列 in (数字);
16.函数:
16.1 count()表示计算的个数。如果是null,不加入计算。
select count( 列) from 表名。
16.2 sum求和,
select sum(列) * from 列名。求几列的总和时(列1+列2+列3)
16.3 avg 返回平均值,如果为null,则不计算。
16.4 max min 最小值
16.5 分组:select from 表名 group by 那列分组;
过滤 having 和where 一样,可以实现筛选。 having 写在 group by 后。
16.6时间 selecct current_date() from dual; 年月日
select current_time() from dual; 时分秒
当前年:月,日
select hour(current_time()) from dual;
16.7 添加时间,减少时间
selecct date_add(now(),interval 1month)from 表名
select date_sub(now(),interval 1 month )from 表名
16.8 两个时间之差:
select datediff(时间,时间) from dual
16.9 拼接 concat 追加数据:不会修改数据库
select concat(任意数据,任意数据) from dual;
16.10
小写变大写:lcase
select lcase(字符串) from dual.
bb
17.链表查询:
将两个链表连在一起:2种方式
1.sele
4.备份数据 在cmd环境下输入
1mysqldump -u root -p123456 库名 >d:/pc.sql; 尖括号。
5.导入数据:yunsuan
1.创建一个新的空的数据库
create database 数据库名;
2. 指向新的数据库
use 新建的数据库名
3. 导入
source d:/pc.sql
6.mysql里的数据类型
1.short----->smallint;
2.int------->int
3.long------->integer
4.float ------->float(m,d) m: 有效数字,d:小数点后几位,如果小数大于d+1,四舍五入。小数等于d+1,五舍六入。
5.double------>double 与flaot类似;
6.boolean----->tinyint 0,1;可以输入true,false,显示的是0,1
7.string----varchar(45)
8.string-----text 大文本.
9date -------date
7.date 详解:
date :年月日
datetime: 年月日时分秒 时间不变 now()获取当前时间。update()更新时不变。
timestamp: 获取当前时间 now() ,update某条记录时,该列的值会自动更新。
update 表名 set 列=值;
8.主键自增:primary key auto_increment
create table users(id int primary key auto_increment);
当添加新的一列时;
alter table users add i int primary key auto_increment