MySQL操作表中数据

create database if not exists xuexiao default charset=utf8;
use  xuexiao;
create table studentInfo (
 `name` varchar(10),
 sex char,
 age int,
 address  varchar(10)
 
);
show tables;
-- 插入数据
insert into studentInfo (`name`, sex,age,address) values ('zhangsan' , '男' ,18,"河南省许昌市");
select *from studentinfo;  -- 打印
--  要插入的数据顺序和创建表格的数据顺序相同,则字母可以省略
insert into studentInfo values ('李四' , '女' , 20 , "河南省安阳市");
--  要插入的数据顺序和创建表格的数据顺序不同,则字段不可以省略,要求一一对应
insert into studentinfo (sex , address , age , `name`) values ('男',"美国拉斯维加斯" , 18,'安德伍德');
insert into studentinfo (`name`) values ('小爱' ); -- 没有默认值, 只给定一个字段,则其他字段为空
create table if not exists studentInfo2(
  `name` varchar(10),
    sex char default '男',
    age int default 18 ,
    address varchar(10) default "河南省"
    
    );
    insert into studentinfo2 (`name` , sex,age,address) values ('张三','男',28,"河南省许昌市");
    select * from studentinfo2;  -- 打印
    insert into studentinfo2 values ('张三' , '男',28,"河南省许昌市");
    -- 有默认值的情况,可以不给定值,则值为默认值,但是字段需要写
    insert into studentinfo2 (`name`) values ('小爱');
    
    create table if not exists studentInfo3 (
     id int primary key auto_increment,
     `name` varchar(10),
     sex char default '男',
     age int default 18 ,
     address varchar(10) default "河南省"
     
     );
     
     insert into studentinfo3 (`name` , sex,age,address) values ( '小红' , '女' , 19 ,"河南");
     select * from studentinfo3;
     insert into studentinfo3 (`name` , sex,age,address) values ( '小红' , '女' , 19 ,"河南");
                                                                                                                            ( '小红' , '女' , 19 ,"河南");
                                                                                                                            ( '小红' , '女' , 19 ,"河南");
                                                                                                                            ( '小红' , '女' , 19 ,"河南");
--  查询
select * from studentinfo;
select * from studentinfo2;
select `name` , sex ,age, address from studentinfo;
select `name` , age  from  studentinfo; -- 自定义查询的字段

-- 更新
update studentinfo2  set  age=age+1 where name = '小爱';
update studentinfo2  set  age=age+1 ;
update studentinfo2  set  age= 18 ;

-- 删除
delete  from studentinfo where `name`="小爱";
delete  from studentinfo2;

truncate table studentinfo;

你可能感兴趣的:(MySQL操作表中数据)