四、数据字段属性    
        unsigned  可以让空间增加一倍(有符号转无符号)  -128~127    0~255
            //只能用于数据值型字段
            //Eg_1;
        zerofill     只能用于数值字段,前导0??
            //就是前面补0    自动加上unsigned
            //有符号是不能加上前导0
            //Eg_2;
        auto_increment
            //只能是整数,数据每增加一条,,自动加一,,不允许重复
            //    NULL 0 留空        每个字段最好都有一个ID这个东西
            //Eg_3;
                /*ERROR 1075 (42000): Incorrect table definition; there can be only one auto colum
                    n and it must be defined as a key    这个错误...诶竟然要创建什么索引..*/
        null not null 默认为空 NULL
            //将来将这个表转换为php的程序的时候,    不一定转换成神农吗东东,,,,
            //建议不要创建空值
            //非空还可以用用~~~~~
        default  缺省值
            //(mon floot(5,3) not null default '0.00');缺省的值弄
            
    CREATE TABLE users(
            id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
            name VARCHAR(30) NOT NULL DEFAULT '',
            height DOUBLE(10,2) NOT NULL DEFAULT 0.00,
            age INT NOT NULL DEFAULT 0,
            sex char not null default ''
    );
            
        
    五、创建索引
        1.主键索引    [primary key]    PRI
            确定数据表里面一条特定数据记录的位置;
            //这一列,,完全不允许重复
            //最好为每一张数据表定义一个主键索引
            
            //***一个表只能定义一个主键,,,设置的值不可为空****
            **primary key是可以为主键定义一个可以在最后创建 如*****
                /*在最后制定 primary key的值
                CREATE TABLE users(
                        id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
                        name VARCHAR(30) NOT NULL DEFAULT '',
                        PRIMARY KEY(id)
                );*/
        2.唯一索引    [unique]    UNI
            *都可以防止创建重复的值
            *每个表都可以有多个唯一索引
            //Eg_5
        3.常规索引    [index]        MUL
            ***最重要的技术,,常规索引,,可以提升数据库的性能//如果优化首先选择常规索引
            /*例:
                111
                222
                ...
                一万多条            怎找???太多了 就像是图书馆一样*/
            *可以调高查找的速度,但是插入,删除,修改  会很慢!!//就像是大仓库和图书馆~
            
            可以单独使用,也可以在创建表的时候单独的使用..(不懂)//和table是一个级别的
            // create index ind1 on users(name,age);
            // drop index ind1 on users;
            * index 和 key是同义词;
            *可以多列
            //Eg_6
        4.全文索引    [fulltext]
            fulltext类型索引,    只能用在MyISAM上...,只有在varchar char text上使用!!
            *也可以多个数据列使用
                create table books(
                    id int,
                    bookname varchar(30),
                    price double,
                    detail text not null,
                    fulltext(detail,bookname),
                    index ind(price),
                    primay key(id));
                select * from books where bookname like '%php%';//之前
                select bookname,price from books where MATCH(detail) AGAINST('php');//之后查询的方法
            
收货:
    drop table users;//删除表用
    unsigned 是转有符号为无符号;
    not null default这两个连着用(mon floot(5,3) not null default '0.00')
    /*创建一个能用的表
        CREATE TABLE users(
                id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
                name VARCHAR(30) NOT NULL DEFAULT '',
                height DOUBLE(10,2) NOT NULL DEFAULT 0.00,
                age INT NOT NULL DEFAULT 0,
                sex char not null default '男'
        );
    */        
    primary key是可以为主键定义一个可以在最后创建 如
        /*在最后制定 primary key的值
        CREATE TABLE users(
                id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
                name VARCHAR(30) NOT NULL DEFAULT '',
                PRIMARY KEY(id)
        );*/
    
            
Eg_1:
    use zp;
    create table t1(id int unsigned);
    insert into t1 values(100);
    insert into t1 values(-100);
    select * from t1;
        ↓↓    ↓↓    ↓↓    ↓↓
            +------+
            | id   |
            +------+
            |  100 |
            |    0 |
            +------+//没有符号  最小为零
            
Eg_2:
    create table t2(num1 int(5) zerofill,num2 float(5,2) zerofill,num3 char(5));
    /*    ↓        ↓↓        ↓↓        ↓↓        ↓↓        ↓
        +-------+------------------------------+------+-----+---------+-------+
        | Field | Type                         | Null | Key | Default | Extra |
        +-------+------------------------------+------+-----+---------+-------+
        | num1  | int(5) unsigned zerofill     | YES  |     | NULL    |       |
        | num2  | float(5,2) unsigned zerofill | YES  |     | NULL    |       |
        | num3  | char(5)                      | YES  |     | NULL    |       |
        +-------+------------------------------+------+-----+---------+-------+*/
    insert into t2__2(num1,num2,num3) values(123456,123456.123,'asdfghj');
    /*    ↓        ↓↓        ↓↓        ↓↓        ↓↓        ↓
        +--------+--------+-------+
        | num1   | num2   | num3  |
        +--------+--------+-------+    这里就int类型超过了设置的5这个值,,其他都没有超过,,为什么呢? 
        | 123456 | 999.99 | asdfg |     因为这个是zerofill的前提,,,看下面例子
        +--------+--------+-------+*/
    insert into t2__2(num1,num2,num3) values(123,12.1,'sdfsdf');
    /*    ↓        ↓↓        ↓↓        ↓↓        ↓↓        ↓
        +--------+--------+-------+
        | num1   | num2   | num3  |
        +--------+--------+-------+
        | 123456 | 999.99 | asdfg |
        |  00123 |  12.10 | sdfsd |前面补了零~~~一共5个数字 要这个干嘛~?
        +--------+--------+-------+*/
Eg_3:
    create table t3(id int auto_increment primary key,name char(10));//primary key必须加??加上去是因为不能让他重复!!!
    /*这个key 和null 和extra有亮点~~
        +-------+----------+------+-----+---------+----------------+
        | Field | Type     | Null | Key | Default | Extra          |
        +-------+----------+------+-----+---------+----------------+
        | id    | int(11)  | NO   | PRI | NULL    | auto_increment |
        | name  | char(10) | YES  |     | NULL    |                |*/
    insert into t3 values('aaaa');、 insert into t3 values(null,'aaaa');//插入多次
    /*从插入过的最大值后增加 delete后也是这样                null是可以设置最大值
            +----+------+
            | id | name |
            +----+------+
            |  1 | sss  |
            |  2 | sss  |
            |  3 | sss  |
            |  4 | sss  |
            |  5 | sss  |
            |  6 | sss  |
            |  7 | sss  |
            |  8 | sss  |
            +----+------+    */

Eg_5:
    mysql> create table users(
        -> id int not null auto_increment,
        -> name varchar(30) not null default '' unique,
        -> age int,
        -> primary key (id));
    /*
        +-------+-------------+------+-----+---------+----------------+
        | Field | Type        | Null | Key | Default | Extra          |
        +-------+-------------+------+-----+---------+----------------+
        | id    | int(11)     | NO   | PRI | NULL    | auto_increment |
        | name  | varchar(30) | NO   | UNI |         |                |
        | age   | int(11)     | YES  |     | NULL    |                |
        +-------+-------------+------+-----+---------+----------------+*/
    insert into users(name,age) values('aa',20);//成功
    insert into users(name,age) values('aa',20);//再次创建失败~  Duplicate entry 'aa' for key 'name'
    
Eg_6:
    mysql> create table carts(
        -> id int not null,
        -> uid int not null,
        -> sid int not null,
        -> number int not null
        -> primary key(id),
        -> key c_uid (uid),
        -> index c_sid(sid));
    /*        ↓↓    ↓↓    ↓↓    ↓↓
    +--------+---------+------+-----+---------+-------+
    | Field  | Type    | Null | Key | Default | Extra |
    +--------+---------+------+-----+---------+-------+
    | id     | int(11) | NO   | PRI | NULL    |       |
    | uid    | int(11) | NO   | MUL | NULL    |       |
    | sid    | int(11) | NO   | MUL | NULL    |       |
    | number | int(11) | NO   |     | NULL    |       |
    +--------+---------+------+-----+---------+-------+*/
    mysql> create table carts(
        -> id int not null,
        -> uid int not null,
        -> sid int not null,
        -> index c_su_id(sid,uid));//可以写多个在一起
    

 

posted on 2013-09-29 11:59  西瓜异族  阅读(277)  评论(0编辑  收藏  举报