mysql基礎知識之-資料庫的建立、查看等常用操作

來源:互聯網
上載者:User

標籤:不相容   ast   varchar   .net   訪問   custom   字串轉換   rename   use   

命令建立 mysql 資料庫 :

先啟動mysql資料庫,串連資料庫:

  mysql -uroot -p123456         (文法:mysql -u登入名稱 -p密碼)

建立表:

  create database spring_data;    (文法:create database 資料庫名稱)

使用建立的資料庫:

  use spring_data;

查看是否存在表:

  show tables;

#查看錶中的列

  SHOW COLUMNS FROM auth_user;

建立表格:

create table student(    id int not null auto_increment,    name varchar(20) not null,    age int not null,    primary key(id));

查看錶結構:

  DESCRIBE auth_user;

  文法:describe 表名 是 show columns from 表名 的一種捷徑。

 

二、修改mysql資料庫密碼

 

方法一:

 

使用phpmyadmin,直接修改Mysql庫的user 表。或者使用Navicat for Mysql 直接修改串連屬性。

 

方法二:使用mysqladmin

 

  #cmd,運行DOS,cd到mysql的bin檔案夾,然後執行如下:

 

    D:\Mysql\bin>mysqladmin -u root -p password newPwd

 

    #Enter password:(在此輸入原密碼)

 

    #newPwd指的是新密碼
  然後開啟mysql 直接輸入新密碼即可

 

格式:mysqladmin -u使用者名稱 -p舊密碼 password 新密碼。

 

 

訪問資料庫,使用use語句

建立資料庫

  mysql> CREATE DATABASE 庫名;

  mysql> USE 庫名;

  mysql> CREATE TABLE 表名 (欄位名 VARCHAR(20), 欄位名 CHAR(1));

刪除資料庫:

  mysql> DROP DATABASE 庫名;

刪除資料表:

  mysql> DROP TABLE 表名;

將表中記錄清空:

  mysql> DELETE FROM 表名;

建立表(複雜形式):

#建立customer表:

 

create table customers(    id int not null auto_increment,    name char(20) not null,    address char(50) null,    city char(50) null,    age int not null,    love char(50) not null default ‘No habbit‘,    primary key(id))engine=InnoDB;

 

  SELECT last_insert_id();   這個函數可以獲得返回最後一個auto_increment值.

  #預設值:default ‘No habbit‘,

  #引擎類型,多為engine = InnoDB,如果省略了engine=語句,則使用預設的引擎(MyISAM)

 

 更改表結構:

 

  #增加一列:文法:alter table tablename add colummname type [null];

    alter table pet add des char(100) null;

 

  #刪除:文法:alter table tablename drop column colummname;

    alter table pet drop column des;

 

重新命名表:

  文法:rename table tablename1 to tablename2;

 

  rename table pet to animals;

 

 添加id欄位

 

則可操作如下:

 

#添加id欄位,包括主鍵

  alter table pet add id int not null primary key auto_increment first;

設定索引:

若要設定外鍵,在參照表(referencing table,即Pc表) 和被參照表 (referenced table,即parts表) 中,相對應的兩個欄位必須都設定索引(index)。

對Parts表:

  ALTER TABLE parts ADD INDEX idx_model (model);

這句話的意思是,為 parts 表增加一個索引,索引建立在 model 欄位上,給這個索引起個名字叫idx_model。

 

 

MyBatis SQL語句 符號不相容 大於符號 小於符號 (XML逸出字元)

 

 當我們需要通過xml格式處理sql語句時,經常會用到< ,<=,>,>=等符號,但是很容易引起xml格式的錯誤,這樣會導致後台將xml字串轉換為xml文檔時報錯,從而導致程式錯誤。這樣的問題在MyBatis中或者自訂的xml處理sql的程式中經常需要我們來處理。其實很簡單,我們只需作如下替換即可避免上述的錯誤:

 

原符號

  <

   <=

   >

   >=

    &

     ‘

      "

替換符號

<

<=

>

>=

&

"

 

 

 

 

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.