Warning: Undefined array key "cperpage" in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 230

Warning: Undefined variable $output in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 300

Warning: Undefined variable $fixed_tags in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 301

Warning: Undefined variable $isshowdots in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 302

Warning: Undefined variable $tag_aditional in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 305

Warning: Undefined variable $tag_aditional in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 308

Warning: Undefined variable $tag_aditional in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 311

Warning: Undefined variable $post in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 320

Warning: Attempt to read property "ID" on null in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 320

Warning: Undefined variable $post in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 320

Warning: Attempt to read property "ID" on null in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 320

Warning: Undefined variable $more_text_link in /www/wwwroot/www.now163.com/wp-content/themes/typology/functions.php on line 320
mysql alter 语句用法,添加、修改、删除字段【转】 – 理想社会

mysql alter 语句用法,添加、修改、删除字段【转】

m

一,连接MySQL

格式:mysql -h 远程主机地址 -u 用户名 -p 回车

输入密码进入:

mysql -u root -p 回车

Enter password: ,输入密码就可以进入

mysql> 进入了

退出命令:>exit 或者ctrl+D

二,MySQL管理与授权

1.修改密码:

格式:mysqladmin -u 用户名 -p 旧密码 password 新密码

2.增加新用户:

>grant create,select,update….(授予相关的操作权限)

->on 数据库.*

-> to 用户名@登录主机 identified by ‘密码’

操作实例:

给root用户添加密码:

# mysqladmin -u root password 52netseek

因为开始root没有密码,所以-p旧密码一项可以省略.

登陆测试:

# mysql -u root -p 回车

输入密码,成功登陆.

将原有的mysql管理登陆密码52netseek改为52china.

# mysqladmin -u root -p 52netseek password ’52china’

创建数据库添加用户并授予相应的权限:

mysql> create database phpbb;

Query OK, 1 row affected (0.02 sec)

mysql> use phpbb;

Database changed

mysql> grant create,select,update,insert,delete,alter

-> on phpbb.*

-> to phpbbroot@localhost identified by ’52netseek’;

Query OK, 0 rows affected (0.00 sec)

授予所有的权限:

>grant all privileges

>on bbs.*

>to bbsroot@localhost identified by ’52netseek’

回收权限:

revoke create,select,update,insert,delete,alter

on phpbb.*

from phpbbroot@localhost identified by ’52netseek’;

完全将phpbbroot这个用户删除:

>use mysql

>delete from user

where user=’phpbbroot’ and host=’localhost’;

>flush privileges; 刷新数据库

三,数据库简单操作

1.显示数据库列表:

>show databases;

mysql

test

2.使其成为当前操作数据库

>use mysql; 打开数据库.

>show tables; 显示mysql数据库中的数据表.

3.显示数据表的表结构:

>describe 表名;

>describe user; 显示user表的表结构:

4.创建数据库,建表

>create database 数据库名;

>use 数据库名;

>create table 表名(字段设定列表)

5.删除数据库,册除表

>drop database 数据库名;

>drop table 表名;

6.显示表中的记录;

select * from 表名;

7.修改数据库结构:

增加字段:

alter table dbname add column <字段名><字段选项>

修改字段:

alter table dbname change <旧字段名> <新字段名><选项>

删除字段:

alter table dbname drop column <字段名>

实例操作:

>create database office;

>use office;

mysql> create table personal(

-> member_no char(5) not null,

-> name char(,

-> birthday date,

-> exam_score tinyint,

-> primary key(member_no)

-> );

Query OK, 0 rows affected (0.01 sec)

>desc personal; 显示表结构:

+————+————+——+—–+———+——-+

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

+————+————+——+—–+———+——-+

| member_no | char(5) | | PRI | | |

| name | char( | YES | | NULL | |

| birthday | date | YES | | NULL | |

| exam_score | tinyint(4) | YES | | NULL | |

+————+————+——+—–+———+——-+

4 rows in set (0.00 sec)

insert into personal values (‘001′,’netseek’,’1983-03-15′,’95’);

insert into personal values (‘002′,’heihei’,’1982-02-24′,’90’);

insert into personal values (‘003′,’gogo’,’1985-05-21′,’85’);

insert into personal values (‘004′,’haha’,’1984-02-25′,’84’);

insert into personal values (‘005′,’linlin’,’1982-04-28′,’85’);

您正在看的MySQL教程是:MySQL数据库学习笔记。 insert into personal values (‘006′,’xinxin’,’1985-03-15′,’75’);

mysql> select * from personal;

+———–+———+————+————+

| member_no | name | birthday | exam_score |

+———–+———+————+————+

| 001 | netseek | 1983-03-15 | 95 |

| 002 | heihei | 1982-02-24 | 90 |

| 003 | gogo | 1985-05-21 | 85 |

| 004 | haha | 1984-02-25 | 84 |

| 005 | linlin | 1982-04-28 | 85 |

| 006 | xinxin | 1985-03-15 | 75 |

+———–+———+————+————+

修改数据库表:

要求: 在birthday这后增加一个为height的字段,数据类型为tinyint.

将字段exam_score 改名为scores,数据类型不变

>alter table personal

->add column height tinyint after birthday,

->change column exam_score scores tinyint;

mysql> select * from personal;

+———–+———+————+——–+——–+

| member_no | name | birthday | height | scores |

+———–+———+————+——–+——–+

| 001 | netseek | 1983-03-15 | NULL | 95 |

| 002 | heihei | 1982-02-24 | NULL | 90 |

| 003 | gogo | 1985-05-21 | NULL | 85 |

| 004 | haha | 1984-02-25 | NULL | 84 |

| 005 | linlin | 1982-04-28 | NULL | 85 |

| 006 | xinxin | 1985-03-15 | NULL | 75 |

+———–+———+————+——–+——–+

给表中插入数据:

>update personal set scores=95+5 where name=’netseek’;

>select scores from personal where name=’netseek’;

+——–+

| scores |

+——–+

| 100 |

+——–+

删除表名字为’gogo’所有的信息中的的:

> delete from personal where name=’gogo’;

册除数据库中的表:

mysql>drop table if exists personal;

三,数据库的导入与导出

导出:

使用select into outfile ‘filename’语句

使用mysqldump实用程序

使用select into outfile ‘filename’语句

1.只能处理单个表,输出文件只有数据,没有表结构

我们要将office,其中有一个表为personal,现在要把personal卸成文本文件out.txt:

>use office;

>select * from personal into outfile ‘out.txt’; 可以看在/var/lib/mysql/office/目录下有out.txt

select * from personal into outfile ‘./out.txt’; 可以看在out.txt 在/var/lib/mysql/目录下用out.txt

2.使用mysqldump实用程序(可以轻松处理多个表)

# cd /var/lib/mysql

导出建立相关表的建表命令和插入指令

# mysqldump bbs >bbs.sql 将数据库bbs导入到bbs.sql中

如果要将bbs.sql导入数据库可以使用:

mysql> create database bbstest; 先建立一个名为office 的数据库.

# mysql bbstest <>

只想导出建表指令:

# mysqldump -d bbs >bbscreate.sql

只想导出插入数据的sql指令:

# mysqldump -t bbs >bbsinsert.sql

同时导出数据库中建表指令和表中的数据:

# mysqldump -T./ bbs cdb_admingroups (其中./表示当前目录,cdb_admingroups为bbs数据库其中的一个表)

#ls

cdb_admingroups.sql 导出了建表指令

cdb_admingroups.txt 导出了表中的数据

导入:

从文件中加载数据库:

mysql>load data infile “/tmp/name.txt” into table names;

mysql>select * from names;

四,数据库备份

1.手动拷贝备份:

MySQL数据库的文件保存在目录/var/lib/mysql中,数据库为每个库建立一个目录,所有的数据库文件都在这些目录中.

[root@linuxhero mysql]#ls

[root@linuxhero mysql]#servcie mysqld stop 先停止数据库

bbs mysql mysql.sock phpbb test office 显示其中的数据库.

如果我们要将现在的数据库目录备份为mysql.bak .

[root@linuxhero lib]# cp -rf mysql mysql.bak

如果数据库遭到了破坏,现在要将数据库恢复:

[root@linuxhero lib]# cp -rf mysql.bak/* mysql

恢复数据库以后,var/lib/mysql中的文件已改变了,要更改文件的所属权限必须改变MySQL数据库的用户读写权限。

所以我们得启动和运行mysql,并登陆数据库:

[root@linuxhero lib]# /etc/init.d/mysqld start

[root@linuxhero lib]# mysql

您正在看的MySQL教程是:MySQL数据库学习笔记。-u root -p

Enter password:输入密码成功登陆.

mysql> show databses;

2.利用mysqldump来备份数据库

[root@linuxhero mysql]# mysqldump –opt bbs -u root -p > bbs.sql

Enter password:

注:–opt添加备份的其它选项,bb为其中一个数据库名,

上面的意思是:使用重定向输出将备份写入到文件bb.sql中.

[root@linuxhero mysql] #less bbs.sql

如果要恢复bb这个数据库,则进行如下操作:

[root@linuxhero mysql] #mysql bbs -u root -p < bbs.sql

如果要备份所有数据库:

[root@linuxhero mysql] #mysqldump –opt –all-databases -u root -p >mysql.bak

Enetr password:输入密码即可

恢复所有数据库,不用输入数据库的名字:

[root@linuxhero mysql] #mysql -u root -p < mysql.bak

Enetr password: 输入密码即可

五,后记:

MySQL数据库个人学习笔记,这是我个人学习过程中的一个简单的总结,这些都是常用的,希望对于希望学习mysql的朋友有所帮助,如果有什么不对或者不妥的地方请多多指教,欢迎大家与我交流学习Linux相关的知识。

About the author

Add comment

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

By now163

Your sidebar area is currently empty. Hurry up and add some widgets.