PHPMySQLI的基础数据

PHP_MySQLi的操作
一、操作步骤:
1. 连接MySQL数据库
2. 判断是否连接成功
3. 选择数据库
4. 设置字符集
5. 准备SQL语句
6. 向MySQL服务发送SQL语句
7. 解析处理结果集
8. 释放结果集,关闭数据库连接

二、常用操作:

  1. mysqli_connect();–连接数据库,并返回一个连接对象
    格式: mysqli_connect(主机名,用户,密码,数据库名);
    –其中参数可选,若不写则参考php.ini中默认配置

  2. mysqli_connect_errno()–获取数据库连接错误号
    mysqli_connect_error()–获取数据库连接错误信息

  3. mysqli_error(); --获取刚刚(最后)执行数据库操作的错误信息
    mysqli_errno(); --获取刚刚(最后)执行数据库操作的错误号
    错误号为0表示没有错误

  4. mysqli_select_db(数据库连接对象,数据库名);
    选择一个数据库,等同于"use 库名"语句

  5. mysqli_set_charset(数据库连接,字符编码); --设置字符编码
    例如:mysqli_set_charset( l i n k , " u t f 8 " ) ; 等 同 于 : m y s q l i q u e r y ( link,"utf8"); 等同于:mysqli_query( link,"utf8");mysqliquery(link,“set names utf8”);

  6. mysqli_query(数据库连接,sql语句); – 发送一条sql语句
    sql语句若是查询,则返回结果集,其他则返回boolean值表示执行是否成功。

  7. 解析结果集函数:
    mysqli_fetch_array(); --以关联和索引两种方式数组解析结果集
    也可以指定第二参数来定义返回格式:
    MYSQL_BOTH(0关联和索引)/MYSQL_NUM(2索引)/MYSQL_ASSOC(1关联)

    mysqli_fetch_row(); --以索引式数组解析结果集
    *mysqli_fetch_assoc(); --以关联式数组解析结果集
    mysqli_fetch_object(); --以对象方式解析结果集

  8. mysqli_free_result(结果集名); --释放结果集

  9. mysqli_close(数据库连接); --关闭数据库连接

  10. mysqli_num_rows(结果集); --获取结果集中的数据条数

  11. mysqli_num_fields(结果集); --获取结果集中的列数(字段数量)

12.mysqli_affected_rows — 取得前一次 MySQL 操作所影响的记录行数
关联的 INSERT,UPDATE 或 DELETE 查询所影响的记录行数。

  1. mysqli_insert_id — 取得上一步 INSERT 操作产生的 ID

一、概念:
数据: data
数据库: DB
数据库管理系统:DBMS
数据库系统:DBS
MySQL:数据库
mysql:客户端命令(用来连接服务或发送sql指令)
SQL:结构化查询语言 ,其中MySQL支持这个。
SQL语言分为4个部分:DDL、DML、DQL、DCL

二、连接数据库:
mysql -h 主机名 -u 用户名 -p密码 库名

C:>mysql --采用匿名账号和密码登陆本机服务
C:>mysql -h localhost -u root -proot --采用root账号和root密码登陆本机服务
C:>mysql -u root -p --推荐方式默认登陆本机
Enter password: ****

C:>mysql -u root -p lamp61 --直接进入lamp61数据库的方式登陆

三、授权:
格式:grant 允许操作 on 库名.表名 to 账号@来源 identified by ‘密码’;

--实例:创建zhangsan账号,密码123,授权lamp61库下所有表的增/删/改/查数据,来源地不限
mysql> grant select,insert,update,delete on lamp61.* to zhangsan@'%' identified by '123';
Query OK, 0 rows affected (0.00 sec)

四、SQL的基本操作
mysql> show character set; 查看系统中可用的字符集
mysql> alter database 库名 default character set utf8;修改数据库字符集
mysql> alter table 表名 character set utf8;修改表字符集
mysql> alter table 表名 modify 字段名 类型 character set utf8;修改字段字符集
mysql>show databases; --查看当前用户下的所有数据库
mysql>create database [if not exists] 数据库名; --创建数据库
mysql> use test; --选择进入test数据库
mysql> drop database 数据库名; --删除一个数据库

mysql> show tables; --查看当前库下的所有表格
mysql> select database();  --查看当前所在的数据库
mysql> desc tb1;  --查看tb1的表结构。
mysql> create table demo(	--创建demo表格
	-> name varchar(16) not null,
	-> age int,
	-> sex enum('w','m') not null default 'm');

mysql> desc demo;  --查看表结构
mysql>\h   -- 快捷帮助
mysql>\c   -- 取消命令输入
mysql>\s   -- 查看当前数据库的状态
mysql>\q   -- 退出mysql命令行

五、 MySQL数据库的数据类型:

MySQL的数据类型分为四大类:数值类型、字串类型、日期类型、NULL。

5.1 数值类型:
	*tinyint(1字节)
	smallint(2字节)
	mediumint(3字节)
	*int(4字节)
	bigint(8字节)
	*float(4字节)   float(6,2)
	*double(8字节)  
	decimal(自定义)字串形数值
	
 5.2 字串类型
	普通字串
	*char  定长字串   	 char(8)  
	*varchar 可变字串 varchar(8)
	
	二进制类型
	tinyblob
	blob
	mediumblob
	longblob
	
	文本类型
	tinytext
	*text      常用于<textarea></textarea>
	mediumtext
	longtext
	
	*enum枚举
	set集合
	
5.3 时间和日期类型:
	date  年月日
	time  时分秒
	datetime 年月日时分秒
	timestamp 时间戳
	year 年

5.4 NULL值
	NULL意味着“没有值”或“未知值”
	可以测试某个值是否为NULL
	不能对NULL值进行算术计算
	对NULL值进行算术运算,其结果还是NULL
	0或NULL都意味着假,其余值都意味着真

MySQL的运算符:
	算术运算符:+ - * / % 
	比较运算符:= > < >= <= <> != 
	数据库特有的比较:in,not in, is null,is not null,like, between and 
	逻辑运算符:and or not

六、**表的字段约束:
unsigned 无符号(正数)
zerofill 前导零填充
auto_increment 自增
default 默认值
not null 非空
PRIMARY KEY 主键 (非null并不重复)
unique 唯一性 (可以为null但不重复)
index 常规索引
设置typeid为type表中的id的外键///typeid int references type(id),

七: *建表语句格式:
create table 表名(
字段名 类型 [字段约束],
字段名 类型 [字段约束],
字段名 类型 [字段约束],

);
1.进入数据库:c:> mysql -u root -p

2.创建数据库:mysql> create database 库名;

3.进入数据库:mysql> use 库名;

4.查看当前在哪个库:mysql> select database();

5.创建表:
–普通创建:create table ~ 表名~(表内容);
– 通过多行创建表:
mysql> create table 表名(
-> id int,
-> name varchar(8),
-> age tinyint
-> );

6.查看表结构///desc 表名;

7.尝试删除表///drop table if exists 表名;

8.查看表的建表语句///mysql> show create table表名\G;

9.添加:-------------------------------------------------=添加=-----------
–标准添加,指定所有字段给定所有的值///mysql> insert into 表名(id,name,age) value(1,‘zhangsan’,20);
– 不指定字段添加值,注意:字段顺序和表结构一致///mysql> insert into 表名 value(2,“lisi”,22);
– 指定部分字段,添加值///mysql> insert into 表名(id,name) value(3,“wangwu”);
– 查看学生信息表的同时,追加一个beijing值的字段,字段名为city(城市)///mysql> select *,“beijing” city from 表名;
– 在表的第一列位置添加字段/// alter table 表名 add 字段名 类型 约束 first;(如不加此first默认在最后添加)
– 给表字段添加索引///alter table 表名 add index 字段名_index(字段名);
10.查看-------------------------------------------------=查看=-----------
–查看所有数据库///show databases;
–查看数据库中所有表///show tables;
–查看表中的所有数据///mysql> select * from 表名;
– 查看表名标的部分字段的数据///mysql> select name,age,classid from 表名;
– 查看部分字段 ,并为name字段起个别名,叫username字段名///mysql> select name as username,age,classid from 表名;
– 查看年龄以及5年后的年龄///mysql> select age,age+5 from 表名;
– 按钮年龄升序排序,取第一条,并显示 : mysql> select * from 表名 order by age asc limit 1;
– 查看学生信息表的同时,追加一个beijing值的字段,字段名为city(城市)///mysql> select *,“beijing” city from 表名;
– 通过表名取字段信息///mysql> select s.name,s.age from 表名 s;
— where条件查询----

– 1. 查看lamp97班级的所有学生信息///mysql> select * from 表名 where classid=‘lamp97’;

– 2. 查看lamp97期性别为m的所有学生信息///mysql> select * from 表名 where classid=‘lamp97’ and sex=‘m’;

– 3. 查看年龄age是20~30岁的学生信息
mysql> select * from 表名 where age>=20 and age<=30;
mysql> select * from 表名 where age between 20 and 30;

– 4. 查看年龄age是20~30岁之外的学生信息
mysql> select * from 表名 where age<20 or age>30;
mysql> select * from 表名 where age not between 20 and 30;

– 5. 查看id号为3,5,8,10的学生信息
mysql> select * from 表名 where id=3 or id=5 or id=8 or id=10;
mysql> select * from 表名 where id in(3,5,8,10);

– 6. 查看lamp97期和lamp98期性别为w的信息
mysql> select * from 表名 where classid in(‘lamp97’,‘lamp98’) and sex=‘w’;
mysql> select * from 表名 where (classid=‘lamp97’ or classid=‘lamp98’) and sex=‘w’;

– 7. 查看姓名中包含ang字串的学生信息
mysql> select * from 表名 where name like ‘%ang%’;
mysql> select * from 表名 where name regexp “ang”; --使用正则

– 8. 查看年龄为偶数,并且性别为m的学生信息///mysql> select * from 表名 where age%2=0 and sex=‘m’;

– 9. 查看姓名为4个任意字符的学生信息
mysql> select * from 表名 where name like ‘____’;
mysql> select * from 表名 where name regexp ‘^.{4}$’;

–10. 查看姓名不为null的信息。///mysql> select * from 表名 where name is not null;
— 统计查询(聚合查询): count() sum() avg() max() min();
– 统计学生表的数据条数
mysql> select count(id) from 表名;
mysql> select count() from 表名;
– 分组,按班级分组//mysql> select classid from 表名 group by classid;
– 统计每个班级的人数和平均年龄。///mysql> select classid,count(
),avg(age) from 表名 group by classid;

– 统计学生信息表中男女生各自人数(按性别分组)。///mysql> select sex,count() from 表名 group by sex;
– 统计lamp97期的按性别分组后的人数///mysql> select sex,count(
) from 表名 where classid=‘lamp97’ group by sex;
– 统计每个班的男女生各自人数(按性别分组)。///mysql> select classid,sex,count(*) from 表名 group by classid,sex;
– 统计每个班的平均年龄,要求只显示24岁以上的班级。///mysql> select classid,avg(age) from 表名 group by classid having avg(age)>24;
– 排序:order by 字段名 [asc|desc]
– 默认asc(升序) desc(降序)
– 查询所有数据 并按年龄做默认升序排序///mysql> select * from 表名 order by age;
– 查询所有数据 并按年龄做默认升序排序///mysql> select * from 表名 order by age asc;
– 查询所有数据 并按年龄降序排序///mysql> select * from 表名 order by age desc;
– 按班级升序,相同的班级按年龄降序排序。///mysql> select * from 表名 order by classid asc,age desc;
– limit 子句:(分页)
–====================================
– 提取前4条数据mysql> select * from 表名 limit 4;
– 排除前4条,取3条///mysql> select * from 表名 limit 4,3;
– limit后面的数值不可以为负数。mysql> select * from 表名 limit 60,-3;
– 统计每个班级的人数,并按人数从大到小排序,只取前两条。mysql> select classid,count(*) num from 表名 group by classid order by num desc limit 2;
– 查询id为7的下一条数据mysql> select * from 表名 where id>7 order by id limit 1;
–================================================================
– 组合查询(多表查询)
– 1. 嵌套查询
– 2. where 关联查询
– 3. join 连接 (左联 left join、右联 right join、内联inner join)
–=================================================================
– 最大年龄 mysql> select max(age) from 表名;
– 查询年龄为36岁的人 mysql> select * from 表名 where age=36;
– 查询年龄最大的学生信息mysql> select * from 表名 where age=(select max(age) from 表名);
– 查询php成绩最高的学生id号mysql> select sid from grade where php=(select max(php) from grade);
– 查出php成绩最高的学生信息
mysql> select * from 表名 where id in(
-> select sid from grade where php=(select max(php) from grade));
– 查询所有字段,条件是学生的id和成绩的sid关联 mysql> select * from 表名1,表名2 where 表名.id=表名1.sid;
– 查询部分字段,条件是学生的id和成绩的sid关联 mysql> select 表名.id,表名.name,表名.age,grade.php,grade.mysql from 表名,grade where 表名.id=grade.sid;
– 通过别名方式查询部分字段,条件是学生的id和成绩的sid关联mysql> select s.id,s.name,s.age,g.php,g.mysql from 表名 s,grade g where s.id =g.sid;
– 左联查询:查询学生表中所有的成绩,没有参加考试的补空
mysql> select s.id,s.name,s.sex,g.php,g.mysql from 表名 s left join grade g
-> on s.id=g.sid;
– 内联相当于where关联,就是学生表和成绩表共有的数据。
mysql> select s.id,s.name,s.sex,g.php,g.mysql from 表名 s inner join grade g
-> on s.id=g.sid;

11.修改:------------------------------------------------=修改=------------
–修改id值为3的年龄信息为25///mysql> update 表名 set age=25 where id=3;
– 修改id值为5的年龄26,名字为qq ///mysql> update 表名 set age=26,name=‘qq’ where id=5;
(-- 修改一个不存在的数据,不会报错。mysql> update 表名 set age=100 where id>50;)
– 将id值为10的表名信息中的name改为qq,sex改为w/// mysql> update 表名 set name=‘qq’,sex=‘w’ where id=10;
– 修改年龄最小的一条的性别信息/// mysql> update 表名 set sex=‘m’ order by age asc limit 1;
– 改名其中为name起别名的as关键字可以省略不用写///mysql> select name username,age,classid from 表名;
– 为age+5起个别名age5(对外的字段名)///mysql> select age,age+5 age5 from 表名;
– 修改demo1表结构,为m字段添加一个前导补零(先进入表格mysql> desc demo1;),m m表示没有改字段名mysql> alter table demo1 change m m int(5) unsigned zerofill;

12.删除:------------------------------------------------=删除=------------
– 删除name值为01的数据/// mysql> delete from 表名 where name=‘01’;
– 删除id值为100到200的数据 ///mysql> delete from 表名 where id>=100 and id<=200;
– 删除id值为10,11和20的数据 /// mysql> delete from 表名 where id in(10,11,20);
– 删除id值大于100的信息///mysql> delete from 表名 where id>100;
– 删除age大于等于50并且小于等于100的数据///mysql> delete from 表名 where age>=50 && age<=100;
– 删除age比10小的或比100大的///mysql> delete from 表名 where age<10 || age>100;
– 使用关键字distinct 去除重复的classid值///mysql> select distinct classid from 表名;
– 删除字段。/// alter table 表名 drop 字段名;

13-- 导出lamp97数据库并以lamp97.sql文件存储
D:\xampp\htdocs\lamp97\lesson23_mysql>mysqldump -u root -p lamp97>lamp97.sql

14-- 导出lamp97数据库中的表名表及数据,以lamp97_表名.sql文件存储。
D:\xampp\htdocs\lamp97\lesson23_mysql>mysqldump -u root -p lamp97 表名>lamp97_表名.sql

– 数据库恢复,将lamp97.sql文件中的数据恢复到数据库lamp97中
D:\xampp\htdocs\lamp97\lesson23_mysql>mysql -u root -p lamp97<lamp97.sql

===================================
PHP_MySQLi的操作

一、操作步骤:
1. 连接MySQL数据库
2. 判断是否连接成功
3. 选择数据库
4. 设置字符集
5. 准备SQL语句
6. 向MySQL服务发送SQL语句
7. 解析处理结果集
8. 释放结果集,关闭数据库连接

二、常用操作:

  1. mysqli_connect();–连接数据库,并返回一个连接对象
    格式: mysqli_connect(主机名,用户,密码,数据库名);
    –其中参数可选,若不写则参考php.ini中默认配置

  2. mysqli_connect_errno()–获取数据库连接错误号
    mysqli_connect_error()–获取数据库连接错误信息

  3. mysqli_error(); --获取刚刚(最后)执行数据库操作的错误信息
    mysqli_errno(); --获取刚刚(最后)执行数据库操作的错误号
    错误号为0表示没有错误

  4. mysqli_select_db(数据库连接对象,数据库名);
    选择一个数据库,等同于"use 库名"语句

  5. mysqli_set_charset(数据库连接,字符编码); --设置字符编码
    例如:mysqli_set_charset( l i n k , " u t f 8 " ) ; 等 同 于 : m y s q l i q u e r y ( link,"utf8"); 等同于:mysqli_query( link,"utf8");mysqliquery(link,“set names utf8”);

  6. mysqli_query(数据库连接,sql语句); – 发送一条sql语句
    sql语句若是查询,则返回结果集,其他则返回boolean值表示执行是否成功。

  7. 解析结果集函数:
    mysqli_fetch_array(); --以关联和索引两种方式数组解析结果集
    也可以指定第二参数来定义返回格式:
    MYSQL_BOTH(0关联和索引)/MYSQL_NUM(2索引)/MYSQL_ASSOC(1关联)

    mysqli_fetch_row(); --以索引式数组解析结果集
    *mysqli_fetch_assoc(); --以关联式数组解析结果集
    mysqli_fetch_object(); --以对象方式解析结果集

  8. mysqli_free_result(结果集名); --释放结果集

  9. mysqli_close(数据库连接); --关闭数据库连接

  10. mysqli_num_rows(结果集); --获取结果集中的数据条数

  11. mysqli_num_fields(结果集); --获取结果集中的列数(字段数量)

12.mysqli_affected_rows — 取得前一次 MySQL 操作所影响的记录行数
关联的 INSERT,UPDATE 或 DELETE 查询所影响的记录行数。

  1. mysqli_insert_id — 取得上一步 INSERT 操作产生的 ID

创建一个表

create table stu(
ip int(10) unsigned not null AUTO_INCREMENT PRIMARY KEY,
name varchar(16) not null,
sex enum(‘w’,‘m’) not null default ‘m’);

修改表字段信息
after 在…之后
–添加一个age字段在sex后面
alter table stu add age tinyint after sex;
–改字段 ip为id其约束不变//主键值设置一次
alter table stu change ip id int(10) unsigned not null AUTO_INCREMENT;

    数据操作

===================================================
添加数据********

–添加一个数据 指定字段方式
insert into stu(id,name,sex,age,classid) values(null,‘dema’,‘m’,18,‘lampsy16’);
–添加一个数据 不指定字段
insert into stu values(null,‘akali’,‘w’,18,‘lampsy16’);
–批量添加
insert into stu4 values
(null,‘liuneng’,‘m’,26,‘lampsy16’),
(null,‘zhaosi’,‘m’,16,‘lampsy15’),
(null,‘xieguangkun’,‘w’,26,‘lampsy16’),
(null,‘zhaoyutian’,‘w’,33,null),
(null,‘tiezhu’,‘m’,28,‘lampsy17’),
(null,‘cuihua’,‘w’,22,‘lampsy17’),
(null,‘xiaofeng’,‘m’,19,‘lampsy17’),
(null,‘xiaofeng’,‘w’,17,‘lampsy16’),
(null,‘xiaofeng’,‘m’,16,‘lampsy16’),
(null,‘xiaofeng’,‘w’,19,null),
(null,‘xiaobai’,‘m’,19,‘lampsy16’),
(null,‘xiaohei’,‘m’,19,‘lampsy16’),
(null,‘xiaohong’,‘w’,19,‘lampsy13’),
(null,‘xiaozi’,‘w’,19,‘lampsy13’),
(null,‘xiaoming’,‘m’,19,‘lampsy16’);

数据修改*******
–修改数据库字符集
alter database lampsy16 default character set utf8;
–修改表字符集为utf8
alter table stu character set utf8;
–修改字段字符集
alter table stu modify name varchar(16) character set utf8;
–修改id值为17 name值为小明
update stu set name=‘小明’ where id=17;
–修改id为6信息 name改为赵信,年龄加1岁
update stu set name=‘赵信’,age=age+1 where id=6;
–修改classid为空的信息 年龄为18岁
update stu set age=18 where classid is null;

数据删除*******
–删除 classid为空的信息
delete from stu where classid is null;
–删除 年龄不在20~25岁之间的女性信息
delete from stu where (age < 20 or age > 25) and sex=‘w’;
–清空所有stu表数据
delete from stu;

数据花式查看*******
//查看只是查看不能修改表中数据
–查看所有字段所有信息
select * from stu;
–查看name,sex,classid字段的信息(查看部分字段)
select name,sex,classid from stu;
–查看name字段,给name字段其一个别名 // as 起别名
##给字段起别名 as 可以省略
select name as mingzi from stu;
select name mingzi from stu;
–查看每个人的年龄,和每个人5年后的年龄 把5年后的年龄起个别名
select name,age,age+5 as 5nianhou from stu;
–查询信息并追加一个新字段city,值为沈阳(新增字段为临时)
select name,age,classid,‘沈阳’ as city from stu;
–查询班级和姓名 把他们连一起输出
select concat(classid,"-",name) from stu;
select concat(classid,"-",name) cname from stu; //cname 是别名
where条件查询*******
–查询lampsy16期所有学员
select * from stu where classid=‘lampsy16’;
–查询lampsy16期所有男孩子
select * from stu where classid=‘lampsy16’ and sex=‘m’;
–查询班级为空的信息
select * from stu where classid is null;
–查询年龄信息20~30之间的信息 **两者之间用 与
select * from stu where age>=20 and age<=30;
select * from stu where age between 20 and 30;
–查询年龄信息20~30之外的信息 **两者之外用 或
select * from stu where age not between 20 and 30;
select * from stu where age<=20 or age>=30;
–查询lampsy16和lampsy17的女生信息
select * from stu where classid in(‘lampsy16’,‘lampsy17’) and sex=‘w’;
–查询年龄在22岁以上的16期男生信息
select * from stu where age>22 and classid=‘lampsy16’ and sex=‘m’;
**支持两个 % _
–查询学生名以xiao开头的信息
select * from stu where name like ‘xiao%’;
–查询学生名包含ao的或任意两个字符大小的名
select * from stu where name like ‘%ao%’ or name like ‘’;
–查询学生名 任意六个字符大小的名
//这里是六个下划线 每个下划线代表一个字符大小,中文算一个字符
select * from stu where name like '
____’;
select * from stu where name regexp ‘1{6}$’;
–查询16期男孩和17期女孩的信息
select * from stu where (classid=‘lampsy16’ and sex=‘m’) or (classid=‘lampsy17’ and sex=‘w’);

MySQL聚合数据函数(统计函数)*******
**总数: count() 最大:max() 最小:min 平均:avg() 总合: sum()
–统计学生表中人数
select count() from stu;
–统计表中拥有classid值的人数//其中值为null的不计数
select count(classid) from stu;
–统计16期的学生信息以年龄分类
select count(
),max(age),min(age),avg(age),sum(age) from stu where classid=‘lampsy16’;

MySQL 分组查询*******
###group by 字段名 (having 子条件)
**分组的目的就是为了统计
–按班级分组
select classid from stu group by classid;
–统计每个班级的人数
select classid,count() from stu group by classid;
–统计每个班级的男生和女生人数 (不含没有班级)
select classid,sex,count(
) from stu where classid is not null group by classid,sex;
–统计每个班级人数 只显示2人以上的班级数据 起别名叫renshu
select classid,count(*) renshu from stu group by classid having renshu>2;

MySQL 排序*******
**order by 字段名 [升序:asc 降序:desc]
–按年龄升序
select * from stu order by age; —默认为升序asc
select * from stu order by age asc;
–按年龄降序
select * from stu order by age desc;
–按班级升序排序,相同班级按年龄排序
select * from stu order by classid asc,age desc;

分页显示limit*******
–分页公式: limit (当前页号-1)页大小 , 页大小
–分页公式: limit 跳过多少条数据 , 显示多少条数据
–进行分页
–第一页
select * from stu limit 0,4;
–第二页
select * from stu limit 4,4;
–第三页
select * from stu limit 8,4;
–获取年龄最大的三个人 **只要3个人
select * from stu order by age desc limit 3;
–统计每个班的男女生人数,并做降序排序,取前4条 起别名
select classid,sex,count(
) num from stu
where classid is not null
group by classid,sex
order by num desc
limit 4;

======================================
MySQL命令手册

常用命令

\q 或 quit 退出MySQL
\G 让数据立起来显示 (不加结束的分号";");
\c 退出当前错误命令等 (如果之前给了一个单个引号那系统会等待你给下一个引号)
\s 显示服务器相关信息

授权操作命令

–设置一个张三账号密码为123 授予对所有库所有表的操作
grant all on . to zhangsan@’%’ identified by ‘123’;
grant all on . to 所有库所有表
@’%’ 通配符 任何主机都可登陆
identified by 设置密码
–查看指定用户的授权信息
show grants for zhangsan@’%’;
–删除用户
drop user ‘zhangsan’@’%’;

数据库操作命令

show databases; 查看MySQL中所有的数据库
create database 库名; 创建数据库
create database [if not exists] 数据库名; 尝试创建数据库
use 库
名; 选择数据库 或进入数据库(创建表之前要进入到数据库中)
drop database 库*名; 删除数据库
select database(); 查询当前在哪个库中

表操作命令

show tables; 查询当前库下所有的表
create table 表名( 创建表
字段名 字段类型 [字段约束],
字段名 字段类型 [字段约束],
字段名 字段类型 [字段约束]
);
show create table 表
名; 查询建表语句
desc 表名 查看表结构
drop table 表
名; 删除表

数据操作命令

添加*****
–指定字段添加
insert into 表名(字段1,字段2,字段3) value(值1,‘值2’,值3);(数字不用加引号 字串需要)
–不指定字段添加
insert into stu value(值1,‘值2’,值3); (需保持添加数据的对应)
insert into 表
名 value
(值1,‘值2’,值3),
(值1,‘值2’,值3),
(值1,‘值2’,值3);
修改*****
–单数据修改 表中 age为38岁 条件是 name为erya的数据
update 表名 set age=38 where name=erya;
–多数据修改 修改表中name为benboba age为999 条件是 id为3的数据
update stu set name=‘benboba’,age=999 where id=3;
删除
****
–删除stu表中id为4的信息
delete from stu where id=4;
查询*****
select * from 表名; 查询表中所有信息
select 字段名 from 表
名; 查询表中某字段信息

    MySQL数据类型

======================================
MySQL的数据类型分为四大类:数值类型、字串类型、日期类型、NULL。

5.1 数值类型:
	*tinyint(1字节)
	smallint(2字节)
	mediumint(3字节)
	*int(4字节)
	bigint(8字节)
	*float(4字节)   float(6,2)
	*double(8字节)  
	decimal(自定义)字串形数值
	
 5.2 字串类型
	普通字串
	*char  定长字串   	 char(8)  
	*varchar 可变字串 varchar(8)
	
	二进制类型
	tinyblob
	blob
	mediumblob
	longblob
	
	文本类型
	tinytext
	*text      常用于<textarea></textarea>
	mediumtext
	longtext
	
	*enum枚举
	set集合
	
5.3 时间和日期类型:
	date  年月日
	time  时分秒
	datetime 年月日时分秒
	timestamp 时间戳
	year 年

5.4 NULL值
	NULL意味着“没有值”或“未知值”
	可以测试某个值是否为NULL
	不能对NULL值进行算术计算
	对NULL值进行算术运算,其结果还是NULL
	0或NULL都意味着假,其余值都意味着真

======================================
MySQL字段约束

表的字段约束:
unsigned 无符号(正数)
zerofill 前导零填充
auto_increment 自增
default 默认值
not null 非空
PRIMARY KEY 主键 (非null并不重复)
unique 唯一性 (可以为null但不重复)
index 常规索引
//设置typeid为type表中的id的外键///typeid int references type(id),

find_in_set的用法:
现在有篇文章他既是 头条,又是热点,还是图文,
type中以 1,3,4的格式存储.
们我们如何用sql查找所有type中有4图文标准的文章呢??
这就要我们的find_in_set出马的时候到了.
以下为引用的内容:
select * from article where FIND_IN_SET(‘4’,type)

group_concat的用法:
group_concat()会计算哪些行属于同一组,将属于同一组的列显示出来。要返回哪些列,由函数参数(就是字段名)决定。分组必须有个标准,就是根据group by指定的列进行分组。
select group_concat(town) from players group by town


  1. a-z ↩︎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值