Database operations:
1, Windows how to use CMD into the MySQL database:
1 Windows + R -> type cmd Run
2 C: \ Users \ ***> D: # installed in D drive
D: \> cd D: \ MySQL \ mysql \ bin # switch to the D bin directory disk installed MySQL
3 D: \ MySQL \ mysql \ bin> mysql -hlocalhost -uroot -p # start MySQL database links
Enter password: ********** # password
4 mysql> # successful link
5 mysql> show databases; # display all of the database (must end with a semicolon)
6 mysql> create database demo; # create a new database demo
7 mysql> exit; # exit database
2, other commands:
1, select now (); # display the current time database
2, select version (); # display the current version of the database
3, important commands:
1, create database python04; # create a new database python04
2, create database python04 charset = utf8; # utf8 specified encoding said default Latin
3, show create database python04; # display database information created
4, drop database python04; # delete python04 database
Data table operations:
1, select database (); # query the database currently in use
2, use python04; # use python04 database
3, show tables; # display all tables in the current database
4, create a table:
- auto_increment means the automatic growth
- not null representation can not be empty
- primary key primary key representation
- default defaults
- create table Data table name (field type constraint [field type constraint]);
create table xxxx (id int, name varchar (30)); # Create a data table
create table yyyy(id int primary key not null auto_increment, name varchar(30));
5, desc xxxx; # View table structure
Students create a table (id, name, age, high, gender, cls_id)
create table students(
id int unsigned not null auto_increment primary key, # (unsigned unsigned range 0 to 255)
name varchar(30),
age tinyint unsigned default 0,
high decimal(5,2),
gender enum ( "male", "female", "confidential") default "confidential" not null,
cls_id int unsigned
);
desc sutdents; # View student table
Insert data:
insert into students values(0,"二狗",24,178.88,"男",0);
delete data:
delete from students where id=1;
View data:
select * from students;
View the table create statement:
show creat table students;
Deletions data change search:
- modify the table - Add Field:
- alter table column name table add type;
alter table students add birthday datetime;
- Modify the table - modify the field: Do not rename edition
- alter table modify table column names and types of constraints;
alter table students modify birthday date;
- Modify the table - modify the field: Rename edition
- alter table table name change, formerly known as the new name and type of constraint;
alter table students change birthday birth date default "1997-01-02";
- Modify the table - Delete field
- alter table drop table column name;
alter table students drop high;
- Delete table
-- drop table 表名;
CRUD:
-- 增加
-- 全列插入
-- insert [into] 表名 values(...)
-- 主键字段 可以用 0 null default 来占位
-- 向calsses表中插入一个班级
insert into classes values(0, "菜鸟班");
-- 向students表插入一个学生信息
insert into students values(0, "小李飞刀", 20, "女", 1, "1990-1-1");
insert into students values(null, "小李飞刀", 20, "女", 1, "1990-1-1");
insert into students values(deauft, "小李飞刀", 20, "女", 1, "1990-1-1");
-- 部分插入
-- insert into 表名(列1,...) values(值1,...);
insert into students (name, gender) values ("小乔", "女");
-- 多行插入
insert into students (name, gender) values ("大乔", "女"),("貂蝉", "女");
insert into students values(0, "西施", 20, "女",1,"1990-2-2"),(null, "王昭君", 21, "女",1,"1990-3-4");
-- 修改
-- update 表名 set 列1=值1,列2=值2... where 条件;
update students set gender=1; # 全部都该了
update students set gender=1 where name="小李飞刀"; # 只要是name是小李飞刀的,都改了
update students set gender=1 where id=3; # 只对id=3的进行了修改
-- 查询的基本使用
-- 查询所有列
-- select * from 表名;
select * from students;
-- 定条件查询
select * from students name="小李飞刀"; # 查询name为小李飞刀的所有信息
select * from students id>3; # 查询id>3的所有信息
-- 查询指定列
-- select 列1,列2,...,from 表名;
select name,gender from students;
-- 可以使用as为列或表指定别名
-- select 字段[as 别名],字段[as 别名] from 数据表 where ...;
select name as 姓名,gender as 性别 from students;
-- 字段的顺序
select gender as 性别,name as 姓名 from students;
-- 删除
-- 物理删除
-- delete from 表名 where 条件
delete from students;
delete from students where name="小李飞刀";
-- 逻辑删除
-- 用一个字段来表示 这条信息是否已经不能再使用了
-- 给students表添加一个is_delete字段 bit 类型
alter table students add is_delete bit default 0;
update students set is_delete=1 where id=6;
注:1. # 表示注释(python用习惯了),MySQL中的注释用的是 --
2. 看的黑马培训机构的python视频,根据视频的内容进行整理的