赞
踩
shell 连接测试
mysql -h47.89.7.156 -uroot -P3306 -phello test_db
1. 插入一条数据
insert into table_name(name,sex) values('Joe','男');
INSERT INTO SELECT 语句从一个表复制数据,然后把数据插入到一个已存在的表中。目标表中任何已存在的行都不会受影响。
- INSERT INTO table2
- SELECT * FROM table1;
insert overwrite table 语句从一个表复制数据,目标表中数据会全部被覆盖。
- insert overwrite table table_name
- select * from table1;
2. 更新一条数据
update table_name set name='807u' where id=11;
3. 删除一条数据
delete from table_name [WHERE condition(s)];
4. row_number()
mysql 8.0以上版本实现了row_number(),以下版本可自己实现。
可先通过 group by 分组,然后 cnt>2 的单独筛选出来进行处理。
下面代码可将重复两次及以上的uid记录全部筛选出来。
- select a.*
- from table_name a
- join (
- select uid, count(1)
- from table_name
- group by uid
- having count(1)>1
- ) b
- on a.uid=b.uid;
- use db_name;
- drop table if exists table_name;
- create table table_name as
- select a.*
- from table_name1 a;
- # 为表字段fid、update_time分别创建索引
- CREATE INDEX fid_inx ON table1 (fid);
- CREATE INDEX update_time_inx ON table1 (update_time);
-
- # 删除索引update_time_inx
- drop index update_time_inx on table1;
-
- # 查看表table1的所有索引
- show index from table1;
- import MySQLdb
- cur = None
- conn = None
-
- # 初始化连接
- def init():
- global conn
- global cur
- conn = MySQLdb.connect(
- host='10.94.136.186',
- port=3306,
- user='root',
- db='database_temp'
- )
- cur = conn.cursor()
-
- # 关闭连接
- def finish():
- if cur:
- cur.close()
- conn.commit()
- conn.close()
- def insert_info_to_db(table_name, cellid, city_code):
- insert_sql = "insert into " + table_name + " values(%s, %s)"
- cur.execute(insert_sql, (cellid, city_code))
- if __name__ == '__main__':
- # 1. connect db
- init()
- table_name = 'tablename_temp'
- # 2. create table
- cur.execute("DROP TABLE IF EXISTS " + table_name)
- create_table_sql = "CREATE TABLE " + table_name + \
- "(cellid int(20) not null," \
- "city_code int(20) NOT NULL, " \
- "primary key(cellid, city_code));"
- cur.execute(create_table_sql)
-
- # 3. insert info
- insert_info_to_db(table_name, 11, 22)
- # 4. close db
- finish()
一款很好用的SQL美化工具,和DBeaver搭配效果超级棒!!!!
点击“+”可以连接各种数据库,比Navicat还好用,能够快速调试SQL语句,定位问题。
以连接Mysql为例:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。