数据库常用操作语句

来源:互联网 发布:李荣浩后羿知乎 编辑:程序博客网 时间:2024/04/30 16:14

数据库常用操作语句:

1、查询同一字段名的多条记录:
select * from admin_log where type in(‘添加管理员’,’修改管理员’);
2、互换两列的值:
update test a inner join test b on a.id=b.id
set a.address=b.phone ,a.phone=b.address;
3、修改数据库表的编码:
ALTER TABLE table1 CONVERT TO CHARACTER SET utf8;
4、like语句的使用:
SELECT * FROM table1 WHERE create_time LIKE ‘%2015-09-06%’;
5、修改字段允许为空:
ALTER TABLE table1 MODIFY email VARCHAR(255) NULL;
6、添加一个字段:
ALTER TABLE table1 ADD user_type VARCHAR(255);
7、添加联合唯一约束性条件:
alter table table1 add constraint mulit_unique unique(email,entity_true);
8、添加联合主键:
ALTER TABLE table1 ADD INDEX mulit_index (os_type,key_sn);
9、删除约束性条件:
ALTER TABLE table1 DROP INDEX mulit_unique;
10、删除一条记录:
DELETE FROM table1 WHERE cert_id>443716;
11、修改记录中字段的值:
UPDATE user_cert SET cert_dn = replace(cert_dn, ‘bb’, ‘in’)
where account=’admbb’;
12、有中间表的查询:
SELECT * FROM tab1,tab2,tab1_tab2 tb where tb.tab1 = tab1.id and tb.tab2 = tab2.id and tab2.create_time is not null group by tab2.id;
13、查询建表语句:
show create table admin_log;
14、给表添加字段:
ALTER TABLE table1 ADD name VARCHAR(255);
15、修改表的某个字段允许为null:
ALTER TABLE table1 MODIFY name VARCHAR(255) NULL;
16、调好2条记录中的值:
UPDATE sys_user AS tb1,sys_user AS tb2 SET tb1.cert_id=tb2.cert_id,tb2.cert_id=tb1.cert_id WHERE tb1.id=9 AND tb2.id=8;
17、处理数据库中死锁的进程:
select * from information_schema.innodb_trx;
#select * from information_schema.processlist where id=[trx_mysql_thread_id];
kill [trx_mysql_thread_id];
18、查看服务器关闭连接之前等待的活动秒数:
show global variables like ‘wait_timeout’;
19、设置服务器关闭连接之前等待的活动秒数:
set global wait_timeout=60;
20、查看mysql日志:
show variables like ‘log_%’;
21、查看表的所有字段属性:
DESC bill;
DESCRIBE bill;
SHOW COLUMNS FROM bill;
22、查看表中某个字段的属性:
SHOW COLUMNS FROM bill WHERE field=’uid’;
23、设置数据库事物自动提交方式为关闭:
show variables like ‘autocommit’;
set autocommit=0;
24、写需要手动commit的sql语句:
begin;
update user_info set email=” where email=’ttt@ttt.com’;
#update delivery set user_info = 5 where id=3;
commit;
begin;
update user_info set email=’1’ where id=3;
commit;
25、查看mysql中连接的进程信息:
show processlist;

0 0