MySQL 基本操作

來源:互聯網
上載者:User

標籤:

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 基本操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.