mysql语句用法,添加、修改、删除字段一,连接MySQL二,MySQL管理与授权三,数据库简单操作四, 数据库备份五,后记一,连接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 changedmysql> 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,alteron phpbb.*from phpbbroot@localhost identified by '52netseek';完全将phpbbroot这个用户删除:>use mysql>delete from userwhere user='phpbbroot' and host='localhost';>flush privileges; 刷新数据库三,数据库简单操作1.显示数据库列表:>show databases;mysqltest2.使其成为当前操作数据库>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.txtselect * from personal into outfile './out.txt'; 可以看在out.txt 在/var/lib/mysql/目录下用out.txt2.使用mysqldump实用程序(可以轻松处理多个表)# cd /var/lib/mysql导出建立相关表的建表命令和插入指令# mysqldump bbs >bbs.sql 将数据库bbs导入到bbs.sql中如果要将bbs.sql导入数据库可以使用:mysql> create database bbstest; 先建立一个名为office 的数据库.# mysql bbstest <bbs.sql (这个常用在将本地的数据库文件传到服务器上,再导入到数据库中)只想导出建表指令:# mysqldump -d bbs >bbscreate.sql只想导出插入数据的sql指令:# mysqldump -t bbs >bbsinsert.sql同时导出数据库中建表指令和表中的数据:# mysqldump -T./ bbs cdb_admingroups (其中./表示当前目录,cdb_admingroups为bbs数据库其中的一个表)#lscdb_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数据库的用户读写权限。