SELECT * FROM information_schema.table_constraints
WHERE table_name = 'test1';
CREATE DATABASE dbtest13;
USE dbtest13;
CREATE TABLE test1(
id INT NOT NULL,
last_name VARCHAR(15) NOT NULL,
email VARCHAR(25),
salary DECIMAL(10,2)
);
DESC test1;
INSERT INTO test1(id,last_name,email,salary)
VALUES(1,'Tom','[email protected]',3400);
INSERT INTO test1(id,last_name,email,salary)
VALUES(2,NULL,'[email protected]',3400);
INSERT INTO test1(id,last_name,email,salary)
VALUES(NULL,'Jerry','[email protected]',3400);
INSERT INTO test1(id,email)
VALUES(2,'[email protected]');
UPDATE test1
SET last_name = NULL
WHERE id = 1;
UPDATE test1
SET email = '[email protected]'
WHERE id = 1;
SELECT * FROM test1;
DESC test1;
ALTER TABLE test1
MODIFY email VARCHAR(25) NOT NULL;
ALTER TABLE test1
MODIFY email VARCHAR(25) NULL;
CREATE TABLE test2(
id INT UNIQUE,
last_name VARCHAR(15) ,
email VARCHAR(25),
salary DECIMAL(10,2),
CONSTRAINT uk_test2_email UNIQUE(email)
);
DESC test2;
SELECT * FROM information_schema.table_constraints
WHERE table_name = 'test2';
INSERT INTO test2(id,last_name,email,salary)
VALUES(1,'Tom','[email protected]',4500);
INSERT INTO test2(id,last_name,email,salary)
VALUES(1,'Tom1','[email protected]',4600);
INSERT INTO test2(id,last_name,email,salary)
VALUES(2,'Tom1','[email protected]',4600);
INSERT INTO test2(id,last_name,email,salary)
VALUES(2,'Tom1',NULL,4600);
INSERT INTO test2(id,last_name,email,salary)
VALUES(3,'Tom2',NULL,4600);
SELECT * FROM test2;
DESC test2;
UPDATE test2
SET salary = 5000
WHERE id = 3;
ALTER TABLE test2
ADD CONSTRAINT uk_test2_sal UNIQUE(salary);
ALTER TABLE test2
MODIFY last_name VARCHAR(15) UNIQUE;
CREATE TABLE USER(
id INT,
`name` VARCHAR(15),
`password` VARCHAR(25),
CONSTRAINT uk_user_name_pwd UNIQUE(`name`,`password`)
);
INSERT INTO USER
VALUES(1,'Tom','abc');
INSERT INTO USER
VALUES(1,'Tom1','abc');
SELECT *
FROM USER;
CREATE TABLE student(
sid INT,
sname VARCHAR(20),
tel CHAR(11) UNIQUE KEY,
cardid CHAR(18) UNIQUE KEY
);
CREATE TABLE course(
cid INT,
cname VARCHAR(20)
);
CREATE TABLE student_course(
id INT,
sid INT,
cid INT,
score INT,
UNIQUE KEY(sid,cid)
);
INSERT INTO student VALUES(1,'张三','13710011002','101223199012015623');
INSERT INTO student VALUES(2,'李四','13710011003','101223199012015624');
INSERT INTO course VALUES(1001,'Java'),(1002,'MySQL');
SELECT * FROM student;
SELECT * FROM course;
INSERT INTO student_course VALUES
(1, 1, 1001, 89),
(2, 1, 1002, 90),
(3, 2, 1001, 88),
(4, 2, 1002, 56);
SELECT * FROM student_course;
INSERT INTO student_course VALUES
(5,2,1002,67);
SELECT * FROM information_schema.table_constraints
WHERE table_name = 'student_course';
SELECT * FROM information_schema.table_constraints
WHERE table_name = 'test2';
DESC test2;
ALTER TABLE test2
DROP INDEX last_name;
ALTER TABLE test2
DROP INDEX uk_test2_sal;
CREATE TABLE test3(
id INT PRIMARY KEY,
last_name VARCHAR(15) PRIMARY KEY,
salary DECIMAL(10,2),
email VARCHAR(25)
);
CREATE TABLE test4(
id INT PRIMARY KEY,
last_name VARCHAR(15),
salary DECIMAL(10,2),
email VARCHAR(25)
);
CREATE TABLE test5(
id INT ,
last_name VARCHAR(15),
salary DECIMAL(10,2),
email VARCHAR(25),
CONSTRAINT pk_test5_id PRIMARY KEY(id)
);
SELECT * FROM information_schema.table_constraints
WHERE table_name = 'test5';
INSERT INTO test4(id,last_name,salary,email)
VALUES(1,'Tom',4500,'[email protected]');
INSERT INTO test4(id,last_name,salary,email)
VALUES(1,'Tom',4500,'[email protected]');
INSERT INTO test4(id,last_name,salary,email)
VALUES(NULL,'Tom',4500,'[email protected]');
SELECT * FROM test4;
CREATE TABLE user1(
id INT,
NAME VARCHAR(15),
PASSWORD VARCHAR(25),
PRIMARY KEY (NAME,PASSWORD)
);
INSERT INTO user1
VALUES(1,'Tom','abc');
INSERT INTO user1
VALUES(1,'Tom1','abc');
INSERT INTO user1
VALUES(1,NULL,'abc');
SELECT * FROM user1;
CREATE TABLE test6(
id INT ,
last_name VARCHAR(15),
salary DECIMAL(10,2),
email VARCHAR(25)
);
DESC test6;
ALTER TABLE test6
ADD PRIMARY KEY (id);
ALTER TABLE test6
DROP PRIMARY KEY;
CREATE TABLE test7(
id INT PRIMARY KEY AUTO_INCREMENT,
last_name VARCHAR(15)
);
INSERT INTO test7(last_name)
VALUES('Tom');
SELECT * FROM test7;
INSERT INTO test7(id,last_name)
VALUES(0,'Tom');
INSERT INTO test7(id,last_name)
VALUES(NULL,'Tom');
INSERT INTO test7(id,last_name)
VALUES(10,'Tom');
INSERT INTO test7(id,last_name)
VALUES(-10,'Tom');
CREATE TABLE test8(
id INT PRIMARY KEY ,
last_name VARCHAR(15)
);
DESC test8;
ALTER TABLE test8
MODIFY id INT AUTO_INCREMENT;
ALTER TABLE test8
MODIFY id INT ;
CREATE TABLE test9(
id INT PRIMARY KEY AUTO_INCREMENT
);
INSERT INTO test9
VALUES(0),(0),(0),(0);
SELECT * FROM test9;
DELETE FROM test9
WHERE id = 4;
INSERT INTO test9
VALUES(0);
DELETE FROM test9
WHERE id = 5;
SELECT * FROM test9;
INSERT INTO test9
VALUES(0);
CREATE TABLE test9(
id INT PRIMARY KEY AUTO_INCREMENT
);
INSERT INTO test9
VALUES(0),(0),(0),(0);
SELECT * FROM test9;
DELETE FROM test9
WHERE id = 4;
INSERT INTO test9
VALUES(0);
DELETE FROM test9
WHERE id = 5;
SELECT * FROM test9;
INSERT INTO test9
VALUES(0);
CREATE TABLE dept1(
dept_id INT,
dept_name VARCHAR(15)
);
CREATE TABLE emp1(
emp_id INT PRIMARY KEY AUTO_INCREMENT,
emp_name VARCHAR(15),
department_id INT,
CONSTRAINT fk_emp1_dept_id FOREIGN KEY (department_id) REFERENCES dept1(dept_id)
);
ALTER TABLE dept1
ADD PRIMARY KEY (dept_id);
DESC dept1;
CREATE TABLE emp1(
emp_id INT PRIMARY KEY AUTO_INCREMENT,
emp_name VARCHAR(15),
department_id INT,
CONSTRAINT fk_emp1_dept_id FOREIGN KEY (department_id) REFERENCES dept1(dept_id)
);
DESC emp1;
SELECT * FROM information_schema.table_constraints
WHERE table_name = 'emp1';
INSERT INTO emp1
VALUES(1001,'Tom',10);
INSERT INTO dept1
VALUES(10,'IT');
INSERT INTO emp1
VALUES(1001,'Tom',10);
DELETE FROM dept1
WHERE dept_id = 10;
UPDATE dept1
SET dept_id = 20
WHERE dept_id = 10;
CREATE TABLE dept2(
dept_id INT PRIMARY KEY,
dept_name VARCHAR(15)
);
CREATE TABLE emp2(
emp_id INT PRIMARY KEY AUTO_INCREMENT,
emp_name VARCHAR(15),
department_id INT
);
ALTER TABLE emp2
ADD CONSTRAINT fk_emp2_dept_id FOREIGN KEY(department_id) REFERENCES dept2(dept_id);
SELECT * FROM information_schema.table_constraints
WHERE table_name = 'emp2';
CREATE TABLE dept(
did INT PRIMARY KEY,
dname VARCHAR(50)
);
CREATE TABLE emp(
eid INT PRIMARY KEY,
ename VARCHAR(5),
deptid INT,
FOREIGN KEY (deptid) REFERENCES dept(did) ON UPDATE CASCADE ON DELETE SET NULL
);
INSERT INTO dept VALUES(1001,'教学部');
INSERT INTO dept VALUES(1002, '财务部');
INSERT INTO dept VALUES(1003, '咨询部');
INSERT INTO emp VALUES(1,'张三',1001);
INSERT INTO emp VALUES(2,'李四',1001);
INSERT INTO emp VALUES(3,'王五',1002);
UPDATE dept
SET did = 1004
WHERE did = 1002;
DELETE FROM dept
WHERE did = 1004;
SELECT * FROM dept;
SELECT * FROM emp;
USE atguigudb;
SELECT * FROM information_schema.table_constraints
WHERE table_name = 'employees';
USE dbtest13;
SELECT * FROM information_schema.table_constraints
WHERE table_name = 'emp1';
ALTER TABLE emp1
DROP FOREIGN KEY fk_emp1_dept_id;
SHOW INDEX FROM emp1;
ALTER TABLE emp1
DROP INDEX fk_emp1_dept_id;
CREATE TABLE test10(
id INT,
last_name VARCHAR(15),
salary DECIMAL(10,2) CHECK(salary > 2000)
);
INSERT INTO test10
VALUES(1,'Tom',2500);
INSERT INTO test10
VALUES(2,'Tom1',1500);
SELECT * FROM test10;
CREATE TABLE test11(
id INT,
last_name VARCHAR(15),
salary DECIMAL(10,2) DEFAULT 2000
);
DESC test11;
INSERT INTO test11(id,last_name,salary)
VALUES(1,'Tom',3000);
INSERT INTO test11(id,last_name)
VALUES(2,'Tom1');
SELECT *
FROM test11;
CREATE TABLE test12(
id INT,
last_name VARCHAR(15),
salary DECIMAL(10,2)
);
DESC test12;
ALTER TABLE test12
MODIFY salary DECIMAL(8,2) DEFAULT 2500;
ALTER TABLE test12
MODIFY salary DECIMAL(8,2);
SHOW CREATE TABLE test12;
CREATE DATABASE test04_emp;
USE test04_emp;
CREATE TABLE emp2(
id INT,
emp_name VARCHAR(15)
);
CREATE TABLE dept2(
id INT,
dept_name VARCHAR(15)
);
ALTER TABLE emp2
ADD CONSTRAINT pk_emp2_id PRIMARY KEY(id);
ALTER TABLE dept2
ADD PRIMARY KEY(id);
ALTER TABLE emp2
ADD dept_id INT;
DESC emp2;
ALTER TABLE emp2
ADD CONSTRAINT fk_emp2_deptid FOREIGN KEY(dept_id) REFERENCES dept2(id);
USE test01_library;
DESC books;
ALTER TABLE books
ADD PRIMARY KEY (id);
ALTER TABLE books
MODIFY id INT AUTO_INCREMENT;
ALTER TABLE books
MODIFY id INT PRIMARY KEY AUTO_INCREMENT;
ALTER TABLE books
MODIFY NAME VARCHAR(50) NOT NULL;
ALTER TABLE books
MODIFY AUTHORS VARCHAR(100) NOT NULL;
ALTER TABLE books
MODIFY price FLOAT NOT NULL;
ALTER TABLE books
MODIFY pubdate YEAR NOT NULL;
ALTER TABLE books
MODIFY num INT NOT NULL;
CREATE DATABASE IF NOT EXISTS test04_company CHARACTER SET 'utf8';
USE test04_company;
CREATE TABLE IF NOT EXISTS offices(
officeCode INT(10) PRIMARY KEY ,
city VARCHAR(50) NOT NULL,
address VARCHAR(50) ,
country VARCHAR(50) NOT NULL,
postalCode VARCHAR(15),
CONSTRAINT uk_off_poscode UNIQUE(postalCode)
);
DESC offices;
CREATE TABLE employees(
employeeNumber INT PRIMARY KEY AUTO_INCREMENT,
lastName VARCHAR(50) NOT NULL,
firstName VARCHAR(50) NOT NULL,
mobile VARCHAR(25) UNIQUE,
officeCode INT(10) NOT NULL,
jobTitle VARCHAR(50) NOT NULL,
birth DATETIME NOT NULL,
note VARCHAR(255),
sex VARCHAR(5),
CONSTRAINT fk_emp_offcode FOREIGN KEY (officeCode) REFERENCES offices(officeCode)
);
DESC employees;
ALTER TABLE employees
MODIFY mobile VARCHAR(25) AFTER officeCode;
ALTER TABLE employees
CHANGE birth employee_birth DATETIME;
ALTER TABLE employees
MODIFY sex CHAR(1) NOT NULL;
ALTER TABLE employees
DROP COLUMN note;
ALTER TABLE employees
ADD favoriate_activity VARCHAR(100);
RENAME TABLE employees
TO employees_info;
DESC employees;
DESC employees_info;