mysql 常用操作一

來源:互聯網
上載者:User

標籤:

安裝後登陸 -->mysql -uroot -p123456
顯示所有資料庫-->show databases;
建立新的資料庫-->create database 資料庫名;
刪除資料庫-->drop database 資料庫名;
使用資料庫-->use datsbase 資料庫名;

建立表 -->create table 表名(  []內容可寫可不寫
-->欄位1 類型[約束],
-->欄位2 類型[約束]
-->foreign (主表外鍵欄位) references 從表名(從表主鍵欄位)
-->);
查看錶結構-->desc 表名;
顯示所有表-->show tables;
刪除表-->drop table 表名;

操作資料增加-->insert into 表名[指定欄位名] value(欄位);
刪除-->delete from 表名[條件陳述式]
修改-->update 表名 set 欄位1=值,欄位2=值;

修改表結構增加一列-->alter table 表名 add 欄位 類型;
刪除一列-->alter table 表名 drop 欄位名;
修改列名-->alter table 表名 change 舊欄位 新欄位 新欄位類型;
修改表名-->alter table 表名 rename 新表名;
更改約束-->alter table 表名 modify 欄位 約束 類型;

約束條件主鍵-->alter table 表名 modify 欄位 primary key;【方法一】
-->create table score(【方法二】
-->id int primary key,
-->score int not null
-->);

-->create table score(
-->id int primary key,
-->score int not null,

-->primary key(id) 【方法三】
-->);
外鍵-->alter table 表名 modify 欄位 foreign key 從表名(主鍵);【方法一】
-->create table stu(
-->id int primary key auto_increment,
-->xuehao_id int not null,
-->name varchar(10) unique,
-->sex enum(‘男‘,‘女‘) default‘男‘,
-->foreign key(xuehao_id) references score(id)【方法二】
-->);

非空-->not null;
預設-->default‘‘;
枚舉-->enum(‘‘,‘‘);
唯一-->unique(資料唯一,null除外);
自增-->auto_increment;

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.