Python全栈开发之mysql

MySQL忘记密码登录

mysqld_safe --user=mysql --skip-grant-tables --skip-networking &

mysql -u root

create database mysql 创建数据库

use mysql 进入mysql数据库

show tables 查看所有的数据表

MariaDB [mysql]> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| slow_log                  |
| student                   |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| user                      |
+---------------------------+
27 rows in set (0.00 sec)

show databases 查看可执行的库

drop mysql 删除数据库

create database 123db charset utf8 创建数据库表123db

show create database 123db查看表

drop create database 123db删除表

创建表student1

MariaDB [mysql]> create table student1(
    -> id int auto_increment,
    -> name char(32) not null,
    -> age int not null,
    -> register_date date not null,
    -> primary key(id));
Query OK, 0 rows affected (0.02 sec)

添加数据

insert into student1 (name,age,register_date) values("zhangshan",3,"2016-10-01");

select * from student1查看列表·

 

desc student1查看表结构

select * from student1 limit 2; 查前两条数据

select * from student1 limit 2 offset 2; 查第2条的后两条数据

select * from student1 where id>3;查看id大于3的数据

select * from student1 where register_date like "2016-04%";模糊查询时间

update student1 set name = "Chenssss",age = 33 where id = 4; 修改id为4的name和age

delete from student1 where name = "chenssss";删除name的值

select *from student1 order by id asc;升序 系统默认升序

select *from student1 order by id desc;降序

select name ,count(*) from student1 group by name; 分组

select name ,count(*)as stu_num from student1 group by name; 改name的名字

select name ,sum(age) from student1 group by name;按名字求年龄总和

select coalesce( name,"Total") ,sum(age) from student1 group by name with rolup;年龄总和

alter table student1 add sex enum("M","F");插入字段sex

alter table student1 drop age;删除字段age

alter table student1 modify sex enum("M","F") not null;数据类型不能为空

alter table student1 change sex gender char(32) not null default "X";修改字段名为gender和数据类型为char(32)

 创建表 study_record

CREATE TABLE `study_record` (
`id` int(11) NOT NULL,
`day` int(11) NOT NULL,
`status` char(32) NOT NULL,
`stu_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_class_key` (`stu_id`),
CONSTRAINT `fk_student_key` FOREIGN KEY (`stu_id`) REFERENCES `student1` (`id`)
)

show create table study_record;查看表的创建纪录

创建A表

创建B表

select * from A inner join B on A.a=B.b;或select A.*,B.* from A,B where A.a=B.b;取交集

select * from A left join B on A.a=B.b;求差集

select * from A left join B on A.a=B.b union select * from A right join B on A.a=B.b;取并集

begin;开始
rollback;回滚
commit;提交后就不能回滚


show index from student1;查看索引

create index index_name on student1(name(32));创建索引

 

猜你喜欢

转载自www.cnblogs.com/xiaoqianbook/p/9062243.html