Mysql独学
6233 ワード
一:基本命令
管理者でcommandネーミングmysql-u root-p起動を開く
quitまたはexitクローズ
select version(); バージョンの表示
select now(); 現在の時刻を表示
show databases;
すべてのデータベースを表示
commandコマンドクエリーipアドレス:ipconfig;リモート接続フォーマット:mysql-h ipアドレス-uユーザー名-p入力パスワードipアドレス接続が解決しません:mysq解決
mysqlコマンドの入力が間違っている場合、ctrl+cは中から退けません.idがプライマリキーの場合insertはnullで埋めます
二:データベース操作
三:表操作
四:データ操作
五:調査
六:関連
Python操作MySQL
Demo1
DemoV2
管理者でcommandネーミングmysql-u root-p起動を開く
quitまたはexitクローズ
select version(); バージョンの表示
select now(); 現在の時刻を表示
show databases;
すべてのデータベースを表示
commandコマンドクエリーipアドレス:ipconfig;リモート接続フォーマット:mysql-h ipアドレス-uユーザー名-p入力パスワードipアドレス接続が解決しません:mysq解決
mysqlコマンドの入力が間違っている場合、ctrl+cは中から退けません.idがプライマリキーの場合insertはnullで埋めます
二:データベース操作
1.
:creae database charset=utf8;
2.
:drop database ;
3.
: use
4.
: select database();
三:表操作
1.
:show tables;
2.
:create table ( )
:auto_increment primary key not null
: create table student(id int auto_increment primary key , name varchar(20) not null, age int not null, gender bit default 1, address varchar(20) , isDelete bit default 0);
3.
:drop table ;
4.
:desc ;
5.
: show create table ;
6.
:rename table to ;
7.
:alter table add|change|drop
四:データ操作
1.
1.
:insert into values(....)
: , , 0, ;
:insert into student values(0,"tom",19,1," ",0);
2.
:insert into ( 1, 2, 3,....) values( 1, 2, 3....)
:insert into student(name,age,address) values("DemoD_",20,"ShangHai");
3. :
:insert into values(....),(.....),......;
:insert into student values(0,"Alice",20,1,"ShanXi",0),(0,"DemoLi_",20,1,"JinCheng",0);
2.
:delete from where ;( delete from ; ;)
:delete from student where id=4;
3.
:update set 1= 1, 2= 2,... where
:update student set age=18 where id=3;
: ;
4.
:
:select * from ;
:select * from student;
五:調査
1.
:select * from
:
a.from ,
b.select , *
c. select , as ,
d. ,
:select name,age from student;
select name as a,age from student;
2.
select distinct
:
select distinct gender from student;
3.
a.
select * from where
b.
=
>
<
>=
<=
!=
:
id>1
select * from student where id>1;
age id>1
select age from student where id>1;
c.
and
or
not
:
id>1 and id<3
select * from student where id>1 and id<3;
id>1 and gender=1;
select * from student where id>1 and gender=1;
d.
like
%
_
:
select * from student where name like “ %”;
e.
in:
between.....and... :
:
id 1,3,5
select * from student where id in (1,3,5);
id 5-6
select * from student where id between 5 and 6;
f.
:null ""
: is null
:is not null
:
select * from student where address is null;
select * from student where address is not null;
g.
() > not > >
and or
4.
,
a.count*) ,*
b.max( )
c.min( )
d.sum( )
e.avg( )
:
select count(*) from student;
select count(id) from student;
id
select max(id) from student where gender=0;
age
select sum(age) from student where gender=1;
select avg(age) frmo student;
5.
,
, ,
, 。
: select 1, 2, ....from group by 1, 2, 3,..having 1, 2,
:
;
select gender,count(*) from student group by gender;
6.
:select * from order by 1 asc|desc, 2 asc|desc,....
:
a. 1 , ·1 , 2
b.
c.asc
d.desc
:
select * from student where isDelete=0 order by age asc;
select * from student where isDelete=0 order by age desc, order id desc;
7.
:select * from limit start ,count;
:start 0
:
select * from student limit 0,3;
select * from student limit 3,3;
select * from student where gender =1 limit 0,3;
六:関連
:
1.create table class(id int auto_increment primary key, name varchar(20) not null
,stuNum int not null);
2.create table students(id int auto_increment primary key,name varchar(20) not
null,gender bit default 1,classid int not null, foreign key(classid)references class(
id));
:
insert into class values(0,"python1",55),(0,"python2",33),(0,"python3",22),(0,"python3",11),(0,"python4",33);
insert into student values(0,"tom",1,1);
select students.name,class.name from class inner join students on class.id
=students.classid;
:
1. A inner join B;
A B
2. A left join B;
A B , A , Null
3. A right join B;
A B , B , Null
Python操作MySQL
Demo1
import pymysql
# 1:mysql ip
# 2:
# 3:
# 4:
db = pymsql.connect("localhost", "root", "", "mysql")
#
# cursor
cursor = db.cursor()
sql = "select version()"
# mysql
cursor.execute(sql)
#
data = cursor.fetchone()
print(data)
cursor.close()
db.close()
DemoV2
import pymysql
# 1:mysql ip
# 2:
# 3:
# 4:
db = pymysql.connect("localhost", "root", "", "mysql")
# cursor
cursor = db.cursor()
# sql = "select version()"
sql = "show databases;"
# mysql
cursor.execute(sql)
#
data = cursor.fetchone()
print(data)
# ----------------------------------------------
sql = "use student"
cursor.execute(sql)
sql = "show tables"
cursor.execute(sql)
data = cursor.fetchone()
print(data)
sql = "select * from student;"
cursor.execute(sql)
data = cursor.fetchone()
print(data)
# -------------------------------------------
cursor.close()
db.close()