mysql数据库基本使用(增删改查)

数据库
db,database
数据库:依照某种数据模型进行组织并存放到存储器的数据集合
dbms,database management system
数据库管理系统:用来操作和管理数据库的大型服务软件
dbs,database system
数据库系统:db+dbms,指带有数据库并整合了数据库管理软件的计算机系统

mysql简介
最著名,应用最广泛的开源数据库软件,最早隶属于瑞典的mysql ab公司
崭新的开源分支mariadb

mysql的特点及应用
主要特点
适用于中小规模,关系型数据库系统
支持linux unix windows等多种操作系统
使用c和c++编写,可移植性强
通过api支持python java perl php等语言
典型应用环境
lamp平台,与apache http server组合
lnmp平台,与nginx组合

——————————————————————————————————————————

主配置文件:/etc/my.cnf
端口:3306
数据库目录:/var/lib/mysql
链接数据库初始随机密码:/root/.mysql_secret

安装mysql(从官网下载)
MySQL-embedded-5.6.15-1.el6.x86_64.rpm
MySQL-server-5.6.15-1.el6.x86_64.rpm
MySQL-shared-5.6.15-1.el6.x86_64.rpm
MySQL-shared-compat-5.6.15-1.el6.x86_64.rpm
MySQL-test-5.6.15-1.el6.x86_64.rpm

[root@mysql ~]# rpm -Uvh MySQL-*.rpm(安装)
[root@mysql ~]# service mysql start(起服务)
[root@mysql ~]# chkconfig mysql on(开机自启)
[root@mysql ~]# cat /root/.mysql_secret(查看初始随机密码)
# The random password set for the root user at Mon Aug 28 05:21:00 2017 (local time): 8qtuNmuK
[root@mysql ~]# mysql -uroot -p8qtuNmuK(登陆)
mysql> set password for root@"localhost"=password("123456");(修改登陆密码,退出重登)
[root@mysql ~]# mysql -uroot -p123456(新密码登陆)
mysql> show databases;(sql命令,每条命令必须要分号(;)结尾,不区分大小写)
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.06 sec)
mysql> show databases\c(结束命令以\c结束)

——————————————————————————————————————————————————

数据库的基本使用

库管理:
库的命名规则:
可以使用数字(不能使用纯数字),字母,下划线,具有唯一性,区分大小写
不要使用特殊字符和sql关键字
存储在数据库服务器上的数据,都是以文件形势存放数据库目录里
mysql> show databases;(查看已有的库)
mysql> create database hydra;(创建新库)
mysql> use hydra;(切换库)
mysql> select database();(显示当前所在库)
mysql> show tables;(显示当前库有哪些表)
mysql> drop database hydra;(删除库)

表管理:
表用来存储数据
mysql> create table 表名(字段名 类型,字段名 类型);(建表)
mysql> select * from 表名;(查看表记录)
mysql> desc 表名(查看表结构)
mysql> insert into 表名 values(字段值列表);(插入记录)
mysql> delete from 表名;(删除表记录)
mysql> drop table 表名;(删除表)

示例:
mysql> use hydra01;(进hydra01库)
mysql> create table hydra((建表)
-> name char(3),(char(3)限制名字有几个字符,默认1个)
-> age int(字段名 类型)
-> );
mysql> insert into hydra values("abc",21),("bcd",21);(插入记录)
mysql> select * from hydra;(查看表记录)
mysql> desc hydra(查看表结构)
mysql> delete from hydra;(删除表记录)
mysql> drop table hydra;(删除表)

mysql数据类型
字符类型
数值类型
日期时间类型
枚举类型

字符类型:(姓名 地址)
字符类型的值要用引号括起来
char:定长,最大为255
varchar:变长,最大为65532
mysql> create table hydraxx(name char(20),school char(10),mail varchar(30));(建表)
mysql> insert into hydraxx values("hydra","kailhydra","hydra@hydra.net")(插入数据)
mysql> select * from hydraxx;
+-------+-----------+-----------------+
| name | school | mail |
+-------+-----------+-----------------+
| hydra | kailhydra | hydra@hydra.net |
+-------+-----------+-----------------+

数值类型:(年龄 工资 学费 成绩)
整数和小数(浮点)类型
整型:根据存储数值的大小分为(有符号 无符号)
微小整型:tinyint 有符号:-128到127 无符号:0-255
小整型: smallint 有符号:-32768到32767 无符号:0-65535
中整型: mediumint
大整型: int
极大整型:bigint
浮点型:单精度 4个字节,双精度 8个字节
单精度:float(M,N)(M总位数,N小数的位数)
双精度:double(M,N)
mysql> create table t1(name char(10),age tinyint unsigned);
mysql> insert into t1 values("hydra",20)(unsigned为取反,默认为有符号)
mysql> select * from t1;
+-------+------+
| name | age |
+-------+------+
| hydra | 20 |
+-------+------+
mysql> create table t2(name char(10),age int unsigned);
mysql> insert into t2 values("xxxx",10000);
mysql> select * from t2;
+------+-------+
| name | age |
+------+-------+
| xxxx | 10000 |
+------+-------+
mysql> create table t3(name char(10),age bigint unsigned);(极大整数)
mysql> insert into t3 values("xxxxx",10000000);
mysql> select * from t3;
+-------+----------+
| name | age |
+-------+----------+
| xxxxx | 10000000 |
+-------+----------+
mysql> create table t5(name char(10),age tinyint(2) unsigned,pay float(7,2));(浮点,总的有7个数,两个小数)
mysql> insert into t5 values("hydra",20,20000.00);
mysql> select * from t5;
+-------+------+----------+
| name | age | pay |
+-------+------+----------+
| hydra | 20 | 20000.00 |
+-------+------+----------+
mysql> insert into t5 values("hydra1",21,20581.78);
mysql> select * from t5;
+--------+------+----------+
| name | age | pay |
+--------+------+----------+
| hydra | 20 | 20000.00 |
| hydra1 | 21 | 20581.78 |
+--------+------+----------+

日期时间类型:(生日 注册时间 入学时间)
year年:yyyy 2017
dete日期:yyyymmdd 20170828
time时间:hhmmss 184250
日期时间:datetime/timestamp yyyymmddhhmmss 20170828184250
datetime与timestamp区别:
datetime:1000-01-01 00:00:00.000000
9999-12-31 23:59:59.999999
timestamp:1970-01-01 00:00:00.000000
2038-01-10 03::14:07.999999
使用mysql的时间函数获取时间给字段赋值
now() year() month() date() day() time()
mysql> create table t6(name char(10),age tinyint(2) unsigned,pay float(7,2),birthdat date,class time,meetting datetime);
mysql> insert into t6 values("hydraxx",21,17800.15,20170828,190320,20170915203051);
mysql> select * from t6;
+---------+------+----------+------------+----------+---------------------+
| name | age | pay | birthdat | class | meetting |
+---------+------+----------+------------+----------+---------------------+
| hydraxx | 21 | 17800.15 | 2017-08-28 | 19:03:20 | 2017-09-15 20:30:51 |
+---------+------+----------+------------+----------+---------------------+
mysql> insert into t6 values("boy",21,17800.15,now(),now(),now());
mysql> select * from t6;
+---------+------+----------+------------+----------+---------------------+
| name | age | pay | birthdat | class | meetting |
+---------+------+----------+------------+----------+---------------------+
| hydraxx | 21 | 17800.15 | 2017-08-28 | 19:03:20 | 2017-09-15 20:30:51 |
| boy | 21 | 17800.15 | 2017-08-28 | 11:18:07 | 2017-08-28 11:18:07 |
+---------+------+----------+------------+----------+---------------------+
mysql> insert into t7 values(now(),now());
mysql> insert into t7 values(time2)(20170828193356);(给第二个字段赋值)
mysql> select * from t7;
+---------------------+---------------------+
| time1 | time2 |
+---------------------+---------------------+
| 2017-08-28 11:32:23 | 2017-08-28 11:32:23 |
| NULL | 2017-08-28 19:33:56 |
+---------------------+---------------------+

枚举类型:(兴趣 爱好 专业 部门)
字段的值只能在列举的列表里选择
enum(值列表):只能在列表选择一个值
set(值列表):可以在列表选择一个或多个值
mysql> create table t10(name char(10),age int,sex enum("boy","girl"),likes set("girl","linux"))
mysql> insert into t10 values("Anonymous",20,"boy","girl,linux");
mysql> select * from t10;
+-----------+------+------+------------+
| name | age | sex | likes |
+-----------+------+------+------------+
| Anonymous | 20 | boy | girl,linux |
+-----------+------+------+------------+
mysql> insert into t11 values("hydra",20,"1","girl,linux");(枚举可以用数字代表值,数字1代表第一个值)
mysql> select * from t11;
+-----------+------+------+------------+
| name | age | sex | likes |
+-----------+------+------+------------+
| Anonymous | 20 | boy | girl,linux |
| hydra | 20 | boy | girl,linux |
+-----------+------+------+------------+

约束条件:
是否允许给字段赋null(空)值,默认允许赋null值
not null(不允许赋空值)
Default:给字段设置默认值,当向表中插入新纪录时,不给字段赋值
给自己赋值,默认值在不设置的情况下,默认值的值是null值
default 值(默认值要与字段类型匹配)
mysql> create table t12(name char(10) not null,age int default 21,sex enum("boy","girl"),likes set("girl","linux","it"));
mysql> desc t12;
+-------+--------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------------------+------+-----+---------+-------+
| name | char(10) | NO | | NULL | |
| age | int(11) | YES | | 21 | |
| sex | enum('boy','girl') | YES | | NULL | |
| likes | set('girl','linux','it') | YES | | NULL | |
+-------+--------------------------+------+-----+---------+-------+
mysql> insert into t12 values("hydra",20,"boy","girl,linux");

修改表结构:
alter table 表名 执行动作;

执行动作:

添加新字段
add 字段名 类型(宽度) 约束条件;
first:把新字段添加在所有字段的前边
after:把新字段添加在指定字段的后面
mysql> alter table t12 add qq char(10),add mial varchar(30) default Anonymous@hydra.net;
mysql> select * from t12;
+-------+------+------+------------+------+---------------------+
| name | age | sex | likes | qq | mial |
+-------+------+------+------------+------+---------------------+
| hydra | 20 | boy | girl,linux | NULL | Anonymous@hydra.net |
+-------+------+------+------------+------+---------------------+
mysql> alter table t12 add id char(9) first;(把新字段添加在所有字段前边)
mysql> alter table t12 add tel char(11) after name;(把新字段添加在name字段后面)
mysql> desc t12;
mysql> desc t12;
+-------+--------------------------+------+-----+---------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------------------+------+-----+---------------------+-------+
| id | char(9) | YES | | NULL | |
| name | char(10) | NO | | NULL | |
| tel | char(11) | YES | | NULL | |
| age | int(11) | YES | | 21 | |
| sex | enum('boy','girl') | YES | | NULL | |
| likes | set('girl','linux','it') | YES | | NULL | |
| qq | char(10) | YES | | NULL | |
| mial | varchar(30) | YES | | Anonymous@hydra.net | |
+-------+--------------------------+------+-----+---------------------+-------+

删除已有字段
drop 字段名;
mysql> alter table t12 drop age;
mysql> desc t12;
+-------+--------------------------+------+-----+---------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------------------+------+-----+---------------------+-------+
| id | char(9) | YES | | NULL | |
| name | char(10) | NO | | NULL | |
| tel | char(11) | YES | | NULL | |
| sex | enum('boy','girl') | YES | | NULL | |
| likes | set('girl','linux','it') | YES | | NULL | |
| qq | char(10) | YES | | NULL | |
| mial | varchar(30) | YES | | Anonymous@hydra.net | |
+-------+--------------------------+------+-----+---------------------+-------

修改字段类型(修改后的类型不能和已有的数据冲突)
modify 字段名 类型(宽度) 约束条件;
mysql> alter table t12 modify qq varchar(10);
+-------+--------------------------+------+-----+---------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------------------+------+-----+---------------------+-------+
| id | char(9) | YES | | NULL | |
| name | char(10) | NO | | NULL | |
| tel | char(11) | YES | | NULL | |
| sex | enum('boy','girl') | YES | | NULL | |
| likes | set('girl','linux','it') | YES | | NULL | |
| qq | varchar(10) | YES | | NULL | |
| mial | varchar(30) | YES | | Anonymous@hydra.net | |
+-------+--------------------------+------+-----+---------------------+-------+

修改字段名
change 原字段名 新字段名 类型(宽度) 约束条件;
mysql> alter table t12 change qq twitter varchar(30);
+---------+--------------------------+------+-----+---------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------------------+------+-----+---------------------+-------+
| id | char(9) | YES | | NULL | |
| name | char(10) | NO | | NULL | |
| tel | char(11) | YES | | NULL | |
| sex | enum('boy','girl') | YES | | NULL | |
| likes | set('girl','linux','it') | YES | | NULL | |
| twitter | varchar(30) | YES | | NULL | |
| mial | varchar(30) | YES | | Anonymous@hydra.net | |
+---------+--------------------------+------+-----+---------------------+-------+
综合使用:
mysql> alter table t12 add cj int(3) default 100,drop tel,modify twitter varchar(50);
mysql> desc t12;
+---------+--------------------------+------+-----+---------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------------------+------+-----+---------------------+-------+
| id | char(9) | YES | | NULL | |
| name | char(10) | NO | | NULL | |
| sex | enum('boy','girl') | YES | | NULL | |
| likes | set('girl','linux','it') | YES | | NULL | |
| twitter | varchar(50) | YES | | NULL | |
| mial | varchar(30) | YES | | Anonymous@hydra.net | |
| cj | int(3) | YES | | 100 | |
+---------+--------------------------+------+-----+---------------------+-------+

——————————————————————————————————————————————————————————————————

上一篇:ZOJ1654 Place the Robots(二分图最大匹配)


下一篇:C++程序面试笔迹六