前言:呈现的是非常基础必备命令以及常规关联语法,因涉及到不同数据库其表达都会有所区别,此篇纯属做个仓库记录更非常规持续更新,专业人士可忽略,且看且珍惜…
mysql -uroot -p
select @@version; 或 select version();
select user(); --查看当前用户
show databases;
select database();
use test237 – 切入对应数据库
show tables;
select table_name from information_schema.tables where table_type = 'base table' and table_schema = 'test237';
# 复制表结构
CREATE TABLE target_database.table_name LIKE source_database.table_name;
create table test237.db1_1 like db1.db1_1;
# 导入数据
INSERT INTO target_database.table_name SELECT * FROM source_database.table_name;
insert into test237.db1_1 select * from db1.db1_1;
#-----------#
create database test237 default character set utf8;
create table test1(ID char(10),NAME char(20) ,SEX char(4),AGE int(10));
create user mysqluser identified by '123456';
grant all on *.* to 'test'@'%';
GRANT 权限 ON 数据库.表 TO 用户名@登录主机 identified by '密码'
GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' identified by 'password'
grant all privileges on *.* to 'test'@'localhost' identified by '123456';
flush privileges
show grants;
show grants for 'username'@'localhost';
show grants for 'root'@'%';
select user from mysql.user;
drop user 'test'@'localhost';
drop user 'test'@'%';
#-----------#
语法:create view 视图名 as select选取的列名1,选取的列名2.... from 源表;
create view view_name as select column1, column2 FROM table_name;
mysql> create view view_test1 as select ID,NAME from test1;
mysql> select * from view_test1;
drop view view_name;
drop view IF EXISTS view_name;
#-----------#
CREATE TABLE table_name (id INT,name VARCHAR(50),PRIMARY KEY (id));
alter table table_name add primary key(ID);
alter table table_name drop primary key(ID);
//一个数据表只可以有一个主键,所以不存在删除某一列的主键
#-----------#
//创建索引/删除索引索引不可以更改,想更改必须删除重新建
create index index_name on table_name (column_name);
//其中,index_name为要创建的索引名称,table_name为要添加索引的表名,column_name为要添加索引的列名。
create index index_name on table_name (column1, column2, ...);
# 删除索引
drop index index_name;
drop index idxname on table_name;
show index from table_name
//其中,table_name为需要查询索引的表名称。输出结果将包含该表的索引信息,包括索引名、列名、索引类型等。
#-----------#
select * from dba_blockers; --查询锁
select * from dba_waiters; --查询被阻塞的会话
select column_name from tab_old intersect select column_name from tab_new; --显示两表的相同数据
select @@version; 或 select version(); --查看数据库版本
select database(); --查看当前数据库
select user(); --查看当前用户
show tables; --查看所有表
show columns from tablename; --查看表中的列的基本信息
desc table; 或 describe table; --表名后加字段名,查看该字段基本信息
select CHARACTER_LENGTH(ID) from tablename; --查询该字段值的长度
#-----------#
MySQL:先创建一个企业数据做演示验证:
mysql -uroot -p
show databases;
show tables;
#-----------#
show databases;
create database test237 default character set utf8;
select schema_name,default_character_set_name from information_schema.schemata where schema_name = 'test237';
mysql> create table test1(ID char(10