客户端把数据存储到数据库服务器上的过程
建库的基本命令 create database 库名(必须唯一性!!!)
[root@host50 ~]# mysql -uroot -pxxxx //数据库管理员登陆
mysql> select user(); # 查看登陆的用户
+----------------+
| user() |
+----------------+
| root@localhost | 用户@客户端地址 (在本机管理员登录)
+----------------+
1 row in set (0.00 sec)
mysql> show databases; # 查看已有的库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| tarena |
+--------------------+
5 rows in set (0.00 sec)
mysql> select database(); # 查看当前所在的库
+------------+
| database() |
+------------+
| NULL | 表示没有在任何一个库,此时所在的位置是/var/lib/mysql 目录
+------------+
1 row in set (0.00 sec)
mysql> create database db1 # 创建新库
Query OK, 1 row affected (0.00 sec)
mysql> show databases; 此时查看已有库
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 | 刚创建的db1库 , 在数据库目录/var/lib/myql 下会有对应名称的db1目录
| mysql |
| performance_schema |
| sys |
| tarena |
+--------------------+
6 rows in set (0.00 sec)
mysql> system ls -ld /var/lib/mysql/db1; # 使用system 命令 在登录状态下执行系统命令
drwxr-x--- 2 mysql mysql 20 9月 7 13:52 /var/lib/mysql/db1
mysql> create database if not exists DB1; 加if not exists语句避免重名报错
Query OK, 1 row affected, 1 warning (0.01 sec)
// 删除库 如果库里有表会一并被删除
mysql> drop database db1 ; 删除没有的库,会报错
mysql> drop database if exists db1; 加if exists 语句 避免报错
建表命令格式
create table 库名.表名(
表头名 数据结构 [约束条件],
表头名 数据结构 [约束条件]
);
使用建表范式衡量表创建的是否合理
mysql> create table DB1.t1(姓名 char(10),班级 char(7)); # 在DB1库里创建t1表
mysql> desc DB1.t1; 查看表头信息
+--------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| 姓名 | char(10) | YES | | NULL | |
| 班级 | char(7) | YES | | NULL | |
+--------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> drop table DB1.t1; 删除创建的表
Query OK, 0 rows affected (0.11 sec)
为了便于操作通常使用英文定义表头名和表名
mysql> create table DB1.t1 ( name char(15) , class char(10));
Query OK, 0 rows affected (0.25 sec)
mysql> desc DB1.t1; 查看表头
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name | char(15) | YES | | NULL | |
| class | char(10) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
发现表建的不合理 可以修改表
mysql> alter table DB1.t1 add school char(20) ; 添加1个新字段school
mysql> desc DB1.t1; 默认新添加的字段追加在已有列的末尾
+--------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| name | char(15) | YES | | NULL | |
| class | char(10) | YES | | NULL | |
| school | char(20) | YES | | NULL | |
+--------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)
一起添加2个字段 分别是 mail 和 address
mysql> alter table DB1.t1 add mail char(50) , add address char(80) ;
Query OK, 0 rows affected (0.37 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc DB1.t1; 查看新添加的表头,都在追加在末尾
+---------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+-------+
| name | char(15) | YES | | NULL | |
| class | char(10) | YES | | NULL | |
| school | char(20) | YES | | NULL | |
| mail | char(50) | YES | | NULL | |
| address | char(80) | YES | | NULL | |
+---------+----------+------+-----+---------+-------+
5 rows in set (0.00 sec)
添加新表头number 在第1列的位置
mysql> alter table DB1.t1 add number int first ;
Query OK, 0 rows affected (0.32 sec)
Records: 0 Duplicates: 0 Warnings: 0
添加新表头在city 放在name的后边
mysql> alter table DB1.t1 add city char(10) after name;
Query OK, 0 rows affected (0.42 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc DB1.t1;
+---------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+-------+
| number | int(11) | YES | | NULL | |
| name | char(15) | YES | | NULL | |
| city | char(10) | YES | | NULL | |
| class | char(10) | YES | | NULL | |
| school | char(20) | YES | | NULL | |
| mail | char(50) | YES | | NULL | |
| address | char(80) | YES | | NULL | |
+---------+----------+------+-----+---------+-------+
7 rows in set (0.00 sec)
mysql> alter table DB1.t1 drop city; 一次删除一个表头
Query OK, 0 rows affected (0.31 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table DB1.t1 drop class , drop school; 一起删除多个表头
Query OK, 0 rows affected (0.32 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc DB1.t1; 查看表头
+---------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+-------+
| number | int(11) | YES | | NULL | |
| name | char(15) | YES | | NULL | |
| mail | char(50) | YES | | NULL | |
| address | char(80) | YES | | NULL | |
+---------+----------+------+-----+---------+-------+
4 rows in set (0.00 sec)
// 修改表头信息
mysql> alter table DB1.t1 modify number tinyint after name; 修改类型和位置
mysql> desc DB1.t1; 查看修改,把类型修改为tinyint 并移动name表头的后边
+---------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------+------+-----+---------+-------+
| name | char(15) | YES | | NULL | |
| number | tinyint(4) | YES | | NULL | |
| mail | char(50) | YES | | NULL | |
| address | char(80) | YES | | NULL | |
+---------+------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> alter table DB1.t1 change address homedir char(80); 只修改表头名,类型原样抄下来
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc DB1.t1;查看修改,address 修改为了 homedir
+---------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------+------+-----+---------+-------+
| name | char(15) | YES | | NULL | |
| number | tinyint(4) | YES | | NULL | |
| mail | char(50) | YES | | NULL | |
| homedir | char(80) | YES | | NULL | |
+---------+------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> alter table DB1.t1 change email vachar(60); 表头名和类型一起修改
Query OK, 0 rows affected (0.44 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc DB1.t1;查看修改表头名类型都变了
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | char(15) | YES | | NULL | |
| number | tinyint(4) | YES | | NULL | |
| email | varchar(60) | YES | | NULL | |
| homedir | char(80) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> alter table DB1.t1 rename stuinfo; 修改表名为 stuinfo
mysql> show tables; 查看表名 已经改变
+---------------+
| Tables_in_DB1 |
+---------------+
| stuinfo |
+---------------+
mysql> create table DB1.t2 select * from tarena.user where 1=2 ; 复制表结构及数据
mysql> create table DB1.t3 select * from tarena.user
create table 库.表名 like 库.表名
# 原来的key也会复制给新表 复制不了数据
对表中存储的行做管理 操作 包括
添加1行给所有表头赋值
mysql> insert into DB1.stuinfo values ("yaya",1,"[email protected]","beijing");
Query OK, 1 row affected (0.03 sec)
添加1行 ,给指定的表头赋值,
mysql> insert into DB1.stuinfo (name,homedir)values("nb","[email protected]");
Query OK, 1 row affected (0.04 sec)
没有赋值的表头number和email 没有数据 ,值是 null (空)
mysql> select * from DB1.stuinfo;
+------+--------+--------------+-------------+
| name | number | email | homedir |
+------+--------+--------------+-------------+
| yaya | 1 | [email protected] | beijing |
| plj | 8 | [email protected] | shanghai |
| jing | 9 | [email protected] | beijing |
| nb | NULL | NULL | [email protected] |
| nb2 | NULL | NULL | [email protected] |
| nb3 | NULL | NULL | [email protected] |
+------+--------+--------------+-------------+
6 rows in set (0.00 sec)
使用set语句插入记录
mysql> insert into DB1.stuinfo set name="plj" , number=10;
mysql> update DB1.stuniof set email="[email protected]" where email is null;
不加条件批量修改
mysql> update DB1.stuinfo set homedir="beijing";
// 只删除与条件符合的行
delete from DB1.stuinfo where name regexp '^nb';
//删除表里的所有行
delete from 库.表
也可用truncate table 库.表;
自增长列 truncate后从1开始 delete继续变好
truncate不能回滚 delete可以
效率略高于delete
包括 数值类型、字符类型、日期时间类型、枚举类型
mysql> show create table db1.t3 # 查看字符集
ENGINE=InnoDB # 定义存储引擎
DEFAULT CHARSET =latin1 # 定义字符集
# 建表的时候指定使用的字符集
create table db1.t4(姓名 char(3),地址 varchar(10)) DEFAULT CHARSET utf8;
mysql> create table db1.t6(
姓名 char(4),
生日 date,
上课时间 time,
家庭聚会 datetime,
聚会地点 varchar(10)
) DEFAULT CHARSET utf8;
mysql> alter table db1.t6 add 出生年份 year after 姓名;
给表头赋值时,值必须在类型规定的范围内选择
create table db1.t8(姓名 char(10) , 性别 enum("男","女","保密") ,
爱好 set("帅哥","金钱"。)
);
一次性向表里存储很多数据 或 一次性把表里的数据都取出来
注意:数据导入或导出 存放数据的文件必须在mysql服务要求的目录下 叫检索目录
要知道数据库默认的检索目录
会修改数据库默认的检索目录
查看默认的检索目录mysql > show variables like "secure_file_priv"
vim /etc/my.cnf [mysql] secure_file_priv=/myload # 手动添加 mkdir /myload chown mysql /myload/ # 给权限