增:
tee D:\1010.sql
回车后显示:Logging to file 'D:\1010.sql'
这句话是把敲的sql及结果都输出到一个sql文件里,便于复习。
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,
#decimal表示浮点型,(6,2)表示总共6位,小数点后2位。
fanbu smallint not null default 0
)engine myisam charset utf8;
建表完成后,可以用下面这个语句来查看表的结构:
desc class;
添值问题:
往哪张表/添哪几列/什么值
#注意:先要set names 一下,以防乱码
set names gbk;
#全插
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自动加了:
原因:id在上例中虽然没有插入,但id是自增型,因此值为2。
且最后,饭补在建表时设置的默认值default为0;
注意,宁愿把值设置成 0 ,也不要设为null,因为到时候检索会很麻烦。
回头再来看,插入所有列的情况,若果插入所有列,则可以不声明待插入的列。即:如果不声明插入的列,则理解为插入所有列。
insert into class
values
(3,'李四','女','新浪',5678.99,125);
注意:在SQL语句中,列与值是必须要按顺序一一对应的。不能在插入所有列时不声明任意一列。
改:
向表中重新插入后:
注意改的要素:
改哪张表?改哪几列?改成什么值?
#千万不能这么改!!因为会把整个表的所有行都改成性别为女
update class set gender='女';
在哪一行上生效?
update class set fanbu= 123 where id=6;
#where后面接的是表达式,只要表达式为真,该语句即被执行。
update class set gender='男', fanbu='212' where sname='孙策';
#改性别为男且工资大于8000的用户:
update class set fanbu=159 where gender='男' and salary>8000;
#注意这里的fanbu值和上面定义的时候,一个用的是字符型,一个用的是整型,其实两个都可以,都表示159这个数。
update class set fanbu=59 where 1;
#表示所有行都被修改。因为1恒为真。
删:
删除,就是指删除一整行,不存在删除一行中的某一列。
要删哪张表上的数据?要删哪几行?where expression
#删除salary大于8800的用户:
delete from class where salary>8800;
#同理,删除薪水大于8000且性别为女的:
delete from class where salary>8000 and gender='女';
##注意!!
delete from 表;
#表中的全部数据都被删了。
查:
查询哪张表上的数据?查询哪一列?
select sname, company,salary from class where id=6;
# * 代表所有列,表明后不加where条件,则选所有行,因此取出所有行。
select * from class;
#但不要频繁使用,因为工作时表一般很大,动不动*,占用资源
# 部分列,所有行,取所有人的姓名和工资。
select sname,salary from class; # 不要where
#查id>3的人的所有列
select * from class where id>3;
#取部分行部分列:
select sname,fanbu from class where id<5;