简单的数据库操作语句(学生时代留下的记录)

update mysql user set password=PASSWORD('1234') where user='ming'

delete from mysql user where user='' and host=''

ipconfig/all

ping 127.0.0.1

if not exists

create table if not exists 表名(字段名 字段类型)

 

ENGINE=InnoDB DEFAULT CHARSET=gb2312

CREATE DATABASE ------------------ 创建数据库

CREATE TABLE --------------------- 创建表

drop ----------------------------- 删除

truncate table 表 ---------------- 清空表

MYSQL -UROOT -P      

mysql -uroot -p

desc 表名 ------------------------ 查看结构

show databases; ------------------ 查看所有数据库

show tables;--------------------- 查看所有表

show create table student; ------- 查看表的结构

show engines --------------------- 查看数据引擎

\c ------------------------------- 清除上方数据

\q ------------------------------- 退出命令

exit ----------------------------- 关闭窗口(退出登陆)--快捷键Ctrl+c

\g ----- show create table 表 \g--

use ------------------------------ 进入

\s ------------------------------- 查看Mysql服务器状态信息

primary key ---------------------- 创建主键

auto_increment ------------------- 自增

not null ------------------------- 不为空

unique --------------------------- 唯一性约束

UNSIGNED ------------------------- 无符号整数

default -------------------------- 默认 ---------- 字段名 数据类型 default 默认值

insert into表(类型)values(内容)- 插入

select*from 表  where 类型='内容'

where ---------------------------- 条件

select 类型 from 表

limit ---------------------------- 限制条数

limit 1(隐藏1条数据),5(显示多少条)(用于分页)

order by 类型 asc ---------------- 由小到大(升序)

order by 类型 desc --------------- 由大到小(降序)

order by -------------------------- 排序

select * from 表 where 类型 like 类型数值

like ----------------------------- 模糊查询

between -------------------------- 数据查询

group by ------------------------- 重复只显示一条

'旧类型名' as '新类型名' ---------- 改名

max ------------------------------ 最大值

min ------------------------------ 最小值

select max(类型) from

nullif(字段1,字段2)-------------- 如果数值相等返回为空,否则返回第一个字段

avg ------------------------------ 平均值

sum ------------------------------ 和

count ---------------------------- 记数

or ------------------------------- 多条件查询

any ------------------------------ 子查询条件

union ---------------------------- 链接两个条件

类型 in(类型的数值) ------------- 条件查询

update 表 set 类型=(要修改的数值),类型=(要修改的数值)

update 表 set -------------------- 修改数值

delete from ---------------------- 删除整行

alter table 旧表名 rename 新表名 - 改表名

alter table 表 add 列名(类型) ---- 添加列

------ alter table 表 add primary key(字段名) -- 增加主键

------ alter table 表 drop primary key -------- 删除主键

------ alter table 表 add 字段名 字段类型 () first (在第一列添加字段)

------ alter table 表 add 字段名 字段类型 () after 字段名(在第二个字段名后面添加一个字段)

alter table 表 drop 列名 --------- 删除列

alter table 表 modify 列名(类型)- 修改数据类型

------ alter table 表 modify 字段名 字段类型 () first (修改第一列字段)

------ alter table 表 modify 字段名 字段类型 () after 字段名(在第二个字段名后面添加一个字段)

alter table 表 change 旧列  新列名  类型(类型) --修改列名

alter table 表名 modify 字段名 字段类型 after 字段名 -- 把第一个字段插入到第二个字段后面

alter table 表名 engine=要修改的储存引擎。

alter table 表名 drop foreign key 外键约束名

is ------------------------------- 是

select distinct(类型) from ------- 去重

inner join ----------------------- 内连接

left join ------------------------ 外链接

having --------------------------- 与where作用相似(必须在group by之后)

rand ----------------------------- 随机排序

substr(列名,1,3) ---------------- X列从一个显示显示三个字符

left join ------------------------ 左链接

right join ----------------------- 右链接

right outer join ----------------- 右外链接

select * from union select * from

FRM ------------------------------ 表结构

MYI ------------------------------ 索引文件

MYD ------------------------------ 数据文件

create view lese as select * from 表名

create view 视图名 as ------------ 创建视图

create view 视图名 li(字段名) as select 字段名 from 表名

---------------------------------- 创建指定列名的视图

show tables ---------------------- 查看视图

show create view teacherli \G ---- 查看视图结构

create or replace view ----------- 如果视图存在就替换

alter view 表名(字段名) as select 字段名 from 表名

---------------------------------- 修改视图

drop view  ----------------------- 删除视图

update 表名 set 字段名=数值 ------- 修改视图

 

----------------------------------创建函数

create procedure 函数名()

begin

select * from 表名 ;

end;

----------------------------------

show procedure status ------------ 查看已有储存过程

call  函数名() ------------------- 调用语法

drop procedure 函数名; ----------- 删除语法

 

Timestamp default now() ------------------------------------------ 设置当前默认时间

 

Timestamp default current_timestamp on update current_timestamp -- 设置当前默认时间

 

constraint 外键名(自己定义) foreign key 字段名1 references 主表名(主键1)

CONSTRAINT `SS` FOREIGN KEY(字段) REFERENCES 主表名(主键1) ON DELETE CASCADE ON UPDATE CASCADE -- 多表外键修改删除

 

index ----------------------- 索引

unique ---------------------- 唯一索引

index(字段名) --------------- 单列索引

index(字段名,字段名) ------- 多列索引

fulltext index -------------- 全文索引

spatial index  -------------- 空间索引

alter table 表名 add index 索引名(字段) --- 添加索引

create index 索引名 on 表名(字段)

alter table 表名 drop index 索引名 ----- 删除索引

drop index 索引名 on 表名

 

explain --------------------- 查看索引

show processlist ------------ 显示运行

kill 519

 

Mysql –hlocalhost  –uroot -p

 Host  主机

 User   用户

 Password 密码

 

MySQL修改用户密码 进入MySQL,

mysql>use mysql;

mysql>update user set password=password('新密码') where user='root';

mysql>flush privileges;

修改完成。

 

数据类型:

字符串类型:

char(M)       固定长度非二进制字符      M字节,1<=M<=255

varvahr(M)    变长非二进制字符串        L+1字节,L<=M,1<=M<=255

tinytext      非常小的非二进制字符串    L+1字节,L<2~8

text          小的非二进制字符串        L+2字节,L<2~16

mediumtext    中等大小的非二进制字符串  L+3字节,L<2~24

longtext      大的非二进制字符串        L+4字节,L<2~32

enum          枚举类型,只能有一个枚举字符串值               1或2个字节,取决于枚举值的数目(最大值65535)

set           一个设置,字符串对象可以有零个或多个set成员    1,2,3,4或8个字节,取决于集合成员的数量(最多64个成员)

整数型数据类型:

tinyint       很小的整数               1个字节,有符号 -128~127,无符号0~255

smallint      小的整数                 2个字节,有符号 32768~32767,无符号0~65535

mediumint     中等大小的整数           3个字节,有符号 -8388608~8388607,无符号0~16777215

int           普通大小的整数           4个字节,有符号 -2147483648~2147483647,无符号0~4294967295

bigint        大整数                   8个字节,有符号 -9223372036854775808~9223372036854775807,无符号0~18446744073709551615

浮点数类型,定点数类型:

float               单精度浮点                   4个字节

double              双精度浮点                   8个字节

decimal(M,D),DEC   压缩的“严格”定点数          M+2个字节

日期与时间数据类型:

year       YYYY              1901~2155                  1字节

time       HH:MM:SS        -838:59:59~838:59:59    3字节

date       YYYY-MM-DD        1000-01-01~9999-12-3       3字节

datetime   YYYY-MM-DD HH:MM:SS 1000-01-01 00:00:00~9999-12-31 23:59:59          8字节

timestamp  YYYY-MM-DD HH:MM:SS 1970-01-01 00:00:01 UTC~2038-01-19 03:14:07 UTC  4字节

 

posted @ 2018-04-28 11:28  键盘碎了  阅读(502)  评论(0编辑  收藏  举报