mysql_入门使用

登录

mysql -u mysql -p  
或者 
mysql -u mysql -p bank  # bank是要用的库

执行sql脚本

source /home/mysql/LearningSQLExample.sql;

查看当前时间

select now();select now() from dual;  # 为了与oracle兼容

常用数据类型

boolean
char
varchar
int
float(p, s)  # p, s用来指定精度, p指定允许的数字总位数, s指定小数点右边允许的位数
double(p, s) 
decimal(p, s)  # 没有浮点数的精度问题 p最大值65, s最大值30
date  # YYYY-MM-DD
datetime  # YYYY-MM-DD HH:MI:SS
timestamp  # YYYY-MM-DD HH:MI:SS  可以由mysql自动赋值, 最大值2037-12-31
year  # YYYY  1901-2155
time  # 可以存储消耗的时间  HHH:MI:SS, 范围是 -838:59:59 ~ 838:59:59
enum

建表语法示例

create table person(
    person_id int unsigned auto_increment,  # 自增
    name varchar(20),
    gender enum('M', 'F'),
    brith_date date,
    constraint pk_person primary key (person_id)  # 主键
);


create table food(
    person_id int unsigned,
    food varchar(200),
    constraint pk_food primary key (person_id, food),
    constraint fk_food foreign key (person_id) references person (person_id)  # 外键, 当person表中没有某个person_id时, food表中也不能有
);

alter

alter table person MODIFY person_id int unsigend auto_increment;
这句话实际上是重新定义了person表的person_id列

插入日期(date)格式数据

insert into person (name, gender, brith_date) values('abc', 'M', '2018-1-1');
insert into person (name, gender, brith_date) values('cde', 'M', str_to_date('2018-1-1', '%Y-%c-%d'));  # %Y年, %c月, %d日, %H小时, %i分钟, %s秒

distinct

数据库执行distinct的时候要对结果集先排序, 这对大的结果集来说是相当耗时的

group by

根据列值对数据进行分组

having

对分组数据进行过滤

union

连接多个数据集, 对结果排序并去除重复项

union all

连接多个数据集, 保留重复项

intersect, intersect all

交集, mysql中可能未实现此操作

except, except all

差集, mysql中可能未实现此操作

猜你喜欢

转载自blog.csdn.net/flyDeDog/article/details/81529902