MySQL数据库增删改查的SQL语句

来源:互联网 发布:网络监听工具 编辑:程序博客网 时间:2024/06/08 06:11


添加的语句:db.execSQL("insert into toutiao (type,title) values (?,?)",new String[]{type,title});
删除的语句:db.execSQL("delete from shoucang where title = ?",new String[]{title});
修改的语句:db.execSQL("update toutiao set type = ? where title = ?",new String[]{type,title});
查询的语句:Cursor query = db.query("toutiao", null, null, null,null,null,null);

按条件查询全部:Cursor query = db.query("toutiao", null, "type=?", new String[]{tiaojian},null,null,null);
        
模糊查询并且降序:sql = "select * from house where price_address like '%"+pri+"%' order by price asc";
Cursor house = db.rawQuery(sql, null);
        
模糊查询并且升序:sql = "select * from house where price_address like '%"+pri+"%' order by price desc";
Cursor house = db.rawQuery(sql, null);



-- 登录数据库

        mysql -uroot -proot
        mysql -uroot -p

        (如果遇到 Can't connect to MySQL server on 'localhost' (10061)
        那么可以右击计算机(我的电脑)--->管理--->服务和应用程序--->服务,
        找到MySQL,启动该服务,关闭cmd命令窗口后,重新打开进行登录)


        show databases;

        -- 新建数据库
        create database 1503d;

        -- 删除数据库
        drop database 1503d;

        -- 选择数据库
        use 1503d;

        -- 查看所有表格
        show tables;

        -- 创建表
        --
        create table t_student2(
            id int(11)    primary key auto_increment,
            stuno varchar(20) not null unique,
            name varchar(10) not null,
            age int(2) default 18,
            sex char(1),
            birthday date default '1998-01-01',
            marriage boolean
        );

        -- 查看表结构
        describe t_student;
        desc t_student;


        -- 向表中插入数据
        -- insert into 表名(列名A,列名B...) value(A列的值,B列的值...),(A列的值,B列的值...),(A列的值,B列的值...)
        insert into t_student(id,stuno,name,age,sex,birthday)  value(null,'162661000529','柴俊杰',23,'男','1993-05-31');
        insert into t_student values(null,'162661000714','王威',19,'男','1997-06-14',false);
        insert into t_student values(null,'162661000715','王威2',19,'男','1997-06-14',false),(null,'162661000716','王威3',19,'男','1997-06-14',false),(null,'162661000717','王威4',19,'男','1997-06-14',false);

        -- 查询表数据
        -- select 列名A,列名B...  from 表名
        select name,sex from t_student;
        -- select  * from  表名

        
        -- 删除表数据
        -- delete from 表名 where 条件

        -- 清空表数据
        truncate table 表名;


        -- 修改表数据
        -- update 表名  set 列名A=A列的新值, 列名B=B列的新值...  where 条件
        update t_student set age = age+1 where id = 1;

        --  设置gbk字符集
        set names gbk;



        -- 修改表名
        alter table 表名  rename 新表名;
        rename table 表名 to 新表名;

        -- 增加表字段
        alter table 表名 add 新列名 数据类型 ;
        alter table 表名 add 新列名 数据类型  first;
        alter table 表名 add 新列名 数据类型  after 列名;

        -- 删除表字段
        alter table 表名 drop 列名;

        -- 修改表字段的名称和数据类型
        alter table 表名 change 列名 新列名 数据类型;

        -- 修改表字段的数据类型
        alter table 表名 modify 列名 数据类型;


        -- 【单表查询】
        -- 排序 order by      asc升序(降序desc)    默认asc
        -- 单列排序
        select 列名A,列名B... from 表名  order by 列名 asc;
        -- 多列排序
        select 列名A,列名B... from 表名  order by 列名1 asc, 列名2 desc;


        -- 分页查询
        -- limit M,N   M表示起始索引(索引是从0开始的),  N表示要查询的数据条数
        -- 每页2条,查询第2页的数据    limit 2,2
        -- 每页10条,查询第5页的数据    limit 40,10
        -- 每页有pageSize条,查询第n页的数据    limit (n-1)*pageSize,pageSize


        -- where条件
        -- 【关系运算符】
        -- 查询年龄介于16和18之间(包含16和18)的所有学生的信息:
        select * from t_student where age between 16 and 18;
        -- 【逻辑运算符  and or not  (&&   ||  !)】
        -- 查询年龄介于16和18之间(包含16和18) 或者是id为偶数的学生:
        select * from t_student where (age >= 16 and age <= 18) or id%2 = 0;
        select * from t_student where (age >= 16 && age <= 18) || id%2 = 0;

        --查询年龄不等于18的学生的信息:
        select * from t_student where age != 18;
        select * from t_student where age <> 18;

        -- 查询性别不为空的学生的信息:
        select * from t_student where xingbie is not null;

        -- 查询年龄不大于18的学生的信息:
        select * from t_student where not(age > 18);
        select * from t_student where !(age > 18);
        select * from t_student where age <= 18;

        -- 【日期函数】
        -- 查询当前日期
        select curdate() from dual;
        select current_date from dual;

        -- 查询当前日期和时间
        select now() from dual;
        select sysdate() from dual;

        -- 查询当前时间(不带有年月日):
        select curtime() from dual;

        -- 【聚合函数 avg平均值    max最大值    min最小值     sum求和   count计数】
        -- 查询学生表中的最大年龄
        -- 起别名时可以使用as或省略
        select max(age) 最大年龄 from t_student;
        select max(age)  as 最大年龄 from t_student;


         select max(age) 最大年龄,min(age) 最小年龄 , avg(age) 平均年龄 ,
         sum(age) 年龄总和, count(age) 行数 from t_student;

         -- like 模糊查询
         -- 通配符:①_:匹配一个字符②%:匹配任意0到n个字符
         -- 查询姓名的倒数第二个字符为"威"的学生信息:
          select * from t_student where name like '%威_';

          -- in的使用
          -- 查询年龄不在16,18,20之中的所有学生的信息:
          select * from t_student where age not in (16,18,20);

          -- 去除重复行
           select distinct xingbie,birthday from t_student;

           -- 分组查询  group by 字段名
           -- 查询平均年龄大于17的城市:
            select address,avg(age) from t_student group by address having avg(age)>17;

        
        -- 查询语句中各关键字的使用顺序:
        select
            [distinct] 列名1 as 别名1,列名2 as 别名2...
        from
            表名
        where 条件
          group by
          having
          order by
          limit



        create table school(
            id int primary key auto_increment,
            name varchar(20) unique,
            address varchar(20),
            tel varchar(20)
        );

        create table student(
            id int primary key auto_increment,
            name varchar(20) ,
            age int ,
            school_id int
        );

        -- 在表已经存在的情况下添加外键约束:
        alter table student
        add constraint foreign key (school_id) references school(id)
        on delete cascade;



        -- 查询 19岁的张三所在学院的地点【内联接】①:
        select sch.address from student stu inner join school sch
            on stu.school_id = sch.id
            where stu.age = 19 and stu.name = '张三';

        select school.address from student inner join school
            on student.school_id = school.id
            where student.age = 19 and student.name = '张三';

        -- 查询 19岁的张三所在学院的地点【内联接】②:
         select school.address from student,school
         where student.school_id = school.id
            and  student.age = 19
            and student.name = '张三';


        -- 查询各个学院的学生信息【内联接】
         select school.*,student.* from school ,student
         where  school.id = student.school_id;
        -- 查询各个学院的学生信息【左联接】
         select school.*,student.* from school left join student
            on school.id = student.school_id;
        -- 查询各个学院的学生信息【右联接】
         select school.*,student.* from student right join school
            on school.id = student.school_id;
        




        insert into school(id,name,address,tel)
        value(1,'移动学院','2号楼','111111')
        ,(2,'游戏学院','6号楼','222222')
        ,(3,'软工学院','8号楼','333333');

        insert into student(name,age,school_id)
        value('张三',19,1)
        ,('李四',20,2)
        ,('王五',18,3)
        ,('赵六',17,2)
        ,('孙七',21,1);



        -- where型子查询
        -- 将移动学院的所有学生的年龄增加1
        update student set age = age+1 where school_id =
        (select id from school where name = '移动学院');

        -- 查询移动学院的学生人数
        select count(*) from student where school_id =
        (select id from school where name = '移动学院');
原创粉丝点击