#自增长列 auto_increment
#在 CREATE TABLE 时创建
CREATE DATABASE dbtest;
use dbtest;
use employees;
SELECT DATABASE();
CREATE TABLE stu_1(
id int PRIMARY key auto_increment,
last_name VARCHAR(15)
CONSTRAINT
);
desc stu_1;
#当我们向主键(含有自增列)的字段上添加0,null时
#实际上会自动往上添加指定的字段的数值
#开发中,一旦主键含有自增列时,则我们添加数据时,就不要给主键对应的字段去赋值
INSERT into stu_1(id,last_name)
VALUES(-40,'aada');
SELECT * from stu_1;
#在 ALTER TABLE 时添加 auto_increment
CREATE TABLE stu_2(
id int PRIMARY key,
last_name VARCHAR(15)
);
CREATE TABLE stu_3(
id int,
last_name VARCHAR(10)
);
ALTER TABLE stu_3
MODIFY id int PRIMARY key;
desc stu_2;
#在 ALTER TABLE 时添加
ALTER TABLE stu_2
MODIFY id int auto_increment;
#在 ALTER TABLE 时删除
ALTER TABLE stu_2
MODIFY id int;
UPDATE employees
set employee_id=100
where employee_id=2;
SELECT * from employees
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
);
CREATE TABLE stu_1(
id int ,
salary VARCHAR(10),
last_name VARCHAR(15)
);
INSERT into stu_1 (id,salary,last_name)
VALUES(1,12123,'qasa');
ALTER TABLE stu_1
MODIFY salary int;