mysql数据库基础知识

sql:结构化查询语句

rdbms:关系型数据库管理系统

 

数据库设计三大范式:

1:原子性。数据库的字段都是具有单一属性的,不可再分。

2:唯一性。每个非主属性都完全函数依赖于键码。记录具有唯一标识。每列都跟主键有关系。(以一对多为例)

3:消除传递依赖。每个非主属性都不伟递领带于键码。即任何字段不能由其他字段派生出来,它要求字段没有冗余。

  没有冗余的数据库设计可以做到。但是,没有冗余的数据库未必是最好的数据库,有时为了提高运行效率,就必须降低范式标准,适当保留冗余数据。具体做法是:在概念数据模型设计时遵守第三范式,降低范式标准的工作放到物理数据模型设计时考虑。降低范式就是增加字段,允许冗余。


 INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录。LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录。RIGHT JOIN(右连接): 与 LEFT JOIN 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。

root@host# mysql -u root -p password;
Enter password:*******
mysql> use RUNOOB;
Database changed
mysql> SELECT * FROM tcount_tbl;
+-----------------+----------------+
| runoob_author | runoob_count |
+-----------------+----------------+
| mahran          |             20 |
| mahnaz          |           NULL |
| Jen             |           NULL |
| Gill            |             20 |
| John Poul       |              1 |
| Sanjay          |              1 |
+-----------------+----------------+
6 rows in set (0.01 sec)
mysql> SELECT * from runoob_tbl;
+-------------+----------------+-----------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-------------+----------------+-----------------+-----------------+
|           1 | Learn PHP      | John Poul       | 2007-05-24      |
|           2 | Learn MySQL    | Abdul S         | 2007-05-24      |
|           3 | JAVA Tutorial  | Sanjay          | 2007-05-06      |
+-------------+----------------+-----------------+-----------------+
3 rows in set (0.00 sec)
mysql>
接下来我们就使用MySQL的 INNER JOIN(也可以省略 INNER 使用 JOIN,效果一样) 来连接以上两张表来读取runoob_tbl表中所有runoob_author字段在tcount_tbl表对应的runoob_count字段值:

mysql> SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob_tbl a INNER JOIN tcount_tbl b ON a.runoob_author = b.runoob_author;
+-----------+---------------+--------------+
| runoob_id | runoob_author | runoob_count |
+-----------+---------------+--------------+
|         1 | John Poul     |            1 |
|         3 | Sanjay        |            1 |
+-----------+---------------+--------------+
2 rows in set (0.00 sec)
等价于:
mysql> SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob_tbl a, tcount_tbl b WHERE a.runoob_author = b.runoob_author;
mysql> SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob_tbl a LEFT JOIN tcount_tbl b ON a.runoob_author = b.runoob_author;
+-------------+-----------------+----------------+
| runoob_id | runoob_author | runoob_count |
+-------------+-----------------+----------------+
|           1 | John Poul       |              1 |
|           2 | Abdul S         |           NULL |
|           3 | Sanjay          |              1 |
+-------------+-----------------+----------------+
3 rows in set (0.02 sec)
mysql> SELECT b.runoob_id, b.runoob_author, a.runoob_count FROM tcount_tbl a RIGHT JOIN runoob_tbl b ON a.runoob_author = b.runoob_author;
+-------------+-----------------+----------------+
| runoob_id | runoob_author | runoob_count |
+-------------+-----------------+----------------+
|           1 | John Poul       |              1 |
|           2 | Abdul S         |           NULL |
|           3 | Sanjay          |              1 |
+-------------+-----------------+----------------+
3 rows in set (0.02 sec)

http://www.runoob.com/mysql/mysql-join.html

 

DML 部分:

SELECT - 从数据库表中获取数据

UPDATE - 更新数据库表中的数据

DELETE - 从数据库表中删除数据

INSERT INTO - 向数据库表中插入数据

DDL 语句:

CREATE DATABASE - 创建新数据库

ALTER DATABASE - 修改数据库

CREATE TABLE - 创建新表

ALTER TABLE - 变更(改变)数据库表

DROP TABLE - 删除表

CREATE INDEX - 创建索引(搜索键)

DROP INDEX - 删除索引


更新重点句子:

alter table add column price int(10)

alter table drop name;

drop table cq_user;

select distinct name from cq_itemtype;

select * from cq_user where namein('haha2','haha1;);

select * from cq_user wherename='haha1' or name='haha2';

select * from cq_itemtype where namelike '%石%';

select * from cq_itemtype where namelike '50朵_ _玫瑰';(注意是两个_,因为白、红是汉字)

select * from cq_itemtype where id like'111_ _ _';(111开头的6位数的id)

select * from cq_action where paramlike '!%data%' escape '!';

select count(*) from usertype;

select max(level) from cq_user;

select * from cq_itemtype group byname;

select * from cq_itemtype order bydesc/asc;

update cq_itemtype set name='白色',level=10 where id=10;

update cq_user set level=level+1;

delete from cq_action where id=1;

 

类型属性:

◦ZEROFILL

–适用于所有数值类型数据数据列

–作用:如果数值的宽度小于定义的显示宽度,则在数值前填充0

◦UNSIGNED

–作用:不允许数据列中出现负数,无符号型

◦AUTO_INCREMENT  自增长

◦NULL/NOT NULL

–作用:控制数据列的值是否为空

 

主要的数据类型:

整数数据类型:int 4个字节

浮点数据类型:float4个字节,double8个字节

日期时间类型:date(0000-00-00),datetime(0000-00-0000:00:00)

字符串类型:char(n),varchar(n)

 

创建表:

CREATE TABLE<表名>

( <列名> <数据列类型><列属性> [{<约束>}],

          <列名> <数据列类型><列属性>[{<约束>}],

        ……

        )

create table cq_action(

        id int(4) not null primary key,

        name int(4))

create table cq_action(

        id int(4) not null,

        name int(4),

        primary key(id))

表的修改:

alter table <表名> add<列定义>|<列约束>

alter table cq_action add price int(4)

alter tabke cq_action alter priceint(10)

alter table cq_action drop name

删除表“

drop table cq_name

 

SELECT命令的格式

select [all|distinct] 列名 [[as] 别名]

from 表名[[as]表别名]

[where <检索条件>]

[group by <列名>[having<条件表达式>]]

[order by <列名>[asc|desc]]

[limit 行数]

select * from cq_action

select distinct name from cq_action(显示表cq_action中的name字段,去除重复)

select name as username from cq_action(查询显示的结果为别名)

select a.id,b.name from cq_userasa,cq_subject as b where a.id=b.typeId

 

条件查询:

=,>,<,>=,<=,!=,<>比较大小

and ,or,not多重条件

in确定集合

like/not like字符匹配

is null空值

between and(>= and <=)确定范围

select * from user where money>0andlevel>100

select * from user where id between 1and 4

select * from itemType where name='弯刀' or name='菜刀'

MySql的like语句中的通配符:百分号、下划线和escape

%代表任意多个字符

Sql代码

select * from user where usernamelike'%huxiao';

select * from user where usernamelike'huxiao%';

select * from user where usernamelike'%huxiao%';

_代表一个字符

Sql代码

select * from user where username like'_';

select * from user where usernamelike'huxia_';

select * from user where usernamelike'h_xiao';

如果我就真的要查%或者_,怎么办呢?使用escape,转义字符后面的%或_就不作为通配符了,注意前面没有转义字符的%和_仍然起通配符作用

Sql代码

select username from gg_user whereusernamelike '%xiao/_%' escape '/';

select username from gg_user where usernamelike'%xiao/%%' escape '/';

 

select * from user limit 100;

常用库函数:

count接列值计算个数,就是一共有几行,不计算空值,但可以计算到0。

max,min,avg,sum

select count(*) from itemtype

select max(level) from user;

分组查询:group by将查询结果按属性或属性列组合在行的方向上进行分组

select * from itemtype group by name

 

order by id desc(降序) asc(升序)

 

数据查询:当查询的时候涉及到两个以上的表就是连接查询。两种方式:1)表与表之间满足一定条件的列和行进行连接,from子句指明进行连接的表名,where子句指明连接的列名及其连接条件;

2)用join进行连接,用join关键字的时候,from子句中应该有关键词on与之对应(个人不是很习惯这种方式)

select * from cq_goods,cq_itemtype

where cq_goods.itemtype=cq_itemtype.id

 

数据更新:

插入数据:insert into <表名> [<列名1>,<列名2>…] values(<值>)

列名是可选的

列名的顺序没有要求,但是如果有指定列名的话values后面的值也要是相对应的

insert intocq_action(id,id_next,id_nextfail,type,data,param) values

 (500,0,505,503, 10000,'')

insert into cq_action values

 (500,0,505,503, 10000,'')

 

修改数据:

update <表名>

set <列名>=<表达式>[,<列名>=<表达式>…..]

[where<条件>]

update cq_user set level=lever+1

 

删除数据:

delete from  <表名>

[where<条件>]

delete from cq_user where id=1000

delete from cq_user

 

 

 --------------分界线--------下面的东西初级的用得少

数值类型:

整型:

BIT[(M)]

 位字段类型。M表示每个值的位数,范围为从1到64。如果M被省略, 默认为1

TINYINT[(M)] [UNSIGNED][ZEROFILL]

 很小的整数。

 SMALLINT[(M)] [UNSIGNED] [ZEROFILL]

 小的整数。

MEDIUMINT[(M)] [UNSIGNED][ZEROFILL]

 中等大小的整数。

 INT[(M)] [UNSIGNED] [ZEROFILL]

 普通大小的整数。

BIGINT[(M)] [UNSIGNED][ZEROFILL]

 大整数。

浮点型:

FLOAT[(M,D)] [UNSIGNED][ZEROFILL]

 小(单精度)浮点数。如果M和D没有写,那么单精度浮点数精确到大约7位小数位

DOUBLE[(M,D)] [UNSIGNED][ZEROFILL]

 普通大小(双精度)浮点数。如果M和D没有写,那么双精度浮点数精确到大约15位小数位

日期和时间类型:

DATETIME类型

 需要同时包含日期和时间信息的值时使用,以‘YYYY-MM-DDHH:MM:SS’格式检索和显示DATETIME值,支持的范围为'1000-01-0100:00:00'到'9999-12-31 23:59:59‘

DATE类型

 只需要日期值而不需要时间部分,用'YYYY-MM-DD'格式检索和显示DATE值

TIMESTAMP[(M)]

 时间戳。范围是'1970-01-0100:00:00'到2037年

TIME类型

 以'HH:MM:SS'格式显示TIME值,但允许使用字符串或数字为TIME列分配值。

YEAR[(2|4)]类型

 两位或四位格式的年。默认是四位格式。在四位格式中,允许的值是1901到2155和0000。在两位格式中,允许的值是70到69,表示从1970年到2069年。

 

字符类型:

—CHAR

◦是固定长度的,每个值占用相同的字节,不够的位数MySQL会在它的右边用空格字符补足。

—VARCHAR

◦是一种可变长度的类型,每个值占用其刚好的字节数再加上一个用来记录其长度的字节即L+1字节。

—BINARY

◦二进制字符串

—varbinary

◦可变长度的二进制字符串

 


  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值