mysql相关命令

-- 查看创建数据库的SQL语句

1 show create database mydb; 

-- 查看当前安装的 MySQL 所支持的字符集。

1 show charset;

-- 查看 MySQL 数据库服务器和数据库字符集

1 show variables like'%char%';

-- 查看创建表的SQL语句

1 show create TABLE classes;

-- 查看创建数据库的SQL语句

1 show create database mydb;

-- 查看所有数据库名字

1 show databases;

-- 查看所有表名字

1 show tables;

-- 修改表的编码,此处必须写成utf8,不能写成utf-8

1 alter table classes DEFAULT CHARACTER set utf8;

-- 修改数据库的编码

1 alter database mydb DEFAULT CHARACTER set utf8;

-- 修改表字段的编码,此处两个className,第一个className指的是原字段名,第二个className指的是要修改后的字段名 

1 ALTER TABLE classes CHANGE className className VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci;-- 正确
2 ALTER TABLE classes CHANGE className className VARCHAR(50)-- 正确,后面必须跟字段的数据类型,此处是varchar(50)
3 ALTER TABLE classes CHANGE className className VARCHAR(50) character set utf8;-- 正确

-- 创建表,并添加外键

1 -- create table students(id int primary key not null auto_increment,classId int ,stuName VARCHAR(50),FOREIGN KEY(classId) REFERENCES classes(id));

-- 查看表的索引

1 show index from table_name 

 --添加索引

1 create index s_name on students(stuName);

--存储过程

 1 drop table if exists infor;
 2 create table infor
 3 (
 4 msg_id     int,
 5 comtent     varchar(255),
 6 type        varchar(255),
 7 creator_id   int,
 8 create_time  DATE
 9 );
10 
11 -- 存储过程代码块不用缩进
12 DELIMITER;//
13 drop procedure  if exists myproc;
14 create procedure myproc()
15 begin 
16 declare num int; 
17 set num=1; 
18 while num <= 1000 do 
19 insert into infor (msg_id,comtent,type,creator_id,create_time) values (num,CONCAT(num,'_testdata'),floor(5 + rand() * 10),10000+num,curdate());
20 set num = num +1;
21 end while;
22 end;//
23  
24 -- 调用存储过程
25 DELIMITER ;
26 CALL myproc();
27 select * from infor

 

posted @ 2018-06-08 19:31  一分耕耘,一分收获  阅读(170)  评论(0编辑  收藏  举报