#自增长列 AUTO_INCREMENT
CREATE DATABASE dbtest;
USE dbtest;
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,'Tom');
INSERT into test1
(id,last_name)
VALUES(NULL,'LiLi');
#开发中,一旦主键作用的字段上声明有(增长列)则我们在添加数据时,就不要给主键对应的字段去赋值
INSERT into test1(id,last_name)
VALUES(30,'meiyangyang');
INSERT into test1(id,last_name)
VALUES(-30,'meiyangyang');
#在ALTER TABLE 时添加(很少)
CREATE TABLE test4(
id int PRIMARY KEY,
last_name VARCHAR(10)
);
DESC test4;
ALTER TABLE test4
MODIFY id INT AUTO_INCREMENT;
#在ALTER TABLE时删除
ALTER TABLE test4
MODIFY id INT;
USE atguigudb;
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
);