#AUTO_INCREMENT 自增列
CREATE DATABASE dbtest;
USE dbtest;
SELECT DATABASE();
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,'Tam');
INSERT INTO test1
(id,last_name)
VALUES(NULL,'lili');
#开发中,一旦主键作用声明有自增列时,则我们在添加数据时就不要给字段对应的字段去赋值。
INSERT INTO test1
(id,last_name)
VALUES(-20,'lucky');
#ALTER TABLE时添加自增列(很少)
CREATE TABLE test2(
id INT PRIMARY KEY,
last_name VARCHAR(10)
);
DESC test2;
ALTER TABLE test2
MODIFY id INT AUTO_INCREMENT;
#ALTER TABLE时删除自增列
DESC test2;
ALTER TABLE test2
MODIFY id INT;
USE company;
#作业
SELECT last_name,salary
FROM employees
WHERE department_id = (
SELECT department_id
FROM employees
WHERE last_name ='Zlotkey'
);
SELECT employee_id,last_name,salary
FROM employees
WHERE salary >(
SELECT AVG(salary)
FROM employees
);