mysql中alter數(shù)據(jù)表中增加、刪除字段與表名修改例子_MySQL教程
推薦:Mysql大小寫敏感的問題一、1 CREATE TABLE NAME(name VARCHAR(10)); 對這個表,缺省情況下,下面兩個查詢的結果是一樣的: 代碼如下: SELECT * FROM TABLE NAME WHERE name='clip'; SELECT * FROM TABLE NAME WHERE name='Clip'; MySql默認查詢是不區(qū)分大小寫的,如果需要區(qū)分他,必須在建表的
alter是非常強大的一個功能我們可以利用alter來修改數(shù)據(jù)表表名字體名及一些其它的操作了,下面一起來看看mysql中alter數(shù)據(jù)表中增加、刪除字段與表名修改的一個例子.
修改刪除mysql數(shù)據(jù)庫中的數(shù)據(jù)內容:
[root@hk ~]# /usr/local/mysql/bin/mysql -uroot -p'admin' #進入mysql
mysql> create database gbk default character set gbk collate gbk_chinese_ci; #建立一個名字叫做gbk的數(shù)據(jù)庫
mysql> use gbk
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| gbk |
+--------------------+
mysql> show tables;
Empty set (0.00 sec)
mysql> create table test( #建立一個叫做test的數(shù)據(jù)表
-> id int(4) not null primary key auto_increment,
-> name char(20) not null
-> );
Query OK, 0 rows affected (0.13 sec)
mysql> show tables;
+---------------+
| Tables_in_gbk |
+---------------+
| test |
+---------------+
1 row in set (0.00 sec)
mysql> insert into test(id,name) values(1,'zy'); #插入部分內容
mysql> insert into test(id,name) values(2,'binghe');
mysql> insert into test(id,name) values(3,'zilong');
mysql> insert into test(id,name) values(4,'feng');
mysql> select * from test; #檢索整個test表
+----+--------+
| id | name |
+----+--------+
| 1 | zy |
| 2 | binghe |
| 3 | zilong |
| 4 | feng |
+----+--------+
4 rows in set (0.00 sec)
[root@hk ~]# /usr/local/mysql/bin/mysqldump -uroot -p'admin' -B gbk >/tmp/gbk.sql #備份gbk數(shù)據(jù)庫
mysql> update test set name = 'zy' ; #未定義
mysql> select * from test; #
+----+------+
| id | name |
+----+------+
| 1 | zy |
| 2 | zy |
| 3 | zy |
| 4 | zy |
+----+------+
[root@hk ~]# /usr/local/mysql/bin/mysql -uroot -p'admin' mysql> use gbk
mysql> select * from test;
+----+--------+
| id | name |
+----+--------+
| 1 | zy |
| 2 | binghe |
| 3 | zilong |
| 4 | feng |
+----+--------+
mysql> update test set name = 'yadianna' where id =1;
mysql> select * from test;
+----+----------+
| id | name |
+----+----------+
| 1 | yadianna |
| 2 | binghe |
| 3 | zilong |
| 4 | feng |
+----+----------+
mysql> update test set id = 999 where name ='yadianna';
mysql> select * from test;
+-----+----------+
| id | name |
+-----+----------+
| 2 | binghe |
| 3 | zilong |
| 4 | feng |
| 999 | yadianna |
+-----+----------+
mysql> delete from test where id =999;
mysql> select * from test;
+----+--------+
| id | name |
+----+--------+
| 2 | binghe |
| 3 | zilong |
| 4 | feng |
+----+--------+
mysql> delete from test where id <4; #以條件刪除
mysql> truncate table test; #刪除all
mysql> select * from test;
Empty set (0.00 sec)
接上上面,修改數(shù)據(jù)庫中表名,表中增加、刪除字段。
mysql> use gbk #進入gbk數(shù)據(jù)庫
mysql> desc test;
+-------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+----------------+
| id | int(4) | NO | PRI | NULL | auto_increment |
| name | char(20) | NO | | NULL | |
+-------+----------+------+-----+---------+----------------+
mysql> alter table test add gender char(4); #增加gender
mysql> desc test;
+--------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+----------------+
| id | int(4) | NO | PRI | NULL | auto_increment |
| name | char(20) | NO | | NULL | |
| gender | char(4) | YES | | NULL | |
+--------+----------+------+-----+---------+----------------+
mysql> alter table test add age int(4) after name;
mysql> desc test;
+--------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+----------------+
| id | int(4) | NO | PRI | NULL | auto_increment |
| name | char(20) | NO | | NULL | |
| age | int(4) | YES | | NULL | |
| gender | char(4) | YES | | NULL | |
+--------+----------+------+-----+---------+----------------+
mysql> show tables;
+---------------+
| Tables_in_gbk |
+---------------+
| test |
+---------------+
mysql> rename table test to hello;
mysql> show tables;
+---------------+
| Tables_in_gbk |
+---------------+
| hello |
+---------------+
mysql> alter table hello rename to world;
mysql> show tables;
+---------------+
| Tables_in_gbk |
+---------------+
| world |
+---------------+
mysql> alter table world drop age;
mysql> desc world;
+--------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+----------------+
| id | int(4) | NO | PRI | NULL | auto_increment |
| name | char(20) | NO | | NULL | |
| gender | char(4) | YES | | NULL | |
+--------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
分享:給Mysql添加遠程訪問權限的方法user:myuser pwd:mypassword 從任何主機連接到mysql服務器 GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION; user:myuser pwd:mypassword 從192.168.1.3連接到mysql服務器 GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.1
- Mysql大小寫敏感的問題
- 給Mysql添加遠程訪問權限的方法
- mysql生成隨機字符串函數(shù)分享
- mysql誤刪root用戶恢復方法
- MySQL編程中的6個實用技巧
- Centos中徹底刪除Mysql(rpm、yum安裝的情況)
- Mysql修改datadir導致無法啟動問題解決方法
- mysql 查詢重復的數(shù)據(jù)的SQL優(yōu)化方案
- mysql的左右內連接用法實例
- mysql中You can’t specify target table for update in FROM clau
- MySQL查詢和修改auto_increment的方法
- MySQL中的if和case語句使用總結
- 相關鏈接:
- 教程說明:
MySQL教程-mysql中alter數(shù)據(jù)表中增加、刪除字段與表名修改例子
。