数据库学习
数据类型:
字符串型:
- char(m) 定长字符串,可以使用定长的情况:手机号码,身份证号码,性别,,,固定长度值的情况(会浪费空间。)
- varchar(m) 变长字符串【常用】,非固定长度的是使用变长字符串(小于256个字);
文本类型:
- text ; 标准文本 64KB
- long text: 长文本 4GB
日期时间型:
- datetime: 日期时间型,占8个字节
- timestamp: 【常用】时间戳,占四个字节(默认值当前时间)
create table date_1(
d1 datetime,
d2 timestamp default current_timestamp
);
字段属性:
1.作用:用于对字段进行修饰(允许字段设置附加属性)
NULL属性:
允许字段为空;
not null 不允许为空
示例: create table null_1( username archer(20), gender char(1)not null ); insert into null_1 values('张三',null); insert into null_1 values('张三','男 ');
Dfault 属性:
设置字段的默认值,当插入此新数据时,如果没有为该字段赋值自动使用默认值
实例: create table default_1( username varchar(20) default `匿名`, gender char(2) ); insert into default_1 value(`张三`,'NULl'); //default关键词,插入数据时使用default 将强制使用认值 insert into default_1 values(default,'章三')
特殊的默认值:
timestamp 时间戳类型,默认值是: current_timestamp on update current_timestamp //时间戳的常用写法【重要】: timestamp default current_timrrstamp alter table default_1 modify column reg_time timestamp default current_timestamp
主键(primary key)属性:通常用于数据表示(ID字段)
1)特性:
1.一个表中只允许有一个主键;
2.主键修饰的字段不能重复;
3.值不能为null
create table classes(
id int primary key auto_increment,
classesname varchar(20),
classesroom char(3)
);
insert into classes values(NULL,'php0217','607');
insert into classes values(null,'web0231','606');
Auto increment
:自动增长:通常和主键一起使用;
第一个章节:
通过命令行链接数据库:
mysql.exe -hlocalhost -uroot(用户名) -proot(密码)
第二个章节:
- 数据库操作:
- 创建数据库
create database 数据库名
- 显示数据库列表
show databases;
- 数据表操作:
- 创建数据表
create table table_name(
字段名1 字段类型 属性
字段名2 字段类型 属性
字段名n 字段类型 属性
);
create table pages(
id BIGINT(7) NOT NULL AUTO_INCREMENT,
title VARCHAR(200),
content VARCHAR(10000),
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,PRIMARY KEY(id)
);
create table pages(
id BIGINT(7) NOT NULL AUTO_INCREMENT,
title varchar(200),
content VARCHAR(10000),
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(id)
);
- 显示数据表清单:
show tables;
- 记录的操作:【重点中的重点 】
- 新增的记录(增)
insert into 表名(字段列表)values(值的列表);//值和字段一一对应
insert into pages (title,content) values("the second title","It's a guessbook and it has nothing");
- 删除记录(删)
delete from 表名 where 条件子句;
delete from pages where id=4;
- 修改记录(改)
update 表名 set 字段名=新的值,字段名2=新的值2 where 条件子句;//做删除和修改操作时,一定要万分小心,提前备份
update pages set content="It has been updated" where id=3;
- 查询记录(查)
select [字段列表] [from子句][where条件子句][order by排序子句][limit 限定子句]
select * from pages;
其他子句
like字句
like
类似于“=”
,常常与%
连用
select * from pages where content like "%has been%";
UNION字句:
select user,password from admin where id=1 union select title,date from news where id=2;
ORDER BY字句:
select * from news order by title;
第三个章节
1.整数型:
int 整型tinyint 微整形unsigned 无符号
2.小数
float(m,d) 单精度小数
3.字符串
char(m) 定长字符串varchar(m) 变长字符串
4.文本
text 标准文本long text 长文本
5.日期时间
datetime 日期时间timesatamp 时间戳
导出数据库:
1.回到dos命令行下
2.执行命令
mysqldump -u用户名 -p密码 数据库名 > 导出的地址和文件名
导入数据库
前提:必须具备.sql;
1.进入mysql命令行模式;
2.source sql文件路径;
4. PHP和数据库
1.配置php支持Mysql数据库
分别独立安装的php和Mysql并不会自动支持,需要做以下决定:
打开php配置文件 php.ini;
去除extension=php_mysql.dll和extension=php_mysqli.dll前面的分号
重启Apache服务器即可加载mysql拓展
注意:mysql,mysqli,pdo,odbc 都是php的数据库扩展程度,根据需要选用;
2.使用php连接数据库
在php中,使用函数连接mysql
语法:
使用php函数,mysql_connect()连接上数据库;
$conn=mysql_connect(‘主机名’,‘用户名’,‘密码’,‘数据库名’);
$conn称为数据库连接标识 ;
//连接数据库一壶,返回数据库标识,赋值给$conn,$conn称为数据库连接标示
$conn=@mysqli_connect('localhost','root','root','itsource') or die('错误');
3.使用php访问数据库:
语法:
mysqli_query(数据库连接标识,sql指令);
$result称为【结果集】
//使用mysql_connct连接上mysql数据库
$conn=@mysqli_connect('localhost','root,'root) or die('错误');
//使用mysqli_query()函数讲sql指令发送到数据库去查询,将返回的结果封装成对象作为函数的返回值,放在变量集中,变量叫【结果集】
$resualt=mysqli_query($counn,'show database');
4.结果集处理:【重点中的重点】
有返回值的情况:
执行成功返回资源类型的兑现,失败返回false;
有返回值的命令:select show desc
由于资源类型永远为真,不能用于判断是否有数据,只能用于判断命令是否成功;
没有返回值的情况:
执行成功返回true,是被返回false;
没有返回值的命令:insert update delete set create drop alter
从结果集中读取数据:
使用php函数 mysqli_fetch_assoc() 从结果剧中读取数据,将读取到的1条数据已关联数组的方式返回 关联数组的键名 就是字段名,同时,结果集指针向下移动一行;
语法:
mysqli_fetch_assoc(结果集);
$connect=@mysqli_connect('localhost','root','root','itsource') or die ('error');
mysqli_query($conn,'set names utf-8');//设置php与mysqli默认交互字符集
$rs=mysql_query($conn,'show database');
$rs=mysqli_query($conn,'selsct *from student order by height desc limit 3');
//使用mysqli_fetch_assoc()函数读取一条数据,并且数据指针移向下一行;
$row=mysqli_fetch_assoc($rs);
//使用while循环,逐条从结果集中读取
while($row=mysqli_fetch_assoc($rs)){
echo 'ID'.$row['id'].'<br/>';
echo '姓名:'.$row['stu_name'].'<br/>';
}
5.关闭数据库连接;
语法:
mysqli_close(数据库连接标识);
$conn=@mysqli_connecy('localhost','root','root','数据库名';);
//mysqli_query($conn,use 数据库名);
mysqli_select_db($conn,数据库名);
//在mysql数据库中使用utf8,
mysqli_query($conn,'set names utf8');
//构造sql语句法网数据库执行,将执行的结果返回到¥rs变量中,$rs叫结果集;
$rs=mysqli_query($conn,'show database');
//通过while循环 去结果集中读取所有数据并显示出来;
while($row=mysqli_fetch_assoc($rs)){
echo $row['database'].'<br/>';
}
//释放结果集所占内存空间
mysqli_free_result($rs);
//关闭数据库连接
mysqli_close(close);
6.其他用法:
1.释放结果集:
语法:
mysqli_free_result($rs);
2.错误信息:
- 错误提示:
语法:mysqli_error(数据库连接标识);
echo mysqli_error($conn);
3.从结果集中读取数据:
mysqli_fetch_assoc() 获取一行数组作为【关联】数组
mysqli_fetch_row(). 获取一行数据作为【索引】数组返回
mysqli_fetch_array() 前面两个数组相加
4.统计结果集中的数据的行数:
- 语法:
mysqli_num_rows(结果集);
通常用于判断结果集中是否有数据,如果是大于0就证明还有数据,等于0就是没数据;
5.结果集指针:
语法:
- mysqli_data_seek(结果集,要移动的位置);
mysqli_data_seek(结果集,要移动的位置);
mysqli_data_seek($rs,1);
6.取得刚查入的数据ID
方法一:
mysqli_query($conn."select@@IDENTITY");
方法二:
mysqli_query_id($conn,"");
5. 编码
1.php与浏览器之间交互的编码:
header('Content-Type:text/html,charset=utf8');
2.php文件保存的编码utf8,在编辑器中设置
3.数据库保存时,用utf-8;
show create table student
4.设置php与mysql交互层用的utf8
mysqli_ query($coon,'set name utf8');
5.HTML meta标签声明必须使用utf-8
<meta charset="utf-8"/>
实现注册新用户:
1)形成表单:
<?php
$username=$_post['username'];
$age=$_post['age'];
$gender=$_post['gender'];
$conn=@mysqli_connect('localhost','root','root','itsource')or die ('error');
mysqli_query($conn,'set names utf8');
//构造sql语句实现向student数据表插入一条数据
$sql="insert into student (stu_name,age,gender) value s('$username','$age','$gender')";
$rs=mysqli_query($conn,$sql);
if($rs)
{
echo "恭喜你,注册成功!";
}else{
echo '注册失败';
echo mysqli_error($conn)
}