一、MYSQL
1、配置MySql
第一步安装服务器(apache)。
第二部安装MySql界面程序
2、MySql数据库的数据类型简单介绍常用的几种
整形:int
小数:float、double、decimal
字符串:varchar、char(他俩的区别是创建的长度与具体长度的问题)
bool类型:bit
日期时间:datetime
3、创建表及约束
创建表语句:
create table InFo(
Pwd varchar(50),
Name varchar(50),
Nation varchar(50),
foreign key(Nation) references Nation(Code) );
建表及写sql语句是注意的事项:
·创建表时最后一列不要加逗号
每个表创建完之后加分号
搜友sql语句中的符号必须是英文状态下的
如果多条语句一起执行,注意在语句之间加分号
·创建表要先分类,再根据关系具体分类,添加主外键及约束条件
约束:
创建表中的列时需要确定列名、列的类型、长度、是否为空,是否有默认值、是否为外键、主键、是否自增长(自增长必须是主键,主键可以不自增长)等:
4、数据库的CRUD操作
·添加数据:
insert into Info values('',''......);
可以添加表中色所有列的信息,也可以添加指定列的信息
insert into Info(code) values('p001');
自增长列的内容可以空,但必须留位置
·删除数据:
delete from Info; 删除所有数据
delete frohttp://www,sdyle.comm Info where code = 'n001'; 根据条件删除指定数据
·修改数据:
update Info set code='',...where code = '';
根据where条件修改一行数据中的一个或多个列
·查询数据:
(1)简单查询
select * from Info
select Code as '代号',Name as '姓名'from Info
(2)条件查询
select * from Car where Code='c002'
select * from Car where Brand='b001' and Powers=130 或者用 or
(3)模糊查询
select * from Car where Name like '%奥迪%' %代表任意多个字符_代表一个字符
(4)排序查询
select * from Car order by Brand,Powers desc
(5)范围查询
select * from Car where Price>=40 and Price<=60
select * from Car where Price between 40 and 50
(6)离散查询
select * from Car where Code in ('c001','c003','c005','c007')
select * from Car where Code not in('c001','c003','c005','c007')
(7)聚合函数,统计查询
select sum(Price) from Car #查询所有价格之和 sum()求和
select count(Code) from Car #查询数据条数
select max(Code) from Car #求最大值
select min(Brand) from Car #求最小值
select avg(Price) from Car #求平均值
(8)分页查询
#####每页显示5条数据,取第2页的数据
select * from Car limit (n-1)*5,5
(9)去重查询
select distinct Brand from Car
(10)分组查询
select count(*),Brand from Car group by Brand
select Brand from Car group by Brand having count(*)>3 #分组之后根据条件查询使用having 不使用where
(11)连接查询
select * from Info,Nation #形成笛卡尔积
select Info.Code,Info.Name,Info.Sex,Nation.Name,Info.Birthday from Info,Nation where Info.Nation = Nation.Code
select * from Info join Nation
select * from Info join Nation on Info.Nation = Nation.Code
(12)联合查询
select Code,Name from Info
union
select Code,Name from Nation
(13)子查询
(1)无关子查询
外层查询 (里层查询)
子查询的结果当做父查询的条件
子查询:select Code from Nation where Name='汉族'
父查询:select * from Info where Nation = ''
select * from Info where Nation = (select Code from Nation where Name='汉族')
(2)相关子查询
查询汽车表中油耗低于该系列平均油耗的所有汽车信息
父查询:select * from Car where Oil<(该系列平均油耗)
子查询:select avg(Oil) from Car where Brand = '某个系列'
select * from Car a where Oil<(select avg(Oil) from Car b where b.Brand = a.Brand )
5、数据库常用函数
数学函数
RAND() 返回0->1的随机数
SELECT RAND() --0.93099315644334
RAND(x) 返回0->1的随机数,x值相同时返回的随机数相同
SELECT RAND(2) --1.5865798029924
·字符串函数
CHAR_LENGTH(s) 返回字符串s的字符数
SELECT CHAR_LENGTH('你好123') -- 5
CONCAT(s1,s2,...) 将字符串s1,s2等多个字符串合并为一个字符串
SELECT CONCAT('12','34') -- 1234
·日期时间函数
NOW() 返回当前日期和时间
SELECT NOW() ->2014-12-17 15:59:02
二、PHP基础
1、基础语法
·注释://单行注释 /*多行注释*/
·输出语句:echo、print、print_r,var_dump
echo是语句只负责输出
print、print_r是函数,有返回值
print只能打印简单类型变量的值,如int,string类型
print_r可以打印复杂类型的值,如数组,对象
var_dump可以打印详细信息。
·数据类型及变量
PHP是弱类型语言,变量没有明确的类型
定义变量:
$a = 10;
$b = 'hello'; //单引号不能解析里面的内容,容易报错
c = "wo{c="wo{a}rd"; //双引号可以解析
$d = >>AA //完整输出内容,不需要解析
"hello","word"
AA;
isset($a); //判断变量是否定义
unset($a); //清除变量
empty($a); //判断变量是否为空
运算符
关系运算符:其中数字与字符串相加,字符串为零计算,字符串与字符串相加为零
整数除整数可以为小数,因为PHP为弱类型语言
比较运算符:
逻辑运算符:
错误抑制符:@
·语句
分支:
循环:
·函数
函数四要素,返回类型,函数名,参数列表,函数体
PHP中不需要返回类型,使用function关键字
普通函数:funtion Show( a)有默认值