时间:2014年3月27日20:08:01网站建表实战与优化意识


建表:

SNS社交网站:

会员表:member

Mysql中没有布尔型,相当于tinyint类型 false 就是0 其他都是true

大小 字节

列名称

列类型

默认值

是否主键

4

编号id

int


Primary key


用户名:uesrname

varchar(20)  char(20)

‘’


1

性别:gender

char(1) tinyint



1

体重KGweight

tinyint unsigned 0--255



3

生日:birthday

date



8

工资:salary

Decimal(8,2)



8

上次登录时间:lastlogin

Datetime  int




个人简介intro

Varchar(1500)




日志 blog

text



数据每一行的长度,都是固定的,查找的速度更快

浪费一定空间,从而加快速度值,但不能浪费过大

存储空间和时间时矛盾的的

时间换空间 空间换时间

所以intro 可以单独拿出来,另放到一张表里

大小 字节

列名称

列类型

默认值

是否主键

4

编号id

int


Primary key


用户名:uesrname

varchar(20)  char(20)

‘’



个人简介intro

Varchar(1500)



在开发中,会员的信息优化,把频繁用到的信息,优先考虑效率,存储到一张表中,

不常用的信息,或比较占据空间的信息,优先考虑空间占据,存储到辅表中

上次登录时间,从查询角度和计算来看,利用int时间戳进行计算

建表语法:所谓建表就是声明列的过程

create table 表名(

列1声明 列1参数,

列1声明 列1参数,

... ...

列n声明 列n参数

)engine myisam/innodb/bdb  charset utf8/gbk/latin1;

create table member(

id int unsigned auto_increment primary key,

username char(20) not null default '',

gender char(1) not null default '',

weight tinyint unsigned not null default 0,

birthday date not null default '0000-00-00',

salary decimal(8,2) not null default 0

)engine myisam charset utf8;

时间:201432817:11:02表修改语法之列的增删改

mysql> #修改表的语法:

mysql> #一张表,创建完毕,有了N个列

mysql> #之后还有可能增加或者删除列

mysql> #使用语句:

mysql> #alter table 表名 add 列名 列类型 列参数

mysql> use test;

Database changed

mysql> create table m1(

   -> id int unsigned auto_increment primary key

   -> )engine myisam charset utf8;

Query OK, 0 rows affected (0.08 sec)

mysql> desc m1;

+-------+------------------+------+-----+---------+----------------+

| Field | Type             | Null | Key | Default | Extra          |

+-------+------------------+------+-----+---------+----------------+

| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |

+-------+------------------+------+-----+---------+----------------+

1 row in set (0.05 sec)

mysql> alter table m1 add username char(20) not null default '';

Query OK, 0 rows affected (0.07 sec)

Records: 0  Duplicates: 0  Warnings: 0

mysql> desc m1;

+----------+------------------+------+-----+---------+----------------+

| Field    | Type             | Null | Key | Default | Extra          |

+----------+------------------+------+-----+---------+----------------+

| id       | int(10) unsigned | NO   | PRI | NULL    | auto_increment |

| username | char(20)         | NO   |     |         |                |

+----------+------------------+------+-----+---------+----------------+

2 rows in set (0.01 sec)

mysql> #关键熟悉列类型

mysql> alter table m1 birthday date not null default '0000-00-00';

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'birthday date not null default '0000-00-00'' at line 1

mysql> alter table m1 add birthday date not null default '0000-00-00';

Query OK, 0 rows affected (0.02 sec)

Records: 0  Duplicates: 0  Warnings: 0

mysql> desc m1;

+----------+------------------+------+-----+------------+----------------+

| Field    | Type             | Null | Key | Default    | Extra          |

+----------+------------------+------+-----+------------+----------------+

| id       | int(10) unsigned | NO   | PRI | NULL       | auto_increment |

| username | char(20)         | NO   |     |            |                |

| birthday | date             | NO   |     | 0000-00-00 |                |

+----------+------------------+------+-----+------------+----------------+

3 rows in set (0.01 sec)

mysql> #发现gender性别列忘了加上,想加到username后,

mysql> alter table m1 add gender char(1) not null default '' after username;

ERROR 1067 (42000): Invalid default value for 'gender'

mysql> set names gbk;

Query OK, 0 rows affected (0.02 sec)

mysql> alter table m1 add gender char(1) not null default '' after username;

Query OK, 0 rows affected (0.08 sec)

Records: 0  Duplicates: 0  Warnings: 0

mysql> desc m1;

+----------+------------------+------+-----+------------+----------------+

| Field    | Type             | Null | Key | Default    | Extra          |

+----------+------------------+------+-----+------------+----------------+

| id       | int(10) unsigned | NO   | PRI | NULL       | auto_increment |

| username | char(20)         | NO   |     |            |                |

| gender   | char(1)          | NO   |     | 男         |                |

| birthday | date             | NO   |     | 0000-00-00 |                |

+----------+------------------+------+-----+------------+----------------+

4 rows in set (0.02 sec)

mysql> #在指定的列后,需要使用after

mysql> #如果想放到第一位,id前面

mysql> #first

mysql> #alter table m1 add pid int not null default 0 first;

mysql> desc m1;

+----------+------------------+------+-----+------------+----------------+

| Field    | Type             | Null | Key | Default    | Extra          |

+----------+------------------+------+-----+------------+----------------+

| id       | int(10) unsigned | NO   | PRI | NULL       | auto_increment |

| username | char(20)         | NO   |     |            |                |

| gender   | char(1)          | NO   |     | 男         |                |

| birthday | date             | NO   |     | 0000-00-00 |                |

+----------+------------------+------+-----+------------+----------------+

4 rows in set (0.01 sec)

mysql> alter table m1 add pid int not null default 0 first;

Query OK, 0 rows affected (0.06 sec)

Records: 0  Duplicates: 0  Warnings: 0

mysql> desc m1;

+----------+------------------+------+-----+------------+----------------+

| Field    | Type             | Null | Key | Default    | Extra          |

+----------+------------------+------+-----+------------+----------------+

| pid      | int(11)          | NO   |     | 0          |                |

| id       | int(10) unsigned | NO   | PRI | NULL       | auto_increment |

| username | char(20)         | NO   |     |            |                |

| gender   | char(1)          | NO   |     | 男         |                |

| birthday | date             | NO   |     | 0000-00-00 |                |

+----------+------------------+------+-----+------------+----------------+

5 rows in set (0.01 sec)

mysql> #把新列加到最前面,使用frist

mysql> #删除列,

mysql> alter table m1 drop pid;

Query OK, 0 rows affected (0.02 sec)

Records: 0  Duplicates: 0  Warnings: 0

mysql> desc m1;

+----------+------------------+------+-----+------------+----------------+

| Field    | Type             | Null | Key | Default    | Extra          |

+----------+------------------+------+-----+------------+----------------+

| id       | int(10) unsigned | NO   | PRI | NULL       | auto_increment |

| username | char(20)         | NO   |     |            |                |

| gender   | char(1)          | NO   |     | 男         |                |

| birthday | date             | NO   |     | 0000-00-00 |                |

+----------+------------------+------+-----+------------+----------------+

4 rows in set (0.01 sec)

mysql> #修改列

mysql> alter table m1 modify gender char(4) not null default '';

Query OK, 0 rows affected (0.03 sec)

Records: 0  Duplicates: 0  Warnings: 0

mysql> desc m1;

+----------+------------------+------+-----+------------+----------------+

| Field    | Type             | Null | Key | Default    | Extra          |

+----------+------------------+------+-----+------------+----------------+

| id       | int(10) unsigned | NO   | PRI | NULL       | auto_increment |

| username | char(20)         | NO   |     |            |                |

| gender   | char(4)          | NO   |     |            |                |

| birthday | date             | NO   |     | 0000-00-00 |                |

+----------+------------------+------+-----+------------+----------------+

4 rows in set (0.01 sec)

mysql> #modify 不能修改列名,

mysql> #修改列名和列类型,需要使用change

mysql> alter table m1 change id uid int unsigned;

Query OK, 0 rows affected (0.02 sec)

Records: 0  Duplicates: 0  Warnings: 0

mysql> desc m1;

+----------+------------------+------+-----+------------+-------+

| Field    | Type             | Null | Key | Default    | Extra |

+----------+------------------+------+-----+------------+-------+

| uid      | int(10) unsigned | NO   | PRI | 0          |       |

| username | char(20)         | NO   |     |            |       |

| gender   | char(4)          | NO   |     |            |       |

| birthday | date             | NO   |     | 0000-00-00 |       |

+----------+------------------+------+-----+------------+-------+

4 rows in set (0.01 sec)

mysql> #主键在,自增长丢失

mysql> 如果列类型改变,导致数据存不下怎么办?

   -> #主键在,自增长丢失

   ->

   -> exit

   -> ;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '如果列类型改变,导致数据存不下怎么办?

exit' at line 1

mysql> #主键在,自增长丢失

mysql> #如果列类型改变,导致数据存不下怎么办?

mysql> #比如,int改为smallint ,如果不匹配,数据将会丢失

mysql> #或者在mysqlstrict mode下,修改不了

mysql> alter table m1 modify gender char(1) not null default '';

Query OK, 0 rows affected (0.03 sec)

Records: 0  Duplicates: 0  Warnings: 0

mysql> desc m1;

+----------+------------------+------+-----+------------+-------+

| Field    | Type             | Null | Key | Default    | Extra |

+----------+------------------+------+-----+------------+-------+

| uid      | int(10) unsigned | NO   | PRI | 0          |       |

| username | char(20)         | NO   |     |            |       |

| gender   | char(1)          | NO   |     |            |       |

| birthday | date             | NO   |     | 0000-00-00 |       |

+----------+------------------+------+-----+------------+-------+

4 rows in set (0.01 sec)

mysql> web操作的读写比是20:1

   -> exit;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'web操作的读写比是20:1

exit' at line 1


你可能感兴趣的:(false,用户名,会员,intro,个人简介)