Mysql是最流行的关系型数据库管理系统(RDBMS)。
一、基本命令
win启动:net start mysql
linux启动:service mysql start
登录:mysql -u root -p
退出:exit
创建用户账号:create user user_name identified by 'user_password';
授权:grant select insert on db_name.table_name to user_name;
二、基本概念
DDL:数据定义语言(create/drop/alter)
DML:数据操作语言(insert/delete/update/truncate)
DQL:数据查询语言(select/show)
三、数据库管理
创建数据库:create database day01 default character set utf8;
删除数据库:drop database day01;
选择数据库:use day01;
四、数据类型
MySQL支持多种类型,大致可以分为三类:数值、日期/时间和字符串(字符)类型。
1.数值类型
类型 | 大小 | 范围(有符号) | 范围(无符号) | 用途 |
---|---|---|---|---|
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) | 单精度 浮点数值 |
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) | 双精度 浮点数值 |
DECIMAL | 对DECIMAL(M,D) ,如果M>D,为M+2否则为D+2 | 依赖于M和D的值 | 依赖于M和D的值 | 小数值 |
2.日期和时间类型
类型 | 大小 (字节) | 范围 | 格式 | 用途 |
---|---|---|---|---|
DATE | 3 | 1000-01-01/9999-12-31 | YYYY-MM-DD | 日期值 |
TIME | 3 | '-838:59:59'/'838:59:59' | HH:MM:SS | 时间值或持续时间 |
YEAR | 1 | 1901/2155 | YYYY | 年份值 |
DATETIME | 8 | 1000-01-01 00:00:00/9999-12-31 23:59:59 | YYYY-MM-DD HH:MM:SS | 混合日期和时间值 |
TIMESTAMP | 4 | 1970-01-01 00:00:00/2037 年某时 | YYYYMMDD HHMMSS | 混合日期和时间值,时间戳 |
3.字符串类型
类型 | 大小 | 用途 |
---|---|---|
CHAR | 0-255字节 | 定长字符串 |
VARCHAR | 0-65535 字节 | 变长字符串 |
TINYBLOB | 0-255字节 | 不超过 255 个字符的二进制字符串 |
TINYTEXT | 0-255字节 | 短文本字符串 |
BLOB | 0-65 535字节 | 二进制形式的长文本数据 |
TEXT | 0-65 535字节 | 长文本数据 |
MEDIUMBLOB | 0-16 777 215字节 | 二进制形式的中等长度文本数据 |
MEDIUMTEXT | 0-16 777 215字节 | 中等长度文本数据 |
LONGBLOB | 0-4 294 967 295字节 | 二进制形式的极大文本数据 |
LONGTEXT | 0-4 294 967 295字节 | 极大文本数据 |
五、表管理
创建表:create table if not exists table_name(column_name column_type);
删除表:drop table table_name;
插入数据:
INSERT INTO table_name ( field1, field2,...fieldN )
VALUES
( value1, value2,...valueN );
分页查询:
select column_name, column_name from table_name where clause limit [index],[count];
or select column_name, column_name from table_name where clause limit [count] offset [index];
where子句:
SELECT field1, field2,...fieldN FROM table_name1, table_name2...
[WHERE condition1 [AND [OR]] condition2.....
update子句:
UPDATE table_name SET field1=new-value1, field2=new-value2
[WHERE Clause]
delete语句:delete from table_name [where clause];
like子句:
SELECT field1, field2,...fieldN FROM table_name1, table_name2...
WHERE field1 LIKE condition1 [AND [OR]] filed2 = 'somevalue'
排序order by子句:
SELECT field1, field2,...fieldN FROM table_name1, table_name2...
ORDER BY field1, [field2...] [ASC [DESC]]
总结示例:
use day01;
show tables;
create table if not exists student(sid int not null auto_increment,
sname varchar(5) not null,
smark varchar(6) not null,
sbirthday date,
primary key(sid));
alter table student add column sgrade varchar(3);
alter table student modify column smark varchar(3);
alter table student change column smark mediumint;
insert into student (sid, sname) values(002, "lirong");
delete from student where sid=003;
update student set mark=2 where sid=002;
select sname, sid from student;
# alter table student rename to teacher;
select * from student order by sid ASC/DESC;
select * from student where sid<>002 limit 1,3;
/*sid在002到004之间*/
select * from student where sid between 002 and 004;
select * from student where sgrade is null;
/*选择sid=001或002并且mark大于1*/
select * from student where (sid=001 or sid=002) and mark>1;
/*选择出sid在(001,002,003)中*/
select * from student where sid in (001,002,003);
select * from student where sname like '_eace%'; #%匹配多个字符,_匹配单个字符