学习数据库

linux:

mysql

数据库命令:

  mysql -uroot -p  输入密码 登录数据库

  查看数据库:show databases;

    删除数据库:drop database 表名;

     查看当前使用的数据库:select database();

  创建数据库:create database 表名 charset=utf8;(不加utf8后面输入文字报错)

  切换数据库:use 数据库名;

数据表操作:

  查看表:show tables;

  创建表:create table students(

  id int auto_increment primary key not null.

  name varchar(10),

  gender varchar(4));

  查看表结构:desc students;

  删除表:drop table 表名;

  alter table 表名 add|change|drop 列;

数据查找:

  查询:select * from 表名 where .....

  插入:insert into 表名 value()

  修改:update 表名 set 字段=值 (where....)

inner join 都匹配上的出现

left join 以左表为准

right join 以右表为准

group by...having...

where....

limit start,count

ordet by ...asc|desc

数据库中的自关联

导入数据库 :进入数据库所在目录 ,source ***.sql;

将表封装可以变成视图:create view v_1 as.......执行完后可以像表一样使用

自关联表物理上是一张表逻辑上是多张表

python中导入mysql模块

python2 :sudo apt-get install python-mysqldb

导入 import mysqldb

python3:http://blog.csdn.net/taodayenidaye/article/details/78570652

# coding=utf-8

from *pymysql impot  *

try:

  conn=connect(

    host = 'localhost',
    port = 3306,
    user = 'root',
    passwd = 'root',
    db = 'python',
    charset = 'utf8'

  )

  cursor1=conn,cursor()

  #填写sql语句

  sql='...'

  conn.commit()

  cursor1.close()

  conn.close()

except Exception as e:

  print(e)

查询删除增加基本无差别

原文地址:https://www.cnblogs.com/coco-shi/p/8604502.html