[root@www ~]# mysql -uroot -p -h127.0.0.1(或者指定-hlocalhost) 這裏指定了用戶密碼和主機,html
Enter password: mysql
Welcome to the MySQL monitor. Commands end with ; or \g.sql
Your MySQL connection id is 7數據庫
Server version: 5.1.73 Source distributionexpress
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.服務器
Oracle is a registered trademark of Oracle Corporation and/or itside
affiliates. Other names may be trademarks of their respectiveui
owners.this
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.url
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
| testdb |
+--------------------+
4 rows in set (0.04 sec)
mysql> use testdb 切換到testdb數據庫
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> create table bird (name char(10) not null, age tinyint,xingbie char(1) not null );
Query OK, 0 rows affected (0.02 sec) 建立bird表 定義相關字段和屬
mysql> desc bird; 查看相關bird表的信息
+---------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------+------+-----+---------+-------+
| name | char(10) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
| xingbie | char(1) | NO | | NULL | |
+---------+------------+------+-----+---------+-------+
3 rows in set (0.03 sec)
mysql> alter table bird change xingbie XB char(1);修改表的相關屬性,這裏注意要帶定義的屬性char(1)
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc bird;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| name | char(10) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
| XB | char(1) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
3 rows in set (0.02 sec)
mysql> alter table bird add zhuangye varchar(100); 這裏增長了一個字段
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc bird
-> ;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| name | char(10) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
| XB | char(1) | YES | | NULL | |
| zhuangye | varchar(100) | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> alter table bird change zhuangye ZY varchar(100); 又一次修改表的字段注意帶上字段定義varchar(100)
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc bird
-> ;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name | char(10) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
| XB | char(1) | YES | | NULL | |
| ZY | varchar(100) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> help alter table 查看修改表的幫助信息 也能夠用help create table查看建立表
Name: 'ALTER TABLE'
Description:
Syntax:
ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name
[alter_specification [, alter_specification] ...]
[partition_options]
alter_specification:
table_options
| ADD [COLUMN] col_name column_definition
[FIRST | AFTER col_name ]
| ADD [COLUMN] (col_name column_definition,...)
| ADD {INDEX|KEY} [index_name]
[index_type] (index_col_name,...) [index_option] ...
| ADD [CONSTRAINT [symbol]] PRIMARY KEY
[index_type] (index_col_name,...) [index_option] ...
| ADD [CONSTRAINT [symbol]]
UNIQUE [INDEX|KEY] [index_name]
[index_type] (index_col_name,...) [index_option] ...
| ADD FULLTEXT [INDEX|KEY] [index_name]
(index_col_name,...) [index_option] ...
| ADD SPATIAL [INDEX|KEY] [index_name]
(index_col_name,...) [index_option] ...
| ADD [CONSTRAINT [symbol]]
FOREIGN KEY [index_name] (index_col_name,...)
reference_definition
| ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}
| CHANGE [COLUMN] old_col_name new_col_name column_definition
[FIRST|AFTER col_name]
| MODIFY [COLUMN] col_name column_definition
[FIRST | AFTER col_name]
| DROP [COLUMN] col_name
| DROP PRIMARY KEY
| DROP {INDEX|KEY} index_name
| DROP FOREIGN KEY fk_symbol
| DISABLE KEYS
| ENABLE KEYS
| RENAME [TO|AS] new_tbl_name
| ORDER BY col_name [, col_name] ...
| CONVERT TO CHARACTER SET charset_name [COLLATE collation_name]
| [DEFAULT] CHARACTER SET [=] charset_name [COLLATE [=] collation_name]
| DISCARD TABLESPACE
| IMPORT TABLESPACE
| ADD PARTITION (partition_definition)
| DROP PARTITION partition_names
| COALESCE PARTITION number
| REORGANIZE PARTITION [partition_names INTO (partition_definitions)]
| ANALYZE PARTITION {partition_names | ALL}
| CHECK PARTITION {partition_names | ALL}
| OPTIMIZE PARTITION {partition_names | ALL}
| REBUILD PARTITION {partition_names | ALL}
| REPAIR PARTITION {partition_names | ALL}
| PARTITION BY partitioning_expression
| REMOVE PARTITIONING
ALTER TABLE changes the structure of a table. For example, you can add
or delete columns, create or destroy indexes, change the type of
existing columns, or rename columns or the table itself. You can also
change characteristics such as the storage engine used for the table or
the table comment.
A number of partitioning-related extensions to ALTER TABLE were added
in MySQL 5.1.5. These can be used with partitioned tables for
repartitioning, for adding, dropping, merging, and splitting
partitions, and for performing partitioning maintenance. It is possible
for an ALTER TABLE statement to contain a PARTITION BY or REMOVE
PARTITIONING clause in an addition to other alter specifications, but
the PARTITION BY or REMOVE PARTITIONING clause must be specified last
after any other specifications. The ADD PARTITION, DROP PARTITION,
COALESCE PARTITION, REORGANIZE PARTITION, ANALYZE PARTITION, CHECK
PARTITION, and REPAIR PARTITION options cannot be combined with other
alter specifications in a single ALTER TABLE, since the options just
listed act on individual partitions. For more information, see
http://dev.mysql.com/doc/refman/5.1/en/alter-table-partition-operations.html
.
Following the table name, specify the alterations to be made. If none
are given, ALTER TABLE does nothing.
URL: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
mysql> alter table bird modify ZY char(11); 修改了ZY定義的字段長度
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc bird;
+--------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------+------+-----+---------+-------+
| name | char(10) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
| XB | char(1) | YES | | NULL | |
| ZY | char(11) | YES | | NULL | |
| weight | int(11) | YES | | NULL | |
+--------+------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> alter table bird add xuli char(10) after weight;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc bird;
+--------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------+------+-----+---------+-------+
| name | char(10) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
| XB | char(1) | YES | | NULL | |
| ZY | char(11) | YES | | NULL | |
| weight | int(11) | YES | | NULL | |
| xuli | char(10) | YES | | NULL | |
+--------+------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql> alter table bird change xuli Xueli char(10) after name; change修改字段
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc bird;
+--------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------+------+-----+---------+-------+
| name | char(10) | NO | | NULL | |
| Xueli | char(10) | YES | | NULL | |
| age | tinyint(4) | YES | | NULL | |
| XB | char(1) | YES | | NULL | |
| ZY | char(11) | YES | | NULL | |
| weight | int(11) | YES | | NULL | |
+--------+------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql> alter table bird drop weight; 刪除weight字段
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc bird;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| name | char(10) | NO | | NULL | |
| Xueli | char(10) | YES | | NULL | |
| age | tinyint(4) | YES | | NULL | |
| XB | char(1) | YES | | NULL | |
| ZY | char(11) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> insert into bird (name,xueli) value ('qiaofeng','xiaoxue'),('yangguo','chuzhong');輸入字段定義的具體信息
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from bird;
+----------+----------+------+------+------+
| name | Xueli | age | XB | ZY |
+----------+----------+------+------+------+
| qiaofeng | xiaoxue | NULL | NULL | NULL |
| yangguo | chuzhong | NULL | NULL | NULL |
+----------+----------+------+------+------+
2 rows in set (0.00 sec)
mysql> select name from bird;
+----------+
| name |
+----------+
| qiaofeng |
| yangguo |
+----------+
2 rows in set (0.04 sec)
mysql> select * from bird;
+----------+----------+------+------+------+
| name | Xueli | age | XB | ZY |
+----------+----------+------+------+------+
| qiaofeng | xiaoxue | NULL | NULL | NULL |
| yangguo | chuzhong | NULL | NULL | NULL |
+----------+----------+------+------+------+
2 rows in set (0.02 sec)
mysql> insert into bird value ('hongqigong','xiaox',50,'man','jianghuadaxia'); 沒有指定哪一個字段則依次修改
Query OK, 1 row affected, 2 warnings (0.00 sec)
mysql> select * from bird;
+------------+----------+------+------+-------------+
| name | Xueli | age | XB | ZY |
+------------+----------+------+------+-------------+
| qiaofeng | xiaoxue | NULL | NULL | NULL |
| yangguo | chuzhong | NULL | NULL | NULL |
| hongqigong | xiaox | 50 | m | jianghuadax |
+------------+----------+------+------+-------------+
3 rows in set (0.00 sec)
mysql> update bird set xueli='daxue' where name='yangguo' 修改指定字段對應的信息
-> ;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from bird 查看
-> ;
+------------+---------+------+------+-------------+
| name | Xueli | age | XB | ZY |
+------------+---------+------+------+-------------+
| qiaofeng | xiaoxue | NULL | NULL | NULL |
| yangguo | daxue | NULL | NULL | NULL |
| hongqigong | xiaox | 50 | m | jianghuadax |
+------------+---------+------+------+-------------+
3 rows in set (0.00 sec)
mysql>
alter是改房間,update 是改傢俱
mysql> create user 'tom'@'%' identified by 'tom';建立tom用戶指定在任何主機上 密碼也是tom,%表示任意長度任意字符
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for 'tom'@'%'
-> ;
+----------------------------------------------------------------------------------------------------+
| Grants for tom@% |
+----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'%' IDENTIFIED BY PASSWORD '*71FF744436C7EA1B954F6276121DB5D2BF68FC07' |
+----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> grant all privileges on testdb.* to 'tom'@'%'; 給tom用戶全部權限
Query OK, 0 rows affected (0.00 sec)
這樣遠程鏈接Mysql服務器的話就能夠直接 mysql -utom -p -h192.168.137.222
若是提示鏈接不上則多是iptables沒有關閉,service iptables stop 便可
、················
###### show databases; 查看數據庫
show tables; 查看錶
show table bird 查看一張叫作bird的表
select name,age...from bird 進bird表中查看name age等相關字段,
################################
步驟 creat testdb;建立testdb數據庫
create table testdb (name char(10) not null, age tinyint,xingbie char(1) not null ); 建立testdb表中的相關字段name age xingbie等
alter table testdb change|modify|add|drop 修改
alter table bird change xuli Xueli char(10) after name; change 修改字段
alter table bird change xingbie XB char(1); 修改字段大小寫,這裏注意要帶定義的屬性char(1)即便沒有變化
alter table bird add zhuangye varchar(100); 這裏增長了一個字段
alter table bird drop weight; 刪除weight字段
alter table bird modify ZY char(11); 修改了ZY定義的字段長度
insert into bird (name,xueli) value ('qiaofeng','xiaoxue'),('yangguo','chuzhong');爲字段name xueli定義的具體信息
insert into bird value ('hongqigong','xiaox',50,'man','jianghuadaxia'); 沒有指定哪一個字段則依次修改
update bird set xueli='daxue' where name='yangguo' 修改指定字段對應的信息
select * from bird 查看
#############################################3
mysql> select User,Host,Password from user;
+-------+-----------------+-------------------------------------------+
| User | Host | Password |
+-------+-----------------+-------------------------------------------+
| root | localhost | *00A51F3F48415C7D4E8908980D443C29C69B60C9 |
| root | www.bird.com | *00A51F3F48415C7D4E8908980D443C29C69B60C9 |
| root | 127.0.0.1 | *00A51F3F48415C7D4E8908980D443C29C69B60C9 |
| | localhost | |
| | www.bird.com | |
| tom | % | *71FF744436C7EA1B954F6276121DB5D2BF68FC07 |
| bird | 192.168.137.223 | *2157821D5B01640A8367829F19E09D525615B0A6 |
| bird | 192.168.137.128 | |
| hello | 192.168.137.128 | *6B4F89A54E2D27ECD7E8DA05B4AB8FD9D1D8B119
設定密碼: 一、mysql> set password for 'root'@'host'=password('123456'); #host能夠是具體的ip
二、# mysqladmin -uroot -p -hhost password ‘123456’
三、mysql> update user set password=password(‘12345’)where user=‘root' and host='127.0.0.1'; 這是修改user表並制定root用戶且host爲127.0.0.1的
create user 'tom'@'%' identified by 'tom';建立tom用戶指定在任何主機上 密碼也是tom,%表示任意長度任意字符 grant all privileges on testdb.* to 'tom'@'%'; 給tom用戶全部權限