Mysql基本语句

来源:互联网 发布:hp1005扫描软件下载 编辑:程序博客网 时间:2024/06/11 05:49

1. 在safe-updates模式下,该模式会导致非主键条件下无法执行update等

    SET SQL_SAFE_UPDATES = 0

2. 创建表

    create table common.qc_question(      qc_type int(10) NOT NULL,      qc_id int NOT NULL AUTO_INCREMENT,      qc_describe VARCHAR(1255) NOT NULL,      qc_answer VARCHAR(255) NOT NULL,      PRIMARY KEY ( qc_id )      );  

3. 查询所有数据

    select * from common.qc_question

4. 清空表的内容

    truncate table common.qc_question

5. 随即获取n组数据

    select qc_describe,qc_answer from common.qc_question where qc_type = 1 and qc_id  >=       (select floor( max(qc_id) * rand()) from test_qc.qc_question ) order by qc_id limit n 

6. 自增数据查询(防止查询到以前查过的数据)

    select qc_id,qc_describe, qc_answer from common.qc_question where qc_type = 1  limit 2 offset  n*2(n为请求的次数)

7. 插入数据

    insert into common.question values(qc_type,null,"qc_describe","qc_answer")

8. 指定自增的字段从某一个序号开始

    alter table qc.qc_question  AUTO_INCREMENT=55;

9. 按照指定条件删除的数据

    delete from qc.qc_question where qc_id >= 55

10 向表中添加新的字段

    alter table table1(表名) add transactor(字段名字) varchar(10) not Null

11 向表中添加索引

    alter table test.test1 add index index_test1(uid) :index_test1为索引的别名    create index index_test2 on test.test1(trans)   : index_test2为索引的别名

12 添加主键

    Alter table tb add primary key(id);

13 获取系统当前时间:now()