簡單的mysql資料庫的常用操作

來源:互聯網
上載者:User


啟動資料庫:

Mysql -uroot -proot (使用者名稱,密碼)

庫(database)的操作:

查看所有的資料庫。
Show databases;
建立資料庫
Create database dbname;
建立資料庫並指定編碼
Create database dbname character set urf8;
Show variables like ‘character%’;(查看資料庫的所有編碼)
修改編碼set character_set_client = gbk;(使用的用戶端編碼)
Set character_set_results = gbk;(結果集編碼)
刪除資料庫
Drop database dbname;
Drop database if exists dbname;
修改資料庫編碼
Alter database dbname character set utf8;
使用資料庫
Use dbname;
備份資料庫
首先是退出資料庫
Mysqldump -uroot -proot dbname > e:\dbname.sql
將dbname備份到e盤的dbname.slq中
恢複資料庫
刪除資料庫 drop database if exists dbname;
建立資料庫 create database dbname;
使用資料庫 use dbname;
匯入資料 source e:\mytest.sql

表的操作:


表的修改:


增加一個列(image)
Alter table tablename add image blob;
修改列
Alter table tablename modify job varchar(20);
修改表名
Rename table tablename to newtablename;
修改表的字元集
Alter table tablename character set utf8;
修改列名
Alter table tablename change column name username varchar(20);
查看所有表
Show tables;
查看錶的建立語句
Show create table tablename;
查看錶的結構
Desc tablename;
刪除表
Drop table tablename;

二、語句:增刪改查
建立表
Create table tablename(
Id int,
Name varchar(20)
);

Insert語句
Insert into tablename(id,name) values(1,’sensen’);

Update語句
Update tablename set salary = 1000 where name=’’;
Update tablename set salary = salary+1000 where name=’l%’;
Update tablename set salary=1000 where name=’l_’;

Delete語句
Delete from tablename where name=’’;
Delete from tablename(刪除表中所有記錄)

Truncate tablename(刪除表再建立表)

Select語句

Select * from tablename;
select * from student where ;
select distinct english from student;(過濾表中重複資料。)
select name,chinese c,english e from student;(使用別名表示學生分數。)
select * from student where english between 80 and 90;(between…and…)
select * from student where math in(89,90,30);(in..)

6.模糊查詢
select * from student where name like ‘李%’;(查詢所有姓李的學產生績。)
select * from student where name like ‘李_’;(查詢所有姓李的,名字是兩個字的學產生績。)

7.order by子句
select * from student order by math;(預設是升序 asc)
select * from student order by math desc;降序

Count
select count(*) from student;
後面可以加上where條件查詢

Max min
select max(chinese) from student;
select min(chinese) from student;

Sum
select sum(math) from student;

Avg
select avg(chinese) from student;

group by(歸類)
select * from orders group by product;

主鍵的設定:

非空
Not null

唯一
unique

主鍵=非空+唯一
Primary key

主鍵,自增長
Primary key auto_increment

多表關聯
一對一
多對一
在多的一方建立外鍵
多對多
需要建立中間表描述關係
中間表有兩個欄位都是外鍵參照兩個表的主鍵列,同時這兩列又是聯合主鍵

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.