auto_increment 字段自动增加
在插入数据的时候,字段会自动生成唯一的序号,序号为整数类型,一般自动增加的属性是给设置pk的字段加的,而且一个表中只能有一个字段加该属性。
--> create table t6(a int primary key auto_increment,b varchar(10)); insert into t6 (b) values('hello'); insert into t6 (b) values('hello'); insert into t6 (b) values('hello'); insert into t6 (a,b) values(10,'hello'); insert into t6 (b) values('hello'); select * from t6;关于数据库的操作:增删改查
插入数据
插入所有列的数据
yufa1: insert into table_name(列1,列2,。。。。。。列n) values(值。。。。。。。); 语法2: insert into table_name values(值1,值2,......,值n); create table mytest(id int,name varcahr(10),age int); insert into mytest(id,name,age) values(1001,'张三',30); insert into mytest(id,name,age) values(1002,'李四',25); select * from mytest;插入表中部分列的数据
b.插入表中部分列的数据 语法: insert into table_name(列1,列2...) values(值1,值2......) 说明:列是表中的部分列,部分数据的插入列必须写。 --> insert into mytest(id,name) values(1001,'zs'); insert into mytest(id) values(1); c.一次插入多条所有列的数据 语法1: insert into table _name(列1~列n,所有列) values(值1~值n),(值1~值n),(值1~值n),(值1~值n); 语法2: insert into table_name values(值1~值n),(值1~值n),(值1~值n),(值1~值n); 说明:值1~值n必须是所有列的值,不能少 --> insert into mytest(id,name,age) values(001,'xz',20),(002,'xl',25),(003,'xw',30),(004,'xh',26); --> insert into mytest values(100,'zx',20),(200,'lx',25),(300,'wx',30); d.一次插入多条部分列的数据 说明:表名后必须写哪几列 --> insert into mytest(id) values(1000),(2000),(3000); e.往表中插入查询中的结果 语法: insert into table_name(f1,f2,f3.....) select f_1,f_2,f_3..... from table_name1; --> create table mytest1(id int,name varchar(20)); 将mytest表中查询的结果,插入到mytest1中 --> insert into mytest1(id,name) select id,name from mytest; --> insert into mytest1(id,name) select id,age from mytest; 说明:查询出的age对应插入到name列,类型不一致会自动转换 -->将mytest表中id为1001的数据更新其name和age update mytest set name='zhangsan',age=40 where id=1001; -->将名字为ls的年龄更新为60 update mytest set age=60 where name='ls'; (2)更新数据操作 1)更新特定的数据 语法: update table _name set f1=v1,f2=v2... where 条件 说明:f1是列名,v1是更新的新数据值 2)更新所有的数据 语法:update table_name set f1=v1,f2=v2; 说明:update语句如果没有where条件,会更新表中所有的数据 -->更新mytest表中所有的数据,id为1001,name为zs,age为20 update mytest set id = 1001,name='zs',age=20; (3)删除数据操作 1)删除特定的数据 语法:delete from table_name where 条件; -->删除mytest表中id为1001的数据 insert into mytest values(1002,'ls',30); insert into mytest values(1003,'ww',10); delete from mytest where id=1001; 2)删除所有的数据 语法:delete from table_name; -->删除mytest所有数据; delete from mytest; (4)查询数据操作 1.单表数据查询 1)简单数据查询x 语法1:select f1,f2,f3.....from table_name; 语法2:select * from table_name; -->select empno,ename,job,MGR,Hiredate,sal,comm,deptno from t_employee; -->select * from t_employee; -->select empno,ename,sal from t_employee; -->select empn,ename,sal from t_employee; 错误的,列名不能写错 1.2 distinct避免重复数据的查询 -->查询t_employee表中职位job列的所有数据 select distinct job from t_employee; 1.3 可以使用四则运算进行数据查询 ( + - * / %) -->查询职工表t_employee中每一个员工的年薪 select ename,sal*12 from t_employee; **说明:给查询出的列重命名 select f1 [as] other_f1 from table_name; f1表示显示的原列名 other_f1表示显示的新列名,as可以省略 -->select ename,sal*12 as yearsal from t_employee; 1.4 设置显示格式的数据查询--concat concat()函数来连接字符串 -->查询职工表t_employee中每一个员工的年薪,年薪的显示格式“员工的年薪为xx” select concat(ename,'的年薪为:',sal*12) yearsal from t_employee; -->练习:查询t_employee表中的数据,显示“我是xx,我的生日是xx号” select concat('我是',ename,'我的生日是',Hiredate,'号')msg from t_employee; 2)条件数据查询 3)排序数据查询 4)限制数据查询 5)统计函数和分组数据查询