您的位置:首页 > 数据库 > MySQL

liunx中(mysql 索引住主外键关系对数据库的操作深入学习)

2013-09-13 20:08 1006 查看
上次说到数据库的使用,是对mysql数据库的基本更、删、改、查、现在要说说如何对数据库的表做一些修饰了,希望大家好好学习。都是我辛苦的记录下来的,真的很不容易,能和你们分享我知足了。

key 索引字段 兼职字段 (比如:id不能重复)

索引 (如果把表中模个字段设置索引,可以根据索引快速查询出来,可以加快查询速度,但是减慢更新的速度 文件MYI文件存索引信息)

btree算法 把100分成50 在把50分成25 一直分下去 方便查找,加快查询速度
--------------------------
index (显示MUL 可以重复)
设置索引 把员工5里的name字段 设置成索引
create index name on yg5(name);

--------------------------
unique (显示UNI 不可以重复)

设置索引 把员工5里的name字段 设置成索引
mysql> create unique index stu_id on yg5(stu_id);

-------------------------
查看表中的索引信息
show index from yg5;

----------------------
删除 索引 索引名
drop index stu_id on yg5;

-----------------
创建表 之初就添加索引

mysql> create table aa(
-> name char(3) not null,
-> sex enum("w","n") default "w",
-> set_id char(4),
-> index(name),
-> unique(stu_id)
-> );

-----------------------
主建 (显示PRI 必须是整数不能是字符)
primary key auto_increment 0 (主键并且自动增长,只有之间自动增加,两个必须连用,一个表中只能设置一个,一般把不能重复的字段设为比如:id)

设置主建

mysql> create table yg20(
-> id int(3) primary key auto_increment,
-> name char(3) not null);
-----------------------
零一个方法
mysql> create table yg12(
-> id int(3) auto_increment,
-> name char(3),
-> primary key(id)
-> );

把UNI(unique) 的NULL为NO 他就会变成 PRI (主建)

-----------------------------------
主键自动增长

mysql> insert into yg12 (name) values ("tom");

mysql> insert into yg12 (name) values ("xx");

mysql> select * from yg12;
+----+------+
| id | name |
+----+------+
| 1 | tom |
| 2 | xx |
+----+------+

----------------------------------
创建表以后,把没有主建的设为主键
mysql> alter table a add primary key(id);
在添加 自动增加
mysql> alter table a modify id int(3) not null auto_increment;

----------------------
删除主键 必须先删除自动增长
mysql> alter table a modify id int(3) not null,先删除自动增长
mysql> alter table a dorp primary key; 在删除 主键

---------------------
附和主建 (把俩个字段设成一个主键,在建立表的时候就创建好,要不就要 先把主建删除,在创建修改成附和主键)(附和主键就是相当于把俩个合并成一个主键,俩个值合起来不重复就可以)

primary primary
hostip port ser_name stat(allow,deny)

创建
mysql> create table server(
-> hostip varchar(15) not null,
-> port varchar(5) not null,
-> sername varchar(15) not null,
-> stat enum("allow","deny") not null default "deny",
-> primary key(hostip,port)
-> );

mysql> insert into server values ("1.1.1.1","21","ftp","allow");
mysql> select * from server;

+---------+------+---------+-------+
| hostip | prot | sername | stat |
+---------+------+---------+-------+
| 1.1.1.1 | 21 | ftp | allow |
+---------+------+---------+-------+

mysql> insert into server values ("1.1.1.1","22","sshd","allow");

mysql> select * from server;
+---------+------+---------+-------+
| hostip | prot | sername | stat |
+---------+------+---------+-------+
| 1.1.1.1 | 21 | ftp | allow |
| 1.1.1.1 | 22 | sshd | allow |
+---------+------+---------+-------+

mysql> insert into server values ("1.1.1.2","22","sshd","deny");

mysql> select * from server;
+---------+------+---------+-------+
| hostip | prot | sername | stat |
+---------+------+---------+-------+
| 1.1.1.1 | 21 | ftp | allow |
| 1.1.1.1 | 22 | sshd | allow |
| 1.1.1.2 | 22 | sshd | deny |
+---------+------+---------+-------+

---------------------------- ------------------
外建foreign key (与存储有关)比如:删除用户带着帖子一起删除 这个不和表有关和存储引擎

当前服务器使用的存储引擎
存储引擎 是开发者作好的 不同的存储引擎 支持的类型不同 功能也不同

mysql> show variables like "table_type";
+---------------+--------+
| Variable_name | Value |
+---------------+--------+
| table_type | MyISAM |
+---------------+--------+

查看当前服务器存储引擎信息 \G换行显示 默认的是MyISAM
mysql> show engines\G;

*************************** 3. row ***************************
Engine: MyISAM
Support: DEFAULT
Comment: Default engine as of MySQL 3.23 with great performance
Transactions: NO
XA: NO
Savepoints: NO
*************************** 4. row ***************************
Engine: InnoDB
Support: YES
Comment: Supports transactions, row-level locking, and foreign keys
Transactions: YES
XA: YES
Savepoints: YES

------------------------
MyISAM 和 InnoDB 的区别

MyISAM 是独享表空间 一个表一个文件 支持表级锁定 不支持行级锁定
InnoDB 是共享表空间 支持事务回滚 锁定事务的方式 支持行级别锁定 也支持表级别锁定
锁又分为 读锁(共享锁) 和写锁(互斥锁)
写锁优先于读锁
内存里的数据没有到硬盘上叫做脏数据
---------------------
查看默认创建的是什么引擎
mysql> show create table ctab;
+-------+----------------------------------------------
| Table | Create Table |
+-------+----------------------------------------------
| ctab | CREATE TABLE `ctab` (
`id` int(2) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |

---------------------------
创建表的时候指定引擎
mysql> create table a(id int(3))engine=innodb;

除此启动文件就生好了 ib_logfile1 日志文件
[root@xu var]# cat /usr/local/mysql/var/
db1/ farm/ ib_logfile1 ucenter/ xu.qq.com.pid
db2/ ibdata1 mysql/ xu/
discuz/ ib_logfile0 test/ xu.qq.com.err

----------------------------------------------
修改表的引擎 (最好不要该,该了就数据就会有相应的变化)
mysql> alter table a engine=myisam;

-------------------------------
修改默认的引擎
vim /etc/my.cnf
2 default-storage-engine=innodb 添加

重启服务
service mysqld restart

------------------------------
外建在创建过程中查看

一个子表只能有一个父表
一个父表可以有多个子表
父表删除 子表跟着同步
---------------------------------------

创建外建表
yg_tab
yg_id int(3) 员工编号 primary key auto_increment
name char(3) not null

gz_tab
id int(3) 工资记录编号 primary key auto_increment
gz_id int(3)
gz float(7,2)
index(gz_id)

qxy 500000.88

-------------------------------------------------
查看外建
mysql> show create table tt_tab;

开始创建外建

mysql> create table yg(
-> yg_id int(3) primary key auto_increment,
-> name char(3) not null
-> ) engine=innodb;

mysql> create table gz(
-> id int(3) primary key auto_increment,
-> gz_id int(3) not null,
-> gz float(7,2) not null, 子表 父表
-> index(gz_id),foreign key(gz_id) references yg(yg_id) on update cascade on delete cascade)engine=innodb;

mysql> insert into yg1 (name) values ("qxy");
select * from yg1;

mysql> insert into gz1 (gz_id,gz) values(1,500000.88);

-------------------------------
添加信息
mysql> insert into yg (name) values ("xzy");
mysql> select * from yg1;
+-------+------+
| yg_id | name |
+-------+------+
| 1 | qxy |
| 2 | xzy |
+-------+------+

mysql> insert into gz1 (gz_id,gz) values (2,70000.88);
mysql> select * from gz1;
+----+-------+----------+
| id | gz_id | gz |
+----+-------+----------+
| 1 | 1 | 99999.99 |
| 3 | 2 | 70000.88 |
+----+-------+----------+
-----------------------------------------
更改父表信息
mysql> update yg1 set yg_id="8" where yg_id="2";
mysql> select * from yg1;
+-------+------+
| yg_id | name |
+-------+------+
| 1 | qxy |
| 8 | xzy |
+-------+------+

mysql> select * from gz1;
+----+-------+----------+
| id | gz_id | gz |
+----+-------+----------+
| 1 | 1 | 99999.99 |
| 3 | 8 | 70000.88 |
+----+-------+----------+

-----------------------------------
删除父表信息 子表跟着同步
mysql> delete from yg1 where yg_id="8";
mysql> select * from yg1;
+-------+------+
| yg_id | name |
+-------+------+
| 1 | qxy |
+-------+------+

mysql> select * from gz1;
+----+-------+----------+
| id | gz_id | gz |
+----+-------+----------+
| 1 | 1 | 99999.99 |
+----+-------+----------+

------------------------------------------
数据导入
passwd 导入到数据库
root:x:0:0:root:/root:/bin/bash
mysql> create table userlist1( username varchar(15) not null, pword char(1) , uid int(3) not null, gid int(3) not null, jml varchar(30) , homedir char(50),shell varchar(20),index(username) );

mysql> desc userlist1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(15) | NO | MUL | NULL | |
| pword | char(1) | YES | | NULL | |
| uid | int(3) | NO | | NULL | |
| gid | int(3) | NO | | NULL | |
| jml | varchar(30) | YES | | NULL | |
| homedir | char(50) | YES | | NULL | |
| shell | varchar(20) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+

数据导入
加载 数据 输入到文件 输入 表 领域 结束 经过 路线 结束 经过 空格
load data infile '文件名' into table 表名 fields terminated by "字段间隔服" lines terminated by "\n";

mysql> load data infile "/etc/passwd" into table userlist fields terminated by ":" lines terminated by "\n";

mysql> select * from userlist;

数据导出
选择 * 来自 输入 输出文件 领域 结束 经过 路线 结束 经过 空格
mysql> select * from userlist into outfile "userlist.txt" fields terminated by "#" lines terminated by "\n";

导出的文件在/usr/local/mysql/var/db3/userlist.txt

------------------------------------
表中记录的操作

增 insert into
删 delete
该 update
查select

查询的格式
select 字段名列表 from 数据名.表名 where 条件表达式列表

name,age

条件表达式

= > >= < <=

表达式1 and 表达式2 and 表达式3 (和)
表达式1 ro 表达式1 (或)

age between 30 and 40 这个之间
age >=30 and age<=40

age in (30,29,21)
age=30 or age=29 or age=21

age not in (30,29,21)

把uid 值在10到20之间的用户名和shell显示出来

mysql> select username,shell from userlist1 where uid >=10 and uid<=20;
+----------+---------------+
| username | shell |
+----------+---------------+
| uucp | /sbin/nologin |
| operator | /sbin/nologin |
| games | /sbin/nologin |
| gopher | /sbin/nologin |
| ftp | /sbin/nologin |
| oprofile | /sbin/nologin |
+----------+---------------+

显示使用的shell 是/sbin/nologin 且 uid 号小于 10的用户

mysql> select shell,uid from userlist1 where shell="/sbin/nologin" and uid<10;
+---------------+-----+
| shell | uid |
+---------------+-----+
| /sbin/nologin | 1 |
| /sbin/nologin | 2 |
| /sbin/nologin | 3 |
| /sbin/nologin | 4 |
| /sbin/nologin | 8 |
+---------------+-----+

模糊查询
like “a%” 一个到多个
mysql> select * from pass where user like "root%";

like “a_” ; 开头是a的
mysql> select * from pass where user like "root_";

like “a_c”; 三个字母 第一个是a 的三个是c的
mysql> select * from pass where user like "a_c";

分组
mysql> select uid,shell from userlist group by shell having uid<=500;

看查询的表中前10行信息 (从第几行显示 ,显示几个)
mysql> select username,uid ,shell from userlist limit 3,5;

排序 uid最高的前10个信息
排寻 降序 从第一行显示显示10行
mysql> select username,uid from usrelist order by uid desc limit 10;

统计username 的有多个
mysql> select count(username) from userlist;

统计每个表中 所有的记录 (空值也算)
mysql> select count(*) from userlist;

加法
mysql> select min(uid) from userlist;

减法
mysql> select sum(uid) from userlist;

查看uid和gid和值
俩加起来 和 别名
mysql> select username uid+gid as he from userlist;

本文出自 “history_xcy” 博客,请务必保留此出处http://historys.blog.51cto.com/7903899/1296701
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: