DDL:数据定义语言,定义数据库对象(库,表,列,索引)的操作。
DML:数据操作语言,定义对数据库记录的操作。
DCL:数据控制语言,定义对数据库、表、字段、用户的访问权限和安全级别。(授权grant,收回权限revoke等)。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| gongsi |
| mysql |
| performance_schema |
+--------------------+
这里面中的information_schema主要存储的是系统中的一些数据库的对象信息。(例如:用户表信息,字符集信息。分区信息等)
mysql存储了系统的用户权限信息以及帮助信息。
有时还会自带一个test数据库,测试数据库,任何用户都可以使用。
语法:create database [if not exists] databasename;
如果该数据库已经存在,将会报错。
语法:drop database [if exists ] databasename;
使用该命令需要谨慎。
语法:use databasename;
latin1支持西欧字符,希腊字符等。
gbk支持中文简体字符。
big5支持繁体字符。
utf8支持全国所有的语言。
所以一般在建立数据库时需要指明数据库的字符集为utf8.
查看:
mysql> show variables like 'character%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+
可以看到character_set_database 为latin1 ,所以我需要修改其字符集类型,否则我在建立每一个数据库时都需要在末尾加入 default character-set=‘utf8’;会是及其麻烦的。
修改方法:
在/etc/my.cnf文件的[mysqld]下添加:default-character-set=utf8(当你发现修改完后导致服务启动不了了,有可能是你的mysql版本太高了,5.5之后的需要使用character-set-server=utf8)。
在client下面也添加:default-character-set=utf8
如果使用了rpm格式装的mysql,是不会在/etc下创建my.cnf文件的。那么就需要在/usr/local/mysql下面拷贝一份my.cnf去/etc下即可!。再进行配置。
配置完毕后重启服务!
修改完后的表:
mysql> show variables like 'character%'; +--------------------------+---------------------------------------------------------------+ | Variable_name | Value | +--------------------------+---------------------------------------------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/local/mysql-5.6.32-linux-glibc2.5-x86_64/share/charsets/ | +--------------------------+---------------------------------------------------------------+ 8 rows in set (0.00 sec)
mysql> select database();
+------------+
| database() |
+------------+
| kaoshi |
+------------+
1 row in set (0.00 sec)
mysql> show create database kaoshi;
+----------+-------------------------------------------------------------------+
| Database | Create Database |
+----------+-------------------------------------------------------------------+
| kaoshi | CREATE DATABASE `kaoshi` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+-------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.32 |
+-----------+
1 row in set (0.00 sec)
mysql> system cat /etc/passwd;
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
........
mysql> show tables;
+------------------+
| Tables_in_kaoshi |
+------------------+
| kaoshi |
| test1 |
| test2 |
| test3 |
| workInfo |
+------------------+
5 rows in set (0.00 sec)
mysql> desc test1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| num | int(11) | YES | | NULL | |
| pass | varchar(50) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
或者使用:
mysql> show create table test1\G;
*************************** 1. row ***************************
Table: test1
Create Table: CREATE TABLE `test1` (
`id` int(11) DEFAULT NULL,
`num` int(11) DEFAULT NULL,
`pass` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
语法:drop table [if exists ] tablename;
mysql> desc test1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| num | int(11) | YES | | NULL | |
| pass | varchar(50) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> alter table test1 modify id int(2);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table test1 add name varchar(20);
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table test1 change id ID int(11);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table test1 rename text;
Query OK, 0 rows affected (0.00 sec)
mysql> desc text;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| ID | int(11) | YES | | NULL | |
| num | int(11) | YES | | NULL | |
| pass | varchar(50) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> rename table text to test1;
Query OK, 0 rows affected (0.00 sec)
mysql> desc test1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| ID | int(11) | YES | | NULL | |
| num | int(11) | YES | | NULL | |
| pass | varchar(50) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
语法一:create table newtablename like oldtablename;
语法二:create table newtablename select * from oldtablename;
mysql> create table test3 like test2;
ERROR 1050 (42S01): Table 'test3' already exists
mysql> create table test4 like test2;
Query OK, 0 rows affected (0.04 sec)
mysql> desc test4;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | MUL | NULL | |
| num | int(11) | YES | | NULL | |
| pass | varchar(50) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> select * from kaoshi;
+------+--------+
| id | name |
+------+--------+
| 1 | gaofan |
| 1 | gaofan |
| 2 | tom |
+------+--------+
3 rows in set (0.06 sec)
mysql> create table test5 select * from kaoshi limit 2;
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from test5;
+------+--------+
| id | name |
+------+--------+
| 1 | gaofan |
| 1 | gaofan |
+------+--------+
2 rows in set (0.01 sec)