MySQL 基本操作

1. show databases; // 显示数据库

2. use db_passport; // 使用db_passport数据库

3. 创建表

create table tb_access
(
aid int unsigned not null primary key,
status tinyint not null,
atime timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
sshkey varchar(33) not null,
aip varchar(16) not null
)engine=innodb default charset utf8;

4. show tables; // 显示数据库表

5. 修改数据库表名

alter table tb_user rename to aaa; // 将 tb_user 表 改名为 aaa

6. 添加列

alter table aaa add column address varchar(30) not null; // 添加一列 address 

7. 删除某列

alter table aaa drop column address; // 删除 address 列

5. desc tb_access; // 显示 tb_access 数据库表结构

6. 插入数据

insert into tb_user(aid,uid,username,password) values(100, 555,'kim','123qwe');

7. 删除某行数据

delete from aaa where name = '123'; // 从 aaa 表中删除 name 为 '123'的数据

8. 修改数据

MySQL 查看约束,添加约束,删除约束 添加列,修改列,删除列

查看表的所有信息:show create table 表名;
添加主键约束:alter table 表名 add constraint 主键 (形如:PK_表名) primary key 表名(主键字段);
添加外键约束:alter table 从表 add constraint 外键(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);
删除主键约束:alter table 表名 drop primary key;
删除外键约束:alter table 表名 drop foreign key 外键(区分大小写);
修改表名:alter table t_book rename to bbb;
添加列:alter table 表名 add column 列名 varchar(30);
删除列:alter table 表名 drop column 列名;
修改列名MySQL: alter table bbb change nnnnn hh int;
修改列名SQLServer:exec sp_rename't_student.name','nn','column';
修改列名Oracle:alter table bbb rename column nnnnn to hh int;
修改列属性:alter table t_book modify name varchar(22);
sp_rename:SQLServer 内置的存储过程,用与修改表的定义。

x. 查看数据库中的存储过程

select name from mysql.proc where db='db_passport'; // 查看 db_passport 的存储过程
+------------+
| name |
+------------+
| p_PreLogin |
+------------+
1 row in set (0.42 sec)

原文地址:https://www.cnblogs.com/sylar-liang/p/4792591.html