登陸數據庫html
mysql -uroot -p123
顯示數據庫node
show databases;
默認數據庫:
mysql - 用戶權限相關數據
test - 用於用戶測試數據
information_schema - MySQL自己架構相關數據mysql
使用數據庫,顯示該數據庫的tablesql
use db;
show tables;
創建表數據庫
create table 表名(
列名 類型 是否能夠爲空,
列名 類型 是否能夠爲空
)ENGINE=InnoDB DEFAULT CHARSET=utf8
刪表編程
drop table 表名
清空表session
delete from 表名 truncate table 表名
修改表架構
添加列:alter table 表名 add 列名 類型 刪除列:alter table 表名 drop column 列名 修改列: alter table 表名 modify column 列名 類型; -- 類型 alter table 表名 change 原列名 新列名 類型; -- 列名,類型 添加主鍵: alter table 表名 add primary key(列名); 刪除主鍵: alter table 表名 drop primary key; alter table 表名 modify 列名 int, drop primary key; 添加外鍵:alter table 從表 add constraint 外鍵名稱(形如:FK_從表_主表) foreign key 從表(外鍵字段) references 主表(主鍵字段); 刪除外鍵:alter table 表名 drop foreign key 外鍵名稱 修改默認值:ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000; 刪除默認值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;
一、增框架
insert into 表 (列名,列名...) values (值,值,值...) insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...) insert into 表 (列名,列名...) select (列名,列名...) from 表
二、刪編程語言
delete from 表 delete from 表 where id=1 and name='alex'
三、改
update 表 set name = 'alex' where id>1
四、查
select * from 表 select * from 表 where id > 1 select nid,name,gender as gg from 表 where id > 1
五、其餘
a、條件 select * from 表 where id > 1 and name != 'alex' and num = 12; select * from 表 where id between 5 and 16; select * from 表 where id in (11,22,33) select * from 表 where id not in (11,22,33) select * from 表 where id in (select nid from 表) b、通配符 select * from 表 where name like 'ale%' - ale開頭的全部(多個字符串) select * from 表 where name like 'ale_' - ale開頭的全部(一個字符) c、限制 select * from 表 limit 5; - 前5行 select * from 表 limit 4,5; - 從第4行開始的5行 select * from 表 limit 5 offset 4 - 從第4行開始的5行 d、排序 select * from 表 order by 列 asc - 根據 「列」 從小到大排列 select * from 表 order by 列 desc - 根據 「列」 從大到小排列 select * from 表 order by 列1 desc,列2 asc - 根據 「列1」 從大到小排列,若是相同則按列2從小到大排序 e、分組 select num from 表 group by num select num,nid from 表 group by num,nid select num,nid from 表 where nid > 10 group by num,nid order nid desc select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid select num from 表 group by num having max(id) > 10 特別的:group by 必須在where以後,order by以前 f、連表 無對應關係則不顯示 select A.num, A.name, B.name from A,B Where A.nid = B.nid 無對應關係則不顯示 select A.num, A.name, B.name from A inner join B on A.nid = B.nid A表全部顯示,若是B中無對應關係,則值爲null select A.num, A.name, B.name from A left join B on A.nid = B.nid B表全部顯示,若是B中無對應關係,則值爲null select A.num, A.name, B.name from A right join B on A.nid = B.nid g、組合 組合,自動處理重合 select nickname from A union select name from B 組合,不處理重合 select nickname from A union all select name from B
SQLAlchemy是Python編程語言下的一款ORM框架,該框架創建在數據庫API之上,使用關係對象映射進行數據庫操做,簡言之即是:將對象轉換成SQL,而後使用數據API執行SQL並獲取執行結果。
一個簡單的完整例子
1 from sqlalchemy.ext.declarative import declarative_base 2 from sqlalchemy import Column, Integer, String 3 from sqlalchemy.orm import sessionmaker 4 from sqlalchemy import create_engine 5 6 engine = create_engine("mysql+mysqldb://root:123@127.0.0.1:3306/s11?charset=utf8") 7 8 Base = declarative_base() 9 10 11 class User(Base): 12 __tablename__ = 'users' 13 id = Column(Integer, primary_key=True) 14 name = Column(String(50)) 15 16 # 尋找Base的全部子類,按照子類的結構在數據庫中生成對應的數據表信息 17 # Base.metadata.create_all(engine) 18 19 Session = sessionmaker(bind=engine) 20 session = Session() 21 22 23 # ########## 增 ########## 24 # u = User(id=2, name='sb') 25 # session.add(u) 26 # session.add_all([ 27 # User(id=3, name='sb'), 28 # User(id=4, name='sb') 29 # ]) 30 # session.commit() 31 32 # ########## 刪除 ########## 33 # session.query(User).filter(User.id > 2).delete() 34 # session.commit() 35 36 # ########## 修改 ########## 37 # session.query(User).filter(User.id > 2).update({'cluster_id' : 0}) 38 # session.commit() 39 # ########## 查 ########## 40 # ret = session.query(User).filter_by(name='sb').first() 41 42 # ret = session.query(User).filter_by(name='sb').all() 43 # print ret 44 45 # ret = session.query(User).filter(User.name.in_(['sb','bb'])).all() 46 # print ret 47 48 # ret = session.query(User.name.label('name_label')).all() 49 # print ret,type(ret) 50 51 # ret = session.query(User).order_by(User.id).all() 52 # print ret 53 54 # ret = session.query(User).order_by(User.id)[1:3] 55 # print ret 56 # session.commit()
filter不支持組合查詢,只能連續調用filter來變相實現。
而filter_by的參數是**kwargs
,直接支持組合查詢。
好比:
q = sess.query(IS).filter(IS.node == node and IS.password == password).all()
對應的sql是
SELECT tb_is.id AS tb_is_id, tb_is.node AS tb_is_node, tb_is.password AS tb_is_password, tb_is.email AS tb_is_email, tb_is.`admin` AS tb_is_admin, tb_is.contact AS tb_is_contact, tb_is.is_available AS tb_is_is_available, tb_is.is_url AS tb_is_is_url, tb_is.note AS tb_is_note
FROM tb_is
WHERE tb_is.node = %(node_1)s
and後面的條件既不報錯,又不生效。
要實現組合查詢,要麼連續調用filter:
q = sess.query(IS).filter(IS.node == node).filter(IS.password == password).all()
或者直接用filter_by:
q = sess.query(IS).filter_by(node=node, password=password).all()
二者都對應sql:
SELECT tb_is.id AS tb_is_id, tb_is.node AS tb_is_node, tb_is.password AS tb_is_password, tb_is.email AS tb_is_email, tb_is.`admin` AS tb_is_admin, tb_is.contact AS tb_is_contact, tb_is.is_available AS tb_is_is_available, tb_is.is_url AS tb_is_is_url, tb_is.note AS tb_is_note
FROM tb_is
WHERE tb_is.password = %(password_1)s AND tb_is.node = %(node_1)s
import sqlalchemy from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, String from sqlalchemy import ForeignKey from sqlalchemy.orm import relationship engine = create_engine("mysql+pymysql://root:alex3714@localhost/testdb", encoding='utf-8', echo=True) Base = declarative_base() #生成orm基類 class User(Base): __tablename__ = 'user' #表名 id = Column(Integer, primary_key=True) name = Column(String(32)) password = Column(String(64)) class Address(Base): __tablename__ = 'addresses' id = Column(Integer, primary_key=True) email_address = Column(String(32), nullable=False) user_id = Column(Integer, ForeignKey('user.id')) user = relationship("User", backref="addresses") #這個nb,容許你在user表裏經過backref字段反向查出全部它在addresses表裏的關聯項 def __repr__(self): return "<Address(email_address='%s')>" % self.email_address Base.metadata.create_all(engine) #建立表結構
表建立好後,咱們能夠這樣反查試試
1 obj = Session.query(User).first() 2 for i in obj.addresses: #經過user對象反查關聯的addresses記錄 3 print(i) 4 5 addr_obj = Session.query(Address).first() 6 print(addr_obj.user.name) #在addr_obj裏直接查關聯的user表
1 from sqlalchemy import Integer, ForeignKey, String, Column 2 from sqlalchemy.ext.declarative import declarative_base 3 from sqlalchemy.orm import relationship 4 5 Base = declarative_base() 6 7 class Customer(Base): 8 __tablename__ = 'customer' 9 id = Column(Integer, primary_key=True) 10 name = Column(String) 11 12 billing_address_id = Column(Integer, ForeignKey("address.id")) 13 shipping_address_id = Column(Integer, ForeignKey("address.id")) 14 15 billing_address = relationship("Address", foreign_keys=[billing_address_id]) 16 shipping_address = relationship("Address", foreign_keys=[shipping_address_id]) 17 18 class Address(Base): 19 __tablename__ = 'address' 20 id = Column(Integer, primary_key=True) 21 street = Column(String) 22 city = Column(String) 23 state = Column(String)
http://www.cnblogs.com/alex3714/articles/5978329.html