链接数据库
在pycharm中打开MySQL,点击右边的database,点击+,数据源选择MySQL,输入用户名:root 密码是自己设置好的,如是出现[08001] Could not create connection to database server. Attempted reconnect。。。这种错误,则是时区问题,在密码框下有一个url框,在后面输入:serverTimezone=UTC!就可以链接成功了!有的是输入:
jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
这个方法我并不是很清楚怎么写才对,所以选择下面的方法来修改时区,
修改时区
上面的方法对有的来说并不行,所以在cmd中修改时区,首先进入到MySQL中,输入:show variables like ‘%time_zone%’;
会显示:±-----------------±-------+
| Variable_name | Value |
±-----------------±-------+
| system_time_zone | EST |
| time_zone | SYSTEM |
±-----------------±-------+
2 rows in set (0.00 sec)
mysql默认使用的SYSTEM时区,即EST时区,查询相关资料可知,EST时区要比北京时间(东八区)慢13个小时。
再次输入(全局时区):set global time_zone = ‘+8:00’; 显示ok
在输入(东八区):set time_zone = ‘+8:00’;
刷新:flush privileges;
查看:show variables like ‘%time_zone%’;
永久的修改是修改my.cnf,在mysqld下边的配置中添加一行:default-time_zone = ‘+8:00’
然后重启mysql!
数据库
用上面的修改了时区后,链接成功:
输入:show databases; 成功显示数据库。(运行是 ctrl + enter)
创建表结构
-
创建的语法为 :
create table 【表名】(
【字段名】 【字段类型】 【字段约束】,
【字段名】 【字段类型】 【字段约束】,
… …
); -
案例
选择数据库:use db1;
创建表单:create table user1( username varchar(16), age tinyint );
结果显示:
-
常用的字段约束
案例
在MySQL数据库中创建表格:
create table user2(
id int primary key auto_increment,
username varchar(16) not null unique ,
password varchar(16)not null ,
gender tinyint default 0,
account decimal(12,2) default 0,
vip boolean default 0
)
显示结果:
增删改查操作
创建表单3:
use db1;
create table user3(
id int primary key auto_increment,
username varchar(16) not null unique,
password varchar(16) not null,
createDatetime date
);
insert into user3 (username,password,createDatetime) values
("张伟","pass1","2019-07-11"),
("王伟","pass2","2019-07-11"),
("王芳","pass3","2019-07-12"),
("李伟","pass4","2019-07-11"),
("王秀英","pass5","2019-07-12"),
("李秀英","pass6","2019-07-11"),
("李娜","pass7","2019-07-10"),
("张秀英","pass8","2019-07-10"),
("刘伟","pass9","2019-07-12"),
("张敏","pass10","2019-07-10");
条件查询
- 查看表内所有的数据
select * from 【表名】;
*代表所有的字段 - 查看指定字段
select 【字段1】,【字段2】from 【表名】; - 比较条件查询
select *from 【表名】 where 【条件】; - 模糊条件查询:
select *from 【表名】 where 【字段】like "表达式“; - 范围查询
select * from 【表名】where 【字段】in (【可能1】,【可能2】…); - 逻辑运算符:
and 和 or 或 - 分组
格式: select *from 【表名】where 【条件】group by 【字段1】,【字段2】,…;
说明:按照哪个字段分组,哪个字段就不会重复;group by 要写在where 的后面。
对分组的条件:
select * from 【表名】group by 【字段1】,【字段2】…having 【条件】;
说明:where 是对select * from的结果进行筛选;having 是对group by 的最后结果进行筛选; - 排序:
格式:select * from 【表名】where 【条件】group by 【字段1】,【字段2】…having 【条件】order by 【字段1】asc|desc,【字段2】asc|desc;
说明: asc:升序(默认) desc:降序
先按【字段1】进行排序,如果【字段1】相同,按【字段2】排序 - 分页
格式:select * from 【表名】where 【条件】group by 【字段1】,【字段2】…having 【条件】order by 【字段1】asc|desc,【字段2】asc|desc limit 【起始索引】,【数据条数】;
比较条件查询:
模糊条件查询:
例如表单3中:
查询字段和表内所有数据
select * from user3;
select username,password from user3;```
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200306143012630.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2RyZW1jbA==,size_16,color_FFFFFF,t_70)
**比较条件查询:**
```python
select password from user3 where username="王伟";
select * from user3 where createDatetime is not null;
select createDatetime from user3 where id >=5;
模糊条件查询:
select * from user3 where username like "%秀%";
select * from user3 where username like "王_";
范围查询:
select * from user3 where username in ("王伟","李娜");
逻辑运算:输出结果与上一代码一样
select * from user3 where username="王伟" or username="李娜";
分组:
select * from user3 group by createDatetime;
select * from user3;
select count(*)from user3 group by createDatetime;
select count(*),createDatetime from user3 group by createDatetime;
select count(*),createDatetime from user3 group by createDatetime having count(*)>3;
分页:
select * from user3 order by createDatetime desc,id asc;