database operation
Table of contents
Addition, deletion, modification and query operations of DML database
2. Database modification and deletion statements
Overview of the seven major clauses
0. View all databases
show database
1. Create a database
create database 数据库名
2. Use the database
use 数据库名
3. Create table
CREATE TABLE students(
id VARCHAR(6) PRIMARY KEY not null,
name VARCHAR(10),
sex CHAR(2) DEFAULT '女',
age INT,
dep VARCHAR(20) DEFAULT '河南省郑州市'
)
4. Add a column name to the table, the type is varchar, and it is not empty
ALTER TABLE students ADD 班级 VARCHAR(50) NOT NULL;
5. Modify the attribute value of the field name in the table (non-repeated field name)
ALTER TABLE students MODIFY 字段名VARCHAR(15);
6. Modify the attribute value of the field name in the table (repeated field name)
alert table 表名 change 原名 新名 类型和约束
7. Delete the field name in the table
alert table 表名 drop 列名
8. Delete the database (the statement that deletes the database and runs away in the company)
drop database 数据库名
9. Create composite keys
alter table 表名 add primary key (字段名1,字段名2);
Addition, deletion, modification and query operations of DML database
Create a data table as an example
CREATE TABLE teacher(
tid INT,
tname VARCHAR(5),
age INT,
gender CHAR(5),
salary DOUBLE(8,2)
);
1. Add data to the database
1. Add a piece of data
/*
添加数据
insert into 表名
添加数据时,字段类型,长度一定需要一一对应
*/
-- 添加一行数据时(全部字段)
insert into teacher values(1,'胡桃',17,'女',8000.00);
insert into teacher values(2,'青椒肉丝',5,'男',9000.50);
insert into teacher values(3,'云韵',25,'女',31000.00);
2. Add multiple pieces of data
insert into teacher values(4,'美杜莎',25,'女',30000.00),
(5,'阿尔法',25,'男',30000.00),
(6,'雅菲',25,'女',31000.00);
3. Add multiple pieces of data (partial fields)
-- 添加数据(部分字段)
INSERT INTO teacher(tanme,gender) values('小医仙','女');
INSERT INTO teacher(tanme,gender) values('萧熏儿','女'),('维列那','女');
4. Query all data information of the current table
select * from teacher;
2. Database modification and deletion statements
/*
DML-修改表数据
update 表名 set
*/
select * from teacher;
-- 修改表数据,将tname改成云曦,如果不标注条件,默认修改所有数据
update teacher set tname = '云曦';
-- 修改表中的数据,tname 改成石昊 筛选条件 tid = 1
update teacher set tname = '石昊' where tid = 1;
/*
DML-删除表数据
delete from 表名
*/
-- 删除tid为1的行数据()
delete from teacher where tid = 4;
3. Delete all the data, but the table structure still exists, operate the table structure DDL statement, delete all the data line by line, how many lines, how many times to delete, leave the table structure
DELETE FROM teacher ;
Statements that drop tables, truncate tables
-- 删除所有的数据,但表结构依然存在(截断表)
-- 无论有多少行数据,操作只有两步,1,drop掉,2,建立一个和原表一致的表结构
TRUNCATE teacher;
DQL single table query
/*
from clause
Table name -- represents the data source of the query
grammar
select
field
from
Table Name
*/
1. Query all fields
select * from teaacher;
2. Query some fields (eid, ename)
select eid,ename from teacher;
3. You can alias the field name when querying (only the display form is modified, the original data is not modified) AS can be uppercase, lowercase, or omitted
select eid,ename AS '名字' from teacher;
4. Query when removing duplicates (DISTINCT)
select distinct did from teacher;
5. Query people between the ages of 20 and 30
select * from teacher where age < 30 and age > 20;
fuzzy query
like
% means any number of arbitrary characters
_ means an arbitrary character
6. Query people with cloud in their name
select * from teacher where ename like '云%';
7. Query people whose surname is cloud and 'name' is a word
select * from teacher where name like '云_';
8. Search for people with the word fairy in their name
select * from teacher where ename like '%仙%';
constraint
1. Primary key constraint: unique + non-null
Keyword: primary key
Features: A table can only have one primary key constraint, and the primary key cannot be repeated or empty.
create table qin(
qid int primary key,
ename varchar(20)
)
2. Self-increment attribute:
Automatically assign a value to a field (automatically increase 1)
It cannot be repeated. There can only be one auto-increment attribute in a table, and it cannot be used alone. It is generally used with the primary key
Keyword: auto_increment
create table qin2(
qid int primary key auto_increment,
ename carchar(20)
)
3. Unique constraint keyword: unique key
Features: A table can have multiple unique constraints, and unique constraints can be empty
create table emp03(
qid int primary key auto_increment,
ename varchar(20) unique key,
cardid char(18) union key
)
4. Non-null constraint: The field is required not to be empty
Keyword: not null Features: A table can have multiple not-null constraints
CREATE TABLE qin4(
qid INT PRIMARY KEY AUTO_INCREMENT,
qname VARCHAR(20) UNIQUE KEY,
cardid CHAR(18) UNIQUE KEY,
tel VARCHAR(11) NOT NULL
);
5. Default constraint keyword: default
Features: When adding a default value, the unspecified value is added as null, and the default value can be set
CREATE TABLE qin5(
eid INT PRIMARY KEY AUTO_INCREMENT,
ename VARCHAR(20) UNIQUE KEY,
cardid CHAR(18) UNIQUE KEY,
tel VARCHAR(11) NOT NULL,
gender VARCHAR(5) DEFAULT '男'
);
6. Check constraint keywords: check
Features: Used to constrain the range of the field value
CREATE TABLE qin06(
eid INT PRIMARY KEY AUTO_INCREMENT,
ename VARCHAR(20) UNIQUE KEY,
age INT CHECK(age >=0 && age<= 150)
);
7. Foreign key constraints
Used to limit the relationship between two tables (master table and slave table/parent table and child table)
The foreign key is established in the slave table
keywords
foreign key (field name from the table) references main table name (referenced field name)
related concepts
Main table (parent table): the table that needs to exist first, the referenced table, and the dependent table
department table
class table
Slave table (child table): table that needs to refer to other tables
employee form
student table
Features:
1. The foreign key is established from the table, and a table can have multiple foreign keys
2. Create a table, first create the main table
3. When deleting, first delete the slave table
4. To establish a foreign key, the key column (unique key, primary key) must be referenced when referencing
5. When creating a foreign key from a table, it needs to be consistent with the referenced key data type and logical meaning (field names are not necessarily consistent)
-- 创建表,先创建主表
CREATE TABLE dept07(
did INT PRIMARY KEY AUTO_INCREMENT,
dname VARCHAR(20)
);
-- 创建从表(员工表)
CREATE TABLE emp07(
eid INT ,
ename VARCHAR(20),
did INT,
FOREIGN KEY (did) REFERENCES dept07(did)-- 创建外键
);
Overview of the seven major clauses
/*
select 7大子句
书写时一定要按此顺序编写
select
字段列表 -- 代表的是最终查询后显示的字段内容
from
表名 -- 代表的是查询的数据源
on
表名 -- 多表查询(后面具体讲)
where
条件列表 -- 查询过程中需要筛选的条件
group by
分组字段 -- 将查询结果按照一定的字段要求进行分组
having
分组后的条件
order by
排序字段 -- 代表的是排序的依据
limit
分页限定 -- 代表将数据进行分页显示
*/