MySQL 常用脚本

常用功能脚本


1.导出整个数据库

1mysqldump -u 用户名 -p –default-character-set=latin1 数据库名 > 导出的文件名(数据库默认编码是latin1)
2
3mysqldump -u wcnc -p smgp_apps_wcnc > wcnc.sql

2.导出一个表

<span>1</span><span>mysqldump</span> <span>-u</span> 用户名 <span>-p</span> 数据库名 表名> 导出的文件名 <br/><span>2</span><br/><span>3</span><span>mysqldump</span> <span>-u</span> <span>wcnc</span> <span>-p</span> <span>smgp_apps_wcnc</span> <span>users</span>> <span>wcnc_users</span><span>.sql</span> <br/>

3.导出一个数据库结构

<span>1</span>mysqldump -u wcnc -p -d –add-<span>drop</span>-<span>table</span> smgp_apps_wcnc >d:wcnc_db.sql <br/><span>2</span><br/><span>3</span>-d 没有数据 –<span>add</span>-<span>drop</span>-<span>table</span> 在每个<span>create</span>语句之前增加一个<span>drop</span> <span>table</span> <br/>

4.导入数据库

<span> 1</span><span>A</span>:常用<span>source</span> 命令 <br/><span> 2</span><br/><span> 3</span>进入<span>mysql</span>数据库控制台, <br/><span> 4</span><br/><span> 5</span>如<span>mysql</span> <span>-u</span> <span>root</span> <span>-p</span> <br/><span> 6</span><br/><span> 7</span><span>mysql</span>><span>use</span> 数据库 <br/><span> 8</span><br/><span> 9</span>然后使用<span>source</span>命令,后面参数为脚本文件(如这里用到的<span>.sql</span>) <br/><span>10</span><br/><span>11</span><span>mysql</span>><span>source</span> <span>wcnc_db</span><span>.sql</span> <br/><span>12</span><br/><span>13</span><span>B</span>:使用<span>mysqldump</span>命令 <br/><span>14</span><br/><span>15</span><span>mysqldump</span> <span>-u</span> <span>username</span> <span>-p</span> <span>dbname</span> < <span>filename</span><span>.sql</span> <br/><span>16</span><br/><span>17</span><span>C</span>:使用<span>mysql</span>命令 <br/><span>18</span><br/><span>19</span><span>mysql</span> <span>-u</span> <span>username</span> <span>-p</span> <span>-D</span> <span>dbname</span> < <span>filename</span><span>.sql</span> <br/>

**启动与退出 **

1、进入MySQL:启动MySQL Command Line Client(MySQL的DOS界面),直接输入安装时的密码即可。此时的提示符是:mysql>

2、退出MySQL:quit或exit

**库操作 **

1、、创建数据库

命令:create database <数据库名>

例如:建立一个名为sqlroad的数据库

mysql> create database sqlroad;

2、显示所有的数据库

命令:show databases (注意:最后有个s)

mysql> show databases;

3、删除数据库

命令:drop database <数据库名>

例如:删除名为 sqlroad的数据库

mysql> drop database sqlroad;

4、连接数据库

命令:use <数据库名>

例如:如果sqlroad数据库存在,尝试存取它:

mysql> use sqlroad;

屏幕提示:Database changed

5、查看当前使用的数据库

mysql> select database();

6、当前数据库包含的表信息:

mysql> show tables; (注意:最后有个s)

**表操作,操作之前应连接某个数据库 **

1、建表

<span> 1</span>命令:create table <表名> ( <字段名> <类型> [,..<字段名n> <类型n>]); <br/><span> 2</span><br/><span> 3</span>mysql> create table MyClass( <br/><span> 4</span><br/><span> 5</span>> id <span>int</span>(<span>4</span>) <span>not</span> <span>null</span> primary key auto_increment, <br/><span> 6</span><br/><span> 7</span>> name char(<span>20</span>) <span>not</span> <span>null</span>, <br/><span> 8</span><br/><span> 9</span>> sex <span>int</span>(<span>4</span>) <span>not</span> <span>null</span> <span>default</span> ’′, <br/><span>10</span><br/><span>11</span>> degree double(<span>16</span>,<span>2</span>)); <br/>

2、获取表结构

<span>1</span>命令:desc 表名,或者show columns <span>from</span> 表名 <br/><span>2</span><br/><span>3</span>mysql>DESCRIBE <span>MyClass</span> <br/><span>4</span><br/><span>5</span>mysql> desc <span>MyClass</span>; <br/><span>6</span><br/><span>7</span>mysql> show columns <span>from</span> <span>MyClass</span>; <br/>

3、删除表

<span>1</span>命令:<span>drop</span> <span>table</span> <表名> <br/><span>2</span><br/><span>3</span>例如:删除表名为 MyClass 的表 <br/><span>4</span><br/><span>5</span>mysql> <span>drop</span> <span>table</span> MyClass; <br/>

4、插入数据

<span>1</span>命令:<span>insert</span> <span>into</span> <表名> <span>[( <字段名>[,..<字段名n> ]</span>)] <span>values</span> ( 值 )<span>[, ( 值n )]</span> <br/><span>2</span><br/><span>3</span>例如,往表 <span>MyClass</span>中插入二条记录, 这二条记录表示:编号为的名为<span>Tom</span>的成绩为<span>.45</span>, 编号为 的名为<span>Joan</span> 的成绩为<span>.99</span>,编号为 的名为<span>Wang</span> 的成绩为<span>.5</span>. <br/><span>4</span><br/><span>5</span><span>mysql</span>> <span>insert</span> <span>into</span> <span>MyClass</span> <span>values</span>(1,’<span>Tom</span>’,96<span>.45</span>),(2,’<span>Joan</span>’,82<span>.99</span>), (2,’<span>Wang</span>’, 96<span>.59</span>); <br/>

5、查询表中的数据

<span> 1</span><span>1</span>)、查询所有行 <br/><span> 2</span><br/><span> 3</span>命令:<span>select</span> <字段,字段,...> <span>from</span> < 表名 > <span>where</span> < 表达式 > <br/><span> 4</span><br/><span> 5</span>例如:查看表 <span>MyClass</span> 中所有数据 <br/><span> 6</span><br/><span> 7</span>mysql> <span>select</span> * <span>from</span> <span>MyClass</span>; <br/><span> 8</span><br/><span> 9</span><span>2</span>)、查询前几行数据 <br/><span>10</span><br/><span>11</span>例如:查看表 <span>MyClass</span> 中前行数据 <br/><span>12</span><br/><span>13</span>mysql> <span>select</span> * <span>from</span> <span>MyClass</span> <span>order</span> <span>by</span> id limit <span>0</span>,<span>2</span>; <br/><span>14</span><br/><span>15</span>或者: <br/><span>16</span><br/><span>17</span>mysql> <span>select</span> * <span>from</span> <span>MyClass</span> limit <span>0</span>,<span>2</span>; <br/>

6、删除表中数据

<span>1</span>命令:<span>delete</span> <span>from</span> 表名 <span>where</span> 表达式 <br/><span>2</span><br/><span>3</span>例如:删除表 MyClass中编号为 的记录 <br/><span>4</span><br/><span>5</span>mysql> <span>delete</span> <span>from</span> MyClass <span>where</span> <span>id</span>=<span>1</span>; <br/>

7、修改表中数据:update 表名 set 字段=新值,…where 条件

<span>1</span><span>mysql></span><span> update MyClass <span>set</span> name=’Mary’<span>where</span> id=1; </span><br/>

8、在表中增加字段:

<span>1</span>命令:alter table 表名 <span>add</span>字段 类型 其他; <br/><span>2</span><br/><span>3</span>例如:在表MyClass中添加了一个字段passtest,类型为<span>int</span>(<span>4</span>),默认值为 <br/><span>4</span><br/><span>5</span>mysql> <span>alter table MyClass <span>add</span> passtest <span>int</span>(<span><span>4</span></span>) <span>default</span> ’′ <br/><span>6</span></span><br/>

9、更改表名:

<span> 1</span>命令:rename table 原表名 <span>to</span> 新表名; <br/><span> 2</span><br/><span> 3</span>例如:在表<span>MyClass</span>名字更改为YouClass <br/><span> 4</span><br/><span> 5</span>mysql> rename table <span>MyClass</span> <span>to</span> YouClass; <br/><span> 6</span><br/><span> 7</span>更新字段内容 <br/><span> 8</span><br/><span> 9</span>update 表名 <span>set</span> 字段名 = 新内容 <br/><span>10</span><br/><span>11</span>update 表名 <span>set</span> 字段名 = replace(字段名,’旧内容’, 新内容’) <br/><span>12</span><br/><span>13</span>update article <span>set</span> content=concat(‘  ’,content); <br/>

字段类型和数据库操作

1.INT[(M)] 型:正常大小整数类型

2.DOUBLE[(M,D)] [ZEROFILL] 型:正常大小(双精密)浮点数字类型

3.DATE 日期类型:支持的范围是-01-01到-12-31。MySQL以YYYY-MM-DD格式来显示DATE值,但是允许你使用字符串或数字把值赋给DATE列

4.CHAR(M) 型:定长字符串类型,当存储时,总是是用空格填满右边到指定的长度

5.BLOB TEXT类型,最大长度为(2^16-1)个字符。

6.VARCHAR型:变长字符串类型

7.导入数据库表

<span>1</span>创建<span>.sql</span>文件 <br/><span>2</span><br/><span>3</span>先产生一个库如<span>auction</span><span>.c</span><span>:mysqlbin</span>><span>mysqladmin</span> <span>-u</span> <span>root</span> <span>-p</span> <span>creat</span> <span>auction</span>,会提示输入密码,然后成功创建。 <br/><span>4</span><br/><span>5</span>导入<span>auction</span><span>.sql</span>文件 <br/><span>6</span><br/><span>7</span><span>c</span><span>:mysqlbin</span>><span>mysql</span> <span>-u</span> <span>root</span> <span>-p</span> <span>auction</span> < <span>auction</span><span>.sql</span>。 <br/><span>8</span><br/><span>9</span>通过以上操作,就可以创建了一个数据库<span>auction</span>以及其中的一个表<span>auction</span>。 <br/>

8.修改数据库

<span>1</span>在mysql的表中增加字段: <br/><span>2</span><br/><span>3</span><span>alter</span> <span>table</span> dbname <span>add</span> <span>column</span> userid <span>int</span>(<span>11</span>) <span>not</span> <span>null</span> primary <span>key</span> auto_increment; <br/><span>4</span><br/><span>5</span>这样,就在表dbname中添加了一个字段userid,类型为int(11)。 <br/>

9.mysql数据库的授权

<span> 1</span><span>mysql></span><span>grant select,insert,delete,create,drop </span><br/><span> 2</span><br/><span> 3</span>on *.* (或test.*/user.*/..) <br/><span> 4</span><br/><span> 5</span>to 用户名@localhost <br/><span> 6</span><br/><span> 7</span>identified by ‘密码’; <br/><span> 8</span><br/><span> 9</span>如:新建一个用户帐号以便可以访问数据库,需要进行如下操作: <br/><span>10</span><span><br/><span>11</span>mysql></span><span> grant usage </span><br/><span>12</span><span><br/><span>13</span>  -></span><span> ON <span>test</span>.* </span><br/><span>14</span><span><br/><span>15</span>  -></span><span> TO testuser@localhost; </span><br/><span>16</span><br/><span>17</span>  Query OK, 0 rows affected (0.15 sec) <br/><span>18</span><br/><span>19</span>  此后就创建了一个新用户叫:testuser,这个用户只能从localhost连接到数据库并可以连接到test 数据库。下一步,我们必须指定testuser这个用户可以执行哪些操作: <br/><span>20</span><span><br/><span>21</span>  mysql></span><span> GRANT select, insert, delete,update </span><br/><span>22</span><span><br/><span>23</span>  -></span><span> ON <span>test</span>.* </span><br/><span>24</span><span><br/><span>25</span>  -></span><span> TO testuser@localhost; </span><br/><span>26</span><br/><span>27</span>  Query OK, 0 rows affected (0.00 sec) <br/><span>28</span><br/><span>29</span>  此操作使testuser能够在每一个test数据库中的表执行SELECT,INSERT和DELETE以及UPDATE查询操作。现在我们结束操作并退出MySQL客户程序: <br/><span>30</span><span><br/><span>31</span>  mysql></span><span> <span>exit</span> </span><br/>

DDL操作

1:使用SHOW语句找出在服务器上当前存在什么数据库:

mysql> SHOW DATABASES;

2、创建一个数据库MYSQLDATA

mysql> Create DATABASE MYSQLDATA;

3:选择你所创建的数据库

mysql> USE MYSQLDATA; (按回车键出现Database changed 时说明操作成功!)

4:查看现在的数据库中存在什么表

mysql> SHOW TABLES;

5:创建一个数据库表

mysql> Create TABLE MYTABLE (name VARCHAR(20), sex CHAR(1));

6:显示表的结构:

mysql> DESCRIBE MYTABLE;

7:往表中加入记录

mysql> insert into MYTABLE values (“hyq”,”M”);

8:用文本方式将数据装入数据库表中(例如D:/mysql.txt)

mysql> LOAD DATA LOCAL INFILE “D:/mysql.txt”INTO TABLE MYTABLE;

9:导入.sql文件命令(例如D:/mysql.sql)

mysql>use database;

mysql>source d:/mysql.sql;

10:删除表

mysql>drop TABLE MYTABLE;

11:清空表

mysql>delete from MYTABLE;

12:更新表中数据

mysql>update MYTABLE set sex=”f”where name=’hyq’;


标题:MySQL 常用脚本
作者:Curry
地址:http://bolo.it-cxy.top/articles/2024/09/01/1725184561756.html

    评论
    0 评论
avatar

取消