1. 程式人生 > >MySQL(1) 基本操作(MySQL的啟動,表的建立,查詢表的結構和表的欄位的修改)

MySQL(1) 基本操作(MySQL的啟動,表的建立,查詢表的結構和表的欄位的修改)

MySQL啟動流程

1 啟動伺服器

 

2 使用者名稱登入到MySQL資料庫中

 

 3  檢視有哪些資料庫

 

4 使用其中的資料庫

 

 5 檢視該資料庫中已有哪些表,沒有就新建

mysql> CREATE TABLE tableA(
-> id INT(11),
-> name VARCHAR(22) NOT NULL,
-> location VARCHAR(50),
-> PRIMARY KEY(ID));

 

 

6、修改欄位的排列位置

更改前:

 

方式1:ALTER TABLE<表名> MODIFY<欄位1><資料型別> FIRST|AFTER<欄位2>

mysql> alter table tablea modify username varchar(30) after location;//改變了username的資料型別,first是將其移動到第一,AFTER<欄位2>是將其移動到<欄位2>的後面。

修改後:

 

方式2:ALTER TABLE<表名> CHANGE<欄位1><欄位2><資料型別> FIRST|AFTER<欄位3>

mysql> alter table tablea change username name varchar(50) after location;//將欄位一改名為欄位二,同時能移動到欄位3後面,且能同時改變資料型別

 

 

7 用指令碼建立表

當在cmd檔案中建立比較複雜的表格時,可能會書寫錯誤,可以先建立一個文字文件,字尾為.sql,然後source一下

如下面的:myFirstSQL.sql

drop database if exists DBName;

create database DBName;

use DBName;


create
table t_Book ( bookId int unsigned primary key not null auto_increment, name varchar(50) not null, author varchar(20) not null, isbn char(20) not null, edition varchar(10) not null, press varchar(20) not null, publicData datetime not null, catagory varchar(50) not null, price decimal not null, description varchar(500) not null, pic varchar(100) not null, sold int unsigned not null, sum int unsigned not null, upShelfDate datetime not null, downShelfDate datetime not null ); create table t_User ( userId int unsigned primary key not null auto_increment, name varchar(20) not null, pass varchar(20) not null, type varchar(10) not null, address varchar(100) not null, postCode varchar(6) not null, phoneNumber varchar(11) not null, email varchar(30) not null ); create table t_UserOrder ( userId int unsigned not null, bookId int unsigned not null, orderId int unsigned not null, primary key(userId,bookId,orderId) );

執行指令碼檔案:

 可以看到資料庫中增加了一個dbname的資料庫: