小能MySQL笔记 第三课

增删改查   --- 针对表

练习增删改查,需要建一张表
create table class( id int primary key auto_increment,

 sname varchar(10) not null default '',

gender char(1) not null default '',

company varchar(20) not null default '',

salary decimal(6,2) not null default 0.00,

fanbu smallint not null default 0

 )engine myisam charset utf8;

 

 

加: 1.往哪张表添加行?   -----class

        2. 给哪几列添加值? -----id,sname,gender,company,salary,     fanbu

        3.分别是什么值?     ----- 1,     张三,      男,         百度,   8888.67,234

 insert into class (id,sname,gender,company,salary,fanbu) values (1,'张三','男','百度',8888.67,234);

 

 

 

 

如果只插入部分呢,比如只插入姓名,性别,工资

Insert   into  class(sname,gender,salary)   values('刀锋','男',8765.43);

id在上列中虽然没有插入,但是id是自增型,因此值为2

 

如果插入所有列,可以不声明插入的列,即,如果不声明插入的列,则理解为依次插入所有列。

Insert  into class    values(3,'李四','女','新浪',5678.98,125);

 

 

 

 

 

 

改:

1.改哪张表?                ---class

2.改哪几列值?            ---gender,company

3.分别改为什么值?     ----女,千度

4.在哪些行生效?        ---- where

 

比如,将class表中曹操同学的fanbu,修改为123

Update  class set  fanbu=123  where id=6;

//where 后面接的是表达式expression,只要是where表达式为真,则改行就发挥作用。

 

 update class set  gender='男',fanbu='212'     where  sname='孙策';

//如果表里有两个名为孙策的同学,那么这样会修改这两行。

 

where进阶。修改性别为男,且工资大于8000的同学,将他们的fanbu都改为159。

Update class set  fanbu=159  where gender='男'  and salary>8000;

 

 

 

 

1.要删哪张表的数据?     class

2.删掉哪些行?            Where expression

删除就是指删除整行,不存在删除一行中的某几列。

 

删除工资大于8880.的同学

Delete from class  where  salary> 8800

 

删除工资大于8000且性别为女的同学

Delete  from class   where  salary>8000 and gender='女';

 

Delete from class;    删表跑路

 

 

查---基本查询

1.查哪张表?

2.查哪些列?

 

比如,要查class表中sname,company,salary这三个列,并且id=6。

Select  sname,company,salary from class where id=6;

 

暴力查询  select  *  from  class;

‘*’是通配符,代表所有列,表名后不加where条件,则选所有行。

 

取所有人的姓名和工资:select  sname,salary from class;

 

 

查id>3的人的所有列: select * from class where id>3;

 

id<5 同学的姓名和饭补:select sname,fanbu from class where id <5;

 

 

 

 

 

 

 

后面还要学什么?

如何自己建表?

如何修改表(增加减少列表)

多表联查

子查询

触发器

事务

存储过程

备份恢复

发布了33 篇原创文章 · 获赞 4 · 访问量 3735

猜你喜欢

转载自blog.csdn.net/linux2422988311/article/details/104224911