MySQL临时表
创建临时表
create temporary table temp_table_name (
column1 datatype,
column2 datatype,
...
);
或者简写为:
create temporary table temp_table_name AS select column1,column2,...
from source_table
where condition;
插入数据到临时表
insert into temp_table_name (column1,column2,...)
values (value1,value2,...);
查询临时表
select * from temp_table_name;
修改临时表
alter table temp_table_name
add column new_column datatype;
删除临时表
drop temporary table if exists temp_table_name;
--创建临时表
create temporary table temp_orders AS select * from orders where order_date >= '2023-01-01';
--查询临时表
select * from temp_orders;
--插入数据到临时表
insert into temp_order(order_id,customer_id,order_date) values(100,1,'2023-01-05');
--查询临时表
select * from temp_orders;
--删除临时表
drop temporary table if exists temp_orders;
MySQL复制表
第一步:使用show create table 命令获取创建数据表(create table)语句,该语句包含了原数据表的结构、索引等)。
show create table runoob_tbl \G;
第二步:复制以下命令显示的SQL语句,修改数据表名,并且执行sql语句,通过以上命令将完全的复制数据表结构。
create table 'clone_tbl'(
'runoob_id' int(11) not null auto_increment,
'runoob_title' varchar(100) not null default '',
'runoob_author' varchar(40) not null default '',
'submission_date' date default null,
primary key ('runoob_id'),
unique key 'author_index' ('runoob_atuthor')
)engine=innodb;
第三步:将数据库中创建新的克隆表clone_tbl
insert into clone_tbl(runoob_id,
runoob_title,
runoob_author,
submission_date)
select runoob_id,runoob_title,runoob_author,submission_date from runoob_tbl;
MySQL元数据
MySQL元数据是关于数据库和其对象(如表、列、索引等)的信息;
元数据存储在系统表中,这些表位于MySQL数据库的information_schema数据库中。
以下是一些常用的MySQL元数据查询:
show databases; //查看所有数据库;
use database_name; //选择数据库;
show tables; //查看数据库中的所有表;
desc table_name; //查看表的结构;
show index from table_name; //查看表的索引;
show create table table_name; //查看表的创建语句;
select count(*) from table_name; //查看表的行数;
select column_name,data_type,is_null,column_key
from information_schema.columns
where table_schema = 'your_database_name'
and table_name = 'your_table_name'; //查看列的信息;your_database_name和your_table_name分别是你的数据库名和表名;
select
table_name,
column_name,
constraint_name,
referenced_table_name,
referenced_column_name
from
information_schema.key_column_usage
where
table_schema = 'your_database_name'
and table_name = 'your_table_name'
and referenced_table_name is not null; //查看外键信息;your_database_name和your_table_name为实际的数据库名和表名;
information_schema数据库
information_schema是MySQL数据库中的一个系统数据库,包含有关数据库服务器的元数据信息,这些信息以表的形式存储。
shemata表
存储有关数据库的信息,如数据库名、字符集、排序规则等。
select * from information_schema.schemata;
tables表
包含有关数据库中所有表的信息,如表名、数据库名、引擎、行数等。
select * from information_schema.tables where table_schema = 'your_database_name';
columns表
包含有关表中列的信息,如列名、数据类型、是否允许null等。
select * from information_schema.columns
where table_schema = 'your_database_name' and table_name = 'your_table_name';
statistics表
提供有关表索引的统计信息,如索引名、列明、唯一性等。
select * from information_schema.statistics
where table_schema = 'your_database_name' and table_name = 'your_table_name';
key_column_usage表
包含有关表中外键的信息,如外键名、列名、关联表等。
select * from information_schema.key_column_usage
where table_schema = 'your_database_name' and table_name = 'your_table_name';
referential_constraints表
存储有关外键约束的信息,如约束名、关联表等。
select * from information_schema.referential_constraints
where constraint_schema = 'your_database_name' and table_name = 'your_table_name';
MySQL序列使用(auto_increment)
create table emple_table (
id int auto_increment primary key,
name varchar(50)
); //使用auto_increment创建表。每次插入一行数据时,id列的值会自动增加。
insert into example_table (name) value ('john'); //插入一行数据时,不指定id列值,数据库会自动分配一个唯一的、自增的值;
select last_insert_id(); //last_insert_id()函数来获取刚刚插入的行的子增值;
show table status like 'example_table'; //获取表的当前自增值;
MySQL处理重复数据
防止表中出现重复数据
设定制定的字段为primary key(主键)、unique(唯一)索引来保证数据的唯一性。
create table person_tbl
(
first_name char(20) not null,
last_name char(20) not null,
sex char(10),
primary key(last_name,first_name)
);
create table person_tbl
(
first_name char(20) not null,
last_name char(20) not null,
sex char(20),
unique(last_name,first_name)
);