表 - 增刪改查

表得增刪改查:
1.表介紹:

                    

 

        id,name,age,sex稱爲字段,其他的,一行內容稱爲一條記錄。    2.建立表:        語法:            create table 表名(            字段名1 類型[(寬度) 約束條件],            字段名2 類型[(寬度) 約束條件],            字段名3 類型[(寬度) 約束條件]            );            #注意:            1. 在同一張表中,字段名是不能相同            2. 寬度和約束條件可選            3. 字段名和類型是必須的        示範:            create database db1 charset utf8;            use db1;            create table t1(                id int,                name varchar(50),                sex enum('male','female'),                age int(3)             );            show tables; #查看db1庫下全部表名            desc t1;            select id,name,sex,age from t1;            select * from t1;            select id,name from t1;            insert into t1 values            (1,'egon','male',18),            (2,'alex','female',20)            ;            insert into t1(id) values            (4),            (5);            mysql> select *  from t1;            +------+------+--------+------+            | id   | name | sex    | age  |            +------+------+--------+------+            |    1 | egon | male   |   18 |            |    2 | alex | female |   20 |            |    4 | NULL | NULL   | NULL |            |    5 | NULL | NULL   | NULL |            +------+------+--------+------+            注意:表中的最後一個字段不要加逗號    3.查看錶結構:        [db1]> describe t1; #查看錶結構,可簡寫爲desc 表名        +-------+-----------------------+------+-----+---------+-------+        | Field | Type                  | Null | Key | Default | Extra |        +-------+-----------------------+------+-----+---------+-------+        | id    | int(11)               | YES  |     | NULL    |       |        | name  | varchar(50)           | YES  |     | NULL    |       |        | sex   | enum('male','female') | YES  |     | NULL    |       |        | age   | int(3)                | YES  |     | NULL    |       |        +-------+-----------------------+------+-----+---------+-------+        [db1]> show create table t1\G; #查看錶詳細結構,可加\G    4.修改表結構:        語法:            1. 修改表名                ALTER TABLE 表名                                    RENAME 新表名;            2. 增長字段                  ALTER TABLE 表名                                      ADD 字段名  數據類型 [完整性約束條件…],                                      ADD 字段名  數據類型 [完整性約束條件…];                  ALTER TABLE 表名                                      ADD 字段名  數據類型 [完整性約束條件…]  FIRST;                  ALTER TABLE 表名                                      ADD 字段名  數據類型 [完整性約束條件…]  AFTER 字段名;            3. 刪除字段                  ALTER TABLE 表名                                      DROP 字段名;            4. 修改字段                  ALTER TABLE 表名                                      MODIFY  字段名 數據類型 [完整性約束條件…];                  ALTER TABLE 表名                                      CHANGE 舊字段名 新字段名 舊數據類型 [完整性約束條件…];                  ALTER TABLE 表名                                      CHANGE 舊字段名 新字段名 新數據類型 [完整性約束條件…];        示範:            1. 修改存儲引擎                mysql> alter table service                    -> engine=innodb;            2. 添加字段                mysql> alter table student10                    -> add name varchar(20) not null,                    -> add age int(3) not null default 22;                mysql> alter table student10                    -> add stu_num varchar(10) not null after name;                //添加name字段以後                mysql> alter table student10                    -> add sex enum('male','female') default 'male' first;          //添加到最前面            3. 刪除字段                mysql> alter table student10                    -> drop sex;                mysql> alter table service                    -> drop mac;            4. 修改字段類型modify                mysql> alter table student10                    -> modify age int(3);                mysql> alter table student10                    -> modify id int(11) not null primary key auto_increment;    //修改成主鍵            5. 增長約束(針對已有的主鍵增長auto_increment)                mysql> alter table student10 modify id int(11) not null primary key auto_increment;                ERROR 1068 (42000): Multiple primary key defined                mysql> alter table student10 modify id int(11) not null auto_increment;                Query OK, 0 rows affected (0.01 sec)                Records: 0  Duplicates: 0  Warnings: 0            6. 對已經存在的表增長複合主鍵                mysql> alter table service2                    -> add primary key(host_ip,port);            7. 增長主鍵                mysql> alter table student1                    -> modify name varchar(10) not null primary key;            8. 增長主鍵和自動增加                mysql> alter table student1                    -> modify id int not null primary key auto_increment;            9. 刪除主鍵                a. 刪除自增約束                mysql> alter table student10 modify id int(11) not null;                b. 刪除主鍵                mysql> alter table student10                    -> drop primary key;    5.複製表:        1.複製表結構+記錄 (key不會複製: 主鍵、外鍵和索引)            mysql> create table new_service select * from service;        2.只複製表結構            mysql> select * from service where 1=2;        //條件爲假,查不到任何記錄                Empty set (0.00 sec)            mysql> create table new1_service select * from service where 1=2;  #(key不會複製: 主鍵、外鍵和索引)                Query OK, 0 rows affected (0.00 sec)                Records: 0  Duplicates: 0  Warnings: 0            mysql> create table t4 like employees;   # 只複製表結構,連同主鍵會一塊兒複製 ...    6.刪除表:        DROP TABLE 表名;        drop table t1;        drop table t1,t2;
相關文章
相關標籤/搜索