標籤:
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)
MySQL 基本操作