mysql

update user set host = '%' where user = 'root';
增:
create table 数据库名称 ; 创建数据库
create 表名{
字段名称 字段类型(长度) 约束,
 
 
};
create table students(
id int not null primary key auto_increment,
name char(10),
age int,
);
char char(3)(长度固定 ,不可以变,没有存的空间空格补齐 )
String varchar(3)(长度可以变)
3代表的是可以存三个字符  
date YYYY-MM-DD
time hh:mm:ss
datetime YYYY-MM-DD  hh:mm:ss 默认是null
timestamp  YYYY-MM-DD  hh:mm:ss 默认是当前时间
 
text 存文本
blob 存放二进制
 
约束:
主键: primary key
唯一约束: unique
非空约束: not null
 
增加字段;
alter table 表名 add 字段名 数据类型 约束;
alter table students add score int not null;
修改字段大小和字段类型:
alter table 表名 modify 字段名 varchar(20);
alter table students modify name varchar(20);
修改字段名称:
alter table students change name student_name varchar(20);
删除字段:
alter table students drop score;
 
修改表名:
rename table students to studentssss;
修改表的字符集:
alter table students character set utf8;
 
 
删除表;
drop students;
 
 
 
 
 
 
 
 
 
 
 
创建表
 
 
创建数据库的指定字符集 create database name character set utf8 collate utf8_bin
collate 校对字符集
修改:
修改数据库字符集 alter databae 数据库名称 character set 字符集
删除:
drop database 数据库名字; 删除数据库
 
 
查:
 
使用数据库:
use 数据库名称;
 
查看正在使用的数据库:
select database();
 
查看表:
show tabLe;
 
查看库的定义过程:
show create database test;
查看表的创建过程;
show create table students;
查看表结构:
desc students;
 
 
 
-------------------------------------------------------------------
 
 
对表操作:
增:
insert into students(id,name,age) values(11,'mlj',111);
简单写法:
insert into students values(11,'mlj',111);
批量插入:
insert into students values
(12,'mlj',111),
(13,'mlj',111),
(14,'mlj',111),
(15,'mlj',111),
(17,'mlj',111),
(18,'mlj',111),
(19,'mlj',111);
 
 
 
 
删:
delete from students where id=10;
delete from students; 删除整个表的数据
truncate students; 删除后重建表
 
delete和truncate drop
区别:delete是一条一条的删除记录
truncate是删除所有记录后重启启动创建表
drop删除后表就啥都不存在了
 
改:
若果参数是字符串或者是日期要加单引号
update students set id=1080 ,name='ssss' where id=19;
 
查:select [distinct][*][字段,字段2] from 表名 [where 条件]
distinct表示去除重复
---别名查询 as可以省略
select p.name ,p.price from product as p;表别名
 
--去掉重复的值
select distinct pprice from product;
---select运算查询
select * ,pprice*2 折后价 from product ;
 
-----条件查询
查询价格大于60的产品
select* from product where pprice>60;
 
关系运算符 > >= < <= = != <>不等于
逻辑运算 and or not
    between..and....
 
--模糊查询:
-:代表的是一个字符
%:代表的是多个字符
 
select * from product where pname like '%米%';
select * from product where pname like '_米%';
 
 
--in 在某个范围中获得值
select * from product where pnum in (1,3,5);
 
--排序查询:order by 关键字
asc:ascend 升序 (默认的排序方式)
desc:descend 降序
select * from product order by pprice desc;价格降序
select * from product where pname like '%米%' order by pprice asc;//包含米字并且升序排序
 
--聚合查询:
sum()
avg()
count()
max()
min()
select sum(pprice) from product;查看所有商品价格的总和
select count(*) from product;查看商品的个数
select * from product where pprice > (select avg(pprice) from product);查询产品大于平均值的信息
 
 
----分组 group by
select pnum ,count(pnum)from product group by pnum; 按pnum分组,求出分组个数
having 可以接聚合函数 出现在分组之后
select pnum ,avg(pprice)from product group by pnum having avg(pprice)> 60; 根据pnum分组 并且要求分组avg大于60
where 他不可以接聚合函数 出现在分组之前
 
 
create table product(
pid int primary key auto_increment,
pname varchar(20),
pprice int,
ptime timestamp,
pnum int
 
);
 
 
insert into product values
(null,'小米6',5000,null,3),
(null,'辣条',1,null,32),
(null,'啤酒',4,null,3),
(null,'啤酒',5000,null,3);
 
 
--------------------多表操作-----------------------------------
---外键操作------------
给product中的这个cno
添加一个外键约束 
 
alter table product add foreign key(cno)  references  category(cid);
    pid int primary key auto_increment,//主键设置为外键
一对多
多对多(多建一张中间表,  将多对多的关系拆成一对多关系,中间至少要有两个外键,这两个外键分别指向原来的那张表)
一对一
 
 
--------------------多表操作-----------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
------------------------------------------------------------------------
 
mysqld --initialize-insecure --user=mysql
mysqld -install
mysqld -remove
 
 
mysqladmin -u root -p password 新密码
 
 
 
1.登录常用参数
    -u 用户名
 
    -p 密码
 
    mysql -uroot -p
    mysql -uroot -proot
 
    -h 服务器名称
 
    mysql -hlocalhost -uroot -p
    mysql -h127.0.0.1 -uroot -p
 
    -P 端口号
 
    mysql -uroot -p -P3306     
 
    -D 打开指定数据库
 
    -V 输出版本信息并退出
    mysql -V
    mysql -uroot -p -Vte
 
2.退出
quit
exit
 
 
 




原文地址:https://www.cnblogs.com/mljqqh/p/9694223.html