SQL语句大体可分为四种:
DDL—数据定义语言(Create,Alter,Drop,DECLARE):
DDL比DML要多,主要的命令有CREATE、ALTER、DROP等,DDL主要是用在定义或改变表(TABLE)的结构,数据类型,表之间的链接和约束等初始化工作上,他们大多在建立表时使用
DML—数据操纵语言(Select,Delete,Update,Insert):
它们是SELECT、UPDATE、INSERT、DELETE,就象它的名字一样,这4条命令是用来对数据库里的数据进行操作的语言
DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)
是数据库控制功能。是用来设置或更改数据库用户或角色权限的语句,包括(grant,deny,revoke等)语句。在默认状态下,只有sysadmin,dbcreator,db_owner或db_securityadmin等人员才有权力执行DCL
DQL-数据查询语言(select):
顾名思义,是查询数据表中的数据的语句,命令只有Select,通常用于数据的查询
SQL语言的一些注意事项
1.每条SQL语句结束时要以;做为结束符.(除了use命令)
2.SQL语句的关键字不区分大小写(除了库名字和表名字)
3.在查询数据库信息或者表信息时,可以以\G做为结束符,表示以文本模式输出
4.当你不需要一条语句输出的结果以\c结束,不可以使用ctrl+c,否则登出mysql.
5.我们可以在命令行执行sql语句,要通过mysql -e参数
mysql -e "show databases \G" 显示到shell上
6.如果需要获取SQL语句的帮助可以用help命令
如:help create
如果需要进一步获取帮助,可以继续使用help命令
如:help create database
SQL数据类型:
数值类型 unsigned
整数类型 字节 范围(有符号) 范围(无符号) 用途
TINYINT 1字节 (-128,127) (0,255) 小整数值
SMALLINT 2字节 (-32 768,32 767) (0,65 535) 大整数值
MEDIUMINT 3字节 (-8 388 608,8 388 607) (0,16 777 215) 大整数值
INT或INTEGER 4字节 (-2 147 483 648,2 147 483 647) (0,4 294 967 295) 大整数值
BIGINT 8字节 (-9 233 372 036 854 775 808,9 223 372 036 854 775 807) (0,18 446 744 073 709 551 615) 极大整数值
FLOAT 4字节 (-3.402 823 466 E+38,1.175 494 351 E-38),0,(1.175 494 351 E-38,3.402 823 466 351 E+38) 0,(1.175 494 351 E-38,3.402 823 466 E+38) 单精度浮点数值 (7个有效位)
DOUBLE 8字节 (1.797 693 134 862 315 7 E+308,2.225 073 858 507 201 4 E-308),0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) 0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) 双精度浮点数值 (15个有效位)
DECIMAL 不存在精度损失,常用于银行帐目计算。(28个有效位)
字符串类型
create table t1(id int(6),name char(50)); 定长
create table t1(id int(6),name varchar(50)); 变长
日期时间类型
日期时间类型 | 占用空间 | 日期格式 | 最小值 | 最大值 | 零值表示 |
DATETIME | 8bytes | YYYY-MM-DD HH:MM:SS | 1000-01-01 00:00:00 | 9999-12-31 23:59:59 | 0000-00-00 00:00:00 |
TIMESTAMP | 4bytes | YYYY-MM-DD HH:MM:SS | 19700101080001 | 2038 年的某个时刻 | 00000000000000 |
DATE | 4bytes | YYYY-MM-DD | 1000-01-01 | 9999-12-31 | 0000-00-00 |
TIME | 3bytes | HH:MM:SS | -838:59:59 |
838:59:59 | 00:00:00 |
YEAR | 1bytes | YYYY | 1901 | 2155 | 0000 |
ENUM和SET类型
ENUM 单项选择 预先设定两个值非此即彼,例如:性别
SET 多项选择,预先设定多个值可以多选 , 例如:爱好
修饰符(约束)
无符号 unsigned
用0补齐 zerofill
not null约束:不能为空
DEFAULT约束:默认值
1.DDL数据库定义语句
建立数据库以及查询
create database db;
create database db CHARACTER SET = 'utf8'; #设置字符格式
show databases;
show create database db;
alter database db CHARACTER SET = 'latin1';
修改库名只需要改数据库目录名称
如果数据库没有内容直接删除重新创建就行可 drop database db
或者直接去数据目录修改数据库的名字
cd /usr/local/mysql/data/
mv db mydb
索引:
什么情况下需要建立索引
1、较频繁地作为查询条件的字段
2、唯一性太差的字段不适合建立索引
什么是唯一性太差的字段。如状态字段、类型字段。那些只存储固定几个值的字段,例如用户登录状态、消息的status等。
3、更新太频繁地字段不适合创建索引
4、不会出现在where条件中的字段不该建立索引
索引建立
create table test1(id int not null ,name char(10),index(id));
mysql> desc test1;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | NO | MUL | NULL | |
| name | char(10) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
向已有表添加索引
create table test2(id int not null ,name char(10));
mysql> desc test2;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | char(10) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> create index id on test2(id);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc test2;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | NO | MUL | NULL | |
| name | char(10) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> alter table test2 add index(name);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc test2;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | NO | MUL | NULL | |
| name | char(10) | YES | MUL | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
删除索引
drop index id on test2;
查询索引
show index from test1;
UNIQUE索引(允许空值)
mysql> create table test3(id int ,name char(10),unique(id));
Query OK, 0 rows affected (0.00 sec)
mysql> desc test3;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | UNI | NULL | |
| name | char(10) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> insert into test3 values(null,'zzz');
Query OK, 1 row affected (0.00 sec)
mysql> select *from test3
-> ;
+------+------+
| id | name |
+------+------+
| NULL | zzz |
+------+------+
1 row in set (0.00 sec)
show index from test3\G
drop index id on test3;
create unique index id on test3(id);
PRIMARY KEY(主键约束 值唯一 uniq和not null的结合)
mysql> create table test4(id int,name char(10),primary key(id));
Query OK, 0 rows affected (0.00 sec)
mysql> desc test4
-> ;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | char(10) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> insert into test4 values(1,'tong');
Query OK, 1 row affected (0.00 sec)
mysql> insert into test4 values(1,'tong');
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
mysql>
删除主键和向已有的表中创建主键
mysql> alter table test4 drop primary key;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> alter table test4 add primary key(id);
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
在多个列上建立主键
mysql> create table test5(id int,name char(10),primary key(id,name));
Query OK, 0 rows affected (0.00 sec)
mysql> desc test5;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | char(10) | NO | PRI | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> insert into test5 values(1,'tong');
Query OK, 1 row affected (0.00 sec)
mysql> insert into test5 values(1,'tom');
Query OK, 1 row affected (0.00 sec)
mysql> select * from test5;
+----+------+
| id | name |
+----+------+
| 1 | tom |
| 1 | tong |
+----+------+
2 rows in set (0.00 sec)
create table t19(id int primary key auto_increment, name char(10)); 自动增长
全本索引
create table t22(id int,name char(10),fulltext(name));
1).MySQL中的全文索引是FultLeXT类型的索引。
2).全文索引只能用于InnoDB或MyISAM表,只能为CHAR、VARCHAR、TEXT列创建。
3).在MySQL 5.7.6中,MySQL提供了支持中文、日文和韩文(CJK)的内置全文ngram解析器,以及用于日文的可安装MeCab全文解析器插件
4).当创建表时,可以在CREATE TABLE语句中给出FULLTEXT索引定义,或者稍后使用ALTER TABLE或CREATE INDEX添加该定义。
5).对于大型数据集,将数据加载到没有FULLTEXT索引的表中然后创建索引要比将数据加载到具有现有FULLTEXT索引的表中快得多。
约束
1.添加约束
create table test7 ( id int primary key auto_increment, name char(20) not null, money float(10,2) default 0, gender enum('M','F') not null, hobby set('a','b','c') not null default 'a', qq char(15) unique, email char(50) unique, jointime datetime);
2.删除掉所有的约束
alter table test7 modify id int; 删除auto_increment
alter table test7 drop primary key; 删除primary key
alter table test7 modify id int; 删除 not null
alter table test7 modify name char(10);
alter table test7 modify money float(10,2);
alter table test7 modify gender enum('M','F');
alter table test7 modify hobby set('a','b','c');
drop index qq on test1;
drop index email on test1;
2.修改表结构
alter table test7 add phone char(11); 添加列
alter table test7 drop phone; 删除列 注意:会删除数据
alter table test7 add phone char(11) after name; 添加列指定位置
alter table test7 add phone char(11) first; 添加列到第一列
alter table test7 modify phone char(11) after name; 修改某一列位置
alter table test7 change phone myphone char(11); 修改列的名字
rename table test7 to myinfo; 修改表名
3.在添加约束
alter table myinfo add primary key(id);
alter table myinfo modify id int auto_increment;
alter table myinfo modify name char(20) not null;
show tables;
show create table t1;
create table user(
id int primary key auto_increment,
name varchar(30) not null,
money float(10,2),
gender enum('M','F') not null,
hobby set('a','b','c','d'),
email varchar(50) unique,
qq char(15) unique,
idcard char(18) unique,
jointime datetime not null,
index(name));
2.DML 数据库操作语句
insert
mysql> INSERT INTO members ( member_id,fname,lname,tel,email) VALUES ( NULL,'john','Doe','1234567','[email protected]');
mysql> INSERT INTO members VALUES ( NULL,'kyo','oyk','7654321','[email protected]');
mysql> INSERT INTO members (fname,lname,email,tel,member_id) VALUES ('bob','kk','[email protected]','22334455',NULL);
update
mysql> UPDATE members SET email = '[email protected]' WHERE member_id = 3;
mysql> UPDATE members SET email = '[email protected]',lname = 'ho' WHERE member_id = 2;
delete
mysql> DELETE FROM members;
mysql> DELETE FROM members WHERE member_id = 1;
sql语句使用
连接数据库
mysql -u root -p123 -h localhost
查看服务器状态
show staus;
显示所有库名
show databases;
使用数据库
use db;
显示当前数据库中的所有表
show tables;
查看表结构
desc tables;
select查询语句
select name from tables; 从表中查询指定列
select id,name,sal from tables; 指定多个列名
select * from tables;查询所有的列
select distinct id from tables; 去掉重复行
select name from tables limit 5; 显示前5行
select name from tables limit 5,5;显示从第5行开始的后5行即6-10行
select name from db.t1;没有使用use进入db库时查询db库的t1表
select t1.name from db.t1; 指定库的表 指定表的列
排序检索语句
select id,name from t1 order by id; 按id排序
select id,name from t1 order by id,name;先按id排序id相同在按name排序
select id,name from t1 order by id desc; 按id反向排序
select id,name from t1 order by id desc,name; 先按id反向排序再按名字排序
select id,name,sal from t1 order by sal desc limit 1;查找工资最高的人
where子句
select id,name,sal from t1 where name='tom'; 查找tom的信息
where 子句的操作符
= 等于
<> 不等于
!= 不等于
< 小于
<= 小于等于
> 大于
>= 大于等于
between 5 and 10 在两个值之间
is null 空值
is not null 非空值
select id,name from t1 where id>5 and name='tom'; and操作符表示两个条件都要满足 与操作
select id,name from t1 where id=10 or name='tom';or操作符表示满足任意条件 或操作
select id,name,sal from t1 where id=10 or id=20 and sal > 5000; id为10的 或者id为20并且薪水大于5000的;and优先执行
select id,name,sal from t1 where (id=10 or id=20) and sal > 5000;id为10或者20 并且薪水大于5000的
select id,name,sal from t1 where id in (10,20,30);id在 10 20 30 中的记录
这条语句用or可以做到相同的结果,那in的好处
1.in的语法更加直观
2.in的计算次序更容易管理(操作符少)
3.in 一般比or执行的更快
4.in的最大优点可以包含其他子句 or不行
通配符%匹配多个字符_匹配一个字符
select id,name from t1 where name like 'jer%';模糊查询名字为jer开头的记录
select id,name from t1 where name like 'j%y'; 匹配j开头y结尾的
select id,name from t1 where name like '_err%' 匹配e前边有一个字符的记录
原则:
尽量少使用通配符,如果其他操作符能做到就不要使用通配符
在确实需要通配符时,尽量不要使用%erry 这种用法搜索起来会更慢
至于使用位置,使用错了得不到想要的结果
正则表达式的使用regexp
select id,name from t1 where name regexp 'je*';调用正则匹配je开头
select id,name from t1 where name regexp 'y$' ;
语句的拼接
select concat(id ,'(',name,')') from t1 将id和name 拼接为1列 oracle用||
select concat(id ,'(',name,')') all_name from t1 别名也可以使用as
函数使用
select upper(name) as new_name from t1; 将名字转换为大写 lower 小写
group by 分组 必须在where之后 分组前过滤 having 可以分组后过滤
sum max min avg count year month day hour minute second