MySQL初识

LinStclair 8年前
   <p>记不住过去,看不透未来——失败。</p>    <p>MySQL基础知识点罗列,帮助理清思路,主要包含安装、基本操作、MySQL约束、C/C++库使用等知识点。</p>    <h2>Ubuntu下安装MySQL</h2>    <p>按照顺序执行下面三条指令:</p>    <pre>  <code class="language-sql">$ sudo apt-get install mysql-server  $ sudo apt-get install mysql-client  $ sudo apt-get install libmysqlclient-dev  </code></pre>    <p>安装完成,检查MySQL是否成功安装:</p>    <pre>  <code class="language-sql">$ sudo netstat -tap | grep mysql  </code></pre>    <p>记住安装过程中需要输入的密码,通过下列命令可以登录mysql数据库:</p>    <pre>  <code class="language-sql">$ mysql -u root -p # -u for user, -p for password  </code></pre>    <p>安装后,MySQL是默认启动的,如果需要自己控制,则可以执行下列命令:</p>    <pre>  <code class="language-sql">$ sudo /etc/init.d/mysql start  $ sudo /etc/init.d/mysql stop  $ sudo /etc/init.d/mysql restart  $ sudo service mysql start  $ sudo service mysql stop  $ sudo service mysql restart  </code></pre>    <h2>MySQL基本操作</h2>    <h3>登录操作</h3>    <pre>  <code class="language-sql">$ mysql -u root -p -P 3306 -h 127.0.0.1  # -u --user  # -p --passwd  # -P --port, 3306 is the default port for mysql  # -h --host  </code></pre>    <h3>设置提示符</h3>    <pre>  <code class="language-sql">mysql> prompt \u@\h \d>   PROMPT set to '\u@\h \d> '  root@localhost (none) > use test  root@localhost test >   # \u 设置显示用户名  # \h 设置显示主机  # \d 设置显示当前数据库  # \D 设置显示完整日期  </code></pre>    <h3>查询警告</h3>    <pre>  <code class="language-sql">mysql > show warnings;  </code></pre>    <h3>查询命令帮助</h3>    <p>使用 help 命令能够找到MySQL语句的具体使用帮助指南,我们以 <em>create database</em> 举例:</p>    <pre>  <code class="language-sql">mysql> help create;  mysql> help create database;  mysql> help alter;  mysql> help alter database;  </code></pre>    <h2>MySQL数据库操作</h2>    <h3>数据库创建</h3>    <p>使用 create database 创建数据库t1,并显示详细信息:</p>    <pre>  <code class="language-sql">root@localhost test > create database t1;  root@localhost test > show databases;  root@localhost test > show create database t1;  root@localhost test > create database if not exists t2 character set gbk;  </code></pre>    <h3>数据库修改</h3>    <pre>  <code class="language-sql">root@localhost test > alter database t2 character set = utf8;  </code></pre>    <h3>数据库删除</h3>    <pre>  <code class="language-sql">root@localhost test > drop database t1;  </code></pre>    <h3>使用数据库</h3>    <pre>  <code class="language-sql">root@localhost test > use t1;  root@localhost t1 > select database(); # 显示当前数据库  </code></pre>    <h3>创建数据库表</h3>    <pre>  <code class="language-sql">root@localhost test > create table tb1(      -> username varchar(20),      -> age tinyint unsigned,      -> salary float(8,2) unsigned      -> );  </code></pre>    <h3>查看数据库表</h3>    <pre>  <code class="language-sql">root@localhost test > show tables;           # 查看数据库表列表  root@localhost test > show tables from test; # 查看数据库test表列表  root@localhost test > show columns from tb1; # 查看数据库表结构  </code></pre>    <h3>插入记录到数据库表</h3>    <pre>  <code class="language-sql">root@localhost test > insert tb1 values('Tom', 25, 7863.25);  root@localhost test > insert tb1 values('Tom', 25); # error  root@localhost test > insert tb1(username,salary) values('John', 4500.69); # ok  </code></pre>    <h3>记录查找</h3>    <pre>  <code class="language-sql">root@localhost test > select * from tb1;  </code></pre>    <h2>MySQL数据类型</h2>    <h3>整形</h3>    <table>     <thead>      <tr>       <th>数据类型</th>       <th>存储需求</th>      </tr>     </thead>     <tbody>      <tr>       <td>TINYINT</td>       <td>8位</td>      </tr>      <tr>       <td>SMALLINT</td>       <td>16位</td>      </tr>      <tr>       <td>MEDIUMINT</td>       <td>24位</td>      </tr>      <tr>       <td>INT</td>       <td>32位</td>      </tr>      <tr>       <td>BIGINT</td>       <td>64位</td>      </tr>     </tbody>    </table>    <h3>浮点型</h3>    <table>     <thead>      <tr>       <th>数据类型</th>       <th>存储需求</th>      </tr>     </thead>     <tbody>      <tr>       <td>FLOAT(M,D)</td>       <td>M是数字总位数,D是小数点后面的位数</td>      </tr>      <tr>       <td>DOUBLE(M,D)</td>      </tr>     </tbody>    </table>    <h3>日期时间型</h3>    <table>     <thead>      <tr>       <th>列类型</th>       <th>存储需求</th>       <th>范围</th>      </tr>     </thead>     <tbody>      <tr>       <td>YEAR</td>       <td>8位</td>       <td>1901~2155</td>      </tr>      <tr>       <td>TIME</td>       <td>24位</td>       <td>-838:59:59~838:59:59</td>      </tr>      <tr>       <td>DATE</td>       <td>24位</td>       <td>1000.01.01~9999.12.31</td>      </tr>      <tr>       <td>DATETIME</td>       <td>64位</td>       <td>1000.01.01 00:00:00~9999.12.31 23:59:59</td>      </tr>      <tr>       <td>TIMESTAMP</td>       <td>32位</td>       <td>1970.01.01 00:00:01~2037 年某时</td>      </tr>     </tbody>    </table>    <h3>字符类型</h3>    <table>     <thead>      <tr>       <th>列类型</th>       <th>存储需求</th>      </tr>     </thead>     <tbody>      <tr>       <td>CHAR(M)</td>       <td>固定长度,M个字节,0<=<M<=255</td>      </tr>      <tr>       <td>VARCHAR(M)</td>       <td>可变长度,L+1个字节,L<=M且0<=M<=2^16 -1</td>      </tr>      <tr>       <td>TINYTEXT</td>       <td>L+1个字节,L<2^8</td>      </tr>      <tr>       <td>TEXT</td>       <td>L+1个字节,L<2^16</td>      </tr>      <tr>       <td>MEDIUMTEXT</td>       <td>L+1个字节,L<2^24</td>      </tr>      <tr>       <td>LONGTEXT</td>       <td>L+1个字节,L<2^32</td>      </tr>      <tr>       <td>ENUM(‘value1’,’value2’,…)</td>       <td>1或2个字节,最多2^16 -1个值</td>      </tr>      <tr>       <td>SET(‘value1’,’value2’,…)</td>       <td>1、2、3、4或8个字节,最多64个成员</td>      </tr>     </tbody>    </table>    <h2>MySQL约束</h2>    <h3>主键约束</h3>    <p>NOT NULL 和 UNIQUE 的结合。确保某列(或两个列多个列的结合)有唯一标识,有助于更容易更快速地找到表中的一个特定的记录。</p>    <p>AUTO_INCREMENT 必须与 主键 组合使用,其会在新记录插入表中时生成一个唯一的数字。默认情况下,起始值为1,每次的增量为1。</p>    <pre>  <code class="language-sql">root@localhost test > create table tb3(                          -> id smallint unsigned AUTO_INCREMENT PRIMARY KEY,      -> username varchar(30) NOT NULL      -> );  root@localhost test > show columns from tb3;  root@localhost test > insert tb3(username) values('Tom');  root@localhost test > insert tb3(username) values('John');  root@localhost test > select * from tb3;  </code></pre>    <h3>非空约束</h3>    <p>NOT NULL 指示某列不能存储 NULL 值。在默认的情况下,表的列接受 NULL 值。</p>    <ul>     <li>NULL,字段值可以为空</li>     <li>NOT NULL,字段值禁止为空</li>    </ul>    <pre>  <code class="language-sql">root@localhost test > create table tb2(      -> username varchar(20) NOT NULL,      -> age tinyint unsigned NULL      -> );  root@localhost test > insert tb2 values('tom',NULL); # ok  root@localhost test > insert tb2 values(NULL,25);    # error  </code></pre>    <h3>唯一约束</h3>    <p>UNIQUE 保证某列的每行必须有唯一的值。唯一约束的字段可以为NULL值,但是必须只有一个NULL值。</p>    <p>请注意,每个表可以有多个 UNIQUE 约束,但是每个表只能有一个 PRIMARY KEY 约束。</p>    <pre>  <code class="language-sql">root@localhost test > create table tb5(      -> id smallint unsigned AUTO_INCREMENT PRIMARY KEY,      -> username varchar(20) NOT NULL UNIQUE KEY,      -> age tinyint unsigned      -> );  root@localhost test > show columns from tb5;  root@localhost test > insert tb5(username, age) values('Tom',22);  root@localhost test > insert tb5(username, age) values('Tom',22); # error  </code></pre>    <h3>默认约束</h3>    <p>为没用明确指出值的字段赋予默认值。</p>    <pre>  <code class="language-sql">root@localhost test > create table tb6(      -> id smallint unsigned AUTO_INCREMENT PRIMARY KEY,      -> username varchar(20) NOT NULL UNIQUE KEY,      -> sex ENUM('1','2','3') DEFAULT '3'      -> );  root@localhost test > insert tb6(username) values('Tom');  root@localhost test > select * from tb6;  </code></pre>    <h2>使用C/C++语言来操作MySQL</h2>    <p>我们可以使用命令行来操作MySQL数据库,譬如:</p>    <pre>  <code class="language-sql">root@localhost test > show databases;  root@localhost test > use mysql;  root@localhost test > show tables;  </code></pre>    <p>也可以通过MySQL对外公布的C接口来操作数据库,譬如:</p>    <pre>  <code class="language-sql">// show_tables.cxx    #include<mysql/mysql.h>  #include<stdio.h>  #include<stdlib.h>    intmain(){      MYSQL * conn;      MYSQL_RES * res;      MYSQL_ROW row;        char server[] = "localhost";      char user[] = "root";      char passwd[] = "lovemime";      char database[] = "mysql";        conn = mysql_init(NULL);      if(!mysql_real_connect(conn, server, user, passwd, database, 0, NULL, 0)) {          fprintf(stderr, "%s/n", mysql_error(conn));          exit(1);      }        if(mysql_query(conn, "show tables")){          fprintf(stderr, "%s/n", mysql_error(conn));          exit(1);      }            res = mysql_use_result(conn);        printf("MySQL Tables in mysql database:\n");        while((row =mysql_fetch_row(res)) != NULL){          printf("%s \n", row[0]);      }        mysql_free_result(res);      mysql_close(conn);        printf("finish! \n");      return 0;  }  </code></pre>    <p>连接MySQL库,来编译该段代码:</p>    <pre>  <code class="language-sql">$ g++ -Wall show_tables.cxx -o showtables -lmysqlclient  </code></pre>    <h2>参考链接</h2>    <ul>     <li><a href="/misc/goto?guid=4959733180465214719" rel="nofollow,noindex">菜鸟教程:SQL 教程</a></li>     <li><a href="/misc/goto?guid=4959733180550319929" rel="nofollow,noindex">菜鸟教程:MySQL 教程</a></li>     <li><a href="/misc/goto?guid=4959733180623808770" rel="nofollow,noindex">慕课视频:与MySQL的零距离接触</a></li>    </ul>    <p> </p>    <p>来自:http://freehacker.cn/reading/db/mysql-basic/</p>    <p> </p>