# auto_increment 自增长列
CREATE DATABASE db;
use db;
SELECT DATABASE();
#在 CREATE TABLE 时创建
CREATE TABLE test1(
id int PRIMARY key auto_increment,
last_name VARCHAR(15)
);
desc test1;
INSERT into test1
(last_name)
VALUES('Sam');
SELECT * from test1;
#当我们向主键(含有自增列)的字段上添加0或null时,
INSERT into test1
(id,last_name)
VALUES(0,'Ta');
INSERT into test1
(id,last_name)
VALUES(0,'ku');
#开发中,一旦主键作用的字段(含有自增列)时
#则我们在添加数据时,就不要给主键对应的字段去赋值
#在 ALTER TABLE 时添加(很少)
CREATE table test2(
id int PRIMARY key,
last_name VARCHAR(15)
);
desc test2;
#添加字段类型
ALTER table test2
MODIFY id int auto_increment;
ALTER table test2
MODIFY id int ;
use atguigudb;
#查询和Zlotkey相同部门的员工姓名和工资
SELECT last_name,salary
from employees
where department_id=(
SELECT department_id
from employees
where last_name='Zlotkey'
);
#查询工资比公司平均工资高的员工的员工号,姓名和工资
SELECT department_id,last_name,salary
from employees
where salary>(
SELECT AVG(salary)
from employees
);