表操作,DDL
表本身,与表结构的操作!
=====================
创建,create table
---------------
create table 表名 (
字段的定义
) [表选项];
其中表名,一定先要确定数据库!因此一个典型的表名是由两部分组成:
所在库.表名
test.itcast test库内itcast表
itcast.stu itcast库内的stu表
但是我们可以设置默认数据库,如果不指定则使用默认数据库(当前数据库)
use 数据库名。选择默认数据库!
在使用表名但是没有指明其所在数据库时,默认数据库才会起作用!

在itcast库内创建:
use itcast ; create table stu;
或者
create table itcast.stu
1. `use itcast;`
2. `create table stu (`
3. `id int,`
4. `name varchar(255)`
5. `) engine=innodb charset=utf8;`
其中字段的部分
字段才是最终的数据的载体(与变量的概念是类似的,都是基本保存数据的),mysql的是强类型,字段的类型是固定的,提前定义好的!
因此,在定义字段时,至少要字段名和字段类型!
两种最基本的mysql数据类型(int, varchar,varchar必须指定最大长度字符为单位)
表选项部分
典型的常用的表选项有:
字符集(校对集),表引擎。

================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
查看,show,desc
------------
### 查看所有的表有哪些
show tables like ‘模式’

1. `show tables like 'stu';`
======================================================================================================================================================
### 查看具体的建表语句
show create table table\_name

1. `show create table stu;`
在mysql的命令行客户端,如果数据过多,不容易展示!
可以使用 \\G 作为语句结束符!

===============================================================================================================================================================================================================================================================================================
### 查询表结构
desc 表名
desc describe的简写

===================================================================================================
修改,alter table
--------------
### 修改表选项
alter table table\_name [新选项]

1. `alter table stu engine=myisam charset=gbk;`
=================================================================================================================================================================================
### 改表名
rename table原表名 to 新表名。

注意,表名可以由库名.表名形式的!
因此,可以跨库修改表名: 只要在表名前增加库名即可

1. `rename table stu to hero;`
2.
3. `rename table hero to test.stu;`
============================================================================================================================================================================================================================================================================================================================
### 修改表结构,表内字段
#### 增加字段
alter table table\_name add column 字段定义 [字段位置]
增加一个 age字段:


增加一个 height 在 name之后:
使用关键字 after some\_column\_name;

在最开始增加sn字段
使用关键字,first

1.
2. `alter table stu add column age int;`
3.
4. `alter table stu add column height int after name;`
5.
6. `alter table stu add column sn varchar(10) first;`
===================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
#### 删除字段
alter table table\_name drop column column\_name;

1. `alter table stu drop column height;`
========================================================================================================================================================================================================
#### 修改已有字段(修改字段定义)
alter table table\_name modify column column\_name 新的定义!
可以修改位置

1. `alter table stu modify column sn int after name;`
=======================================================================================================================================================================================================================
#### 字段改名
alter table table\_name change column 原字段名新字段名新字段定义!
注意,不是纯粹的改名,而是需要在修改定义的同时改名!

1. alter table stu change column sn stu\_sn varchar(255) after age;
==========================================================================================================================================================================================================================================================
删除,drop table
-------------

1. `drop table stu;`
drop table if exists
如果存在就删除
create table if not exists!
如果不存在就创建
================================================================================================================================================================================================================================================
查询表:
----
table.php
利用 mysql\_num\_rows($result)可以获得 结果集中的记录数:

展示一个增加表名与记录数的小表单:

=============================================================================================================================================================================================================================
显示创建table的表单
------------
table\_create\_form.php

===============================================================================================
处理提交数据:
-------
应该为每组数据形成一个唯一的标识!采用 数组下标的形式!


循环对数据做判断,然后依次拼凑sql语句:
================================================================================================================================================================================================================