用MySQL建立資料庫和資料表

2021-07-12 00:48:52 字數 2160 閱讀 6193

步驟:

使用show

語句找出在伺服器上當前存在什麼資料庫:

mysql>show databases;

建立乙個資料庫

test

:mysql>create database test;

選擇你所建立的資料庫:

mysql>use test;

4建立乙個資料表:

首先檢視剛才建立的資料庫中存在什麼表:

mysql>show tables;

(說明剛才建立的資料庫中還沒有資料庫表)

接著我們建立乙個關於

students

的資料表:包括學生的學號

(id)

,姓名(name)

,性別(***)

,年齡(age)。

mysql>create table students(id int unsigned notnull auto_increment primary key,name char(8) not null,*** char(4) not null,agetinyint unsigned not null,);

解釋:以

"id int unsigned not null auto_increment primary key"

行進行介紹:

"id"

為列的名稱;

"int"

指定該列的型別為

int(

取值範圍為

-8388608

到8388607),

在後面我們又用

"unsigned"

加以修飾

, 表示該型別為無符號型

, 此時該列的取值範圍為0到

16777215;

"not null"

說明該列的值不能為空

, 必須要填

, 如果不指定該屬性

, 預設可為空;

"auto_increment"

需在整數列中使用

, 其作用是在插入資料時若該列為

null, mysql

將自動產生乙個比現存值更大的唯一識別符號值。在每張表中僅能有乙個這樣的值且所在列必須為索引列。

"primary key"

表示該列是表的主鍵

, 本列的值必須唯一

, mysql

將自動索引該列。

下面的char(8)

表示儲存的字元長度為

8, tinyint

的取值範圍為

-127

到128, default

屬性指定當該列值為空時的預設值。

建立乙個表後,用

show tables

顯示資料庫中有哪些表:

mysql>show tables;

顯示表結構:

mysql>describe students;

在表中新增記錄:

首先用select

命令來檢視表中的資料:

mysql>select*from students;

(說明剛才建立的資料庫表中還沒有任何記錄)

接著加入一條新紀錄:

mysql>insert into studentsvalue(『01』,』tom』,』f

』,』18』)

; 再用select

命令來檢視表中的資料的變化:

mysql>select*from students;

用文字方式將資料裝入乙個資料庫表:

建立乙個文字檔案「

student.sql

」,每行包括乙個記錄,用

tab鍵把值分開,並且以在

create table

語句中列出的次序,例如:

02   tony    f    18

03   amy    m    18

04   lisa    m    18

將文字檔案「

student.sql

」裝載到

students

表中:

mysql>load data localinfile」e:\\student.sql」into table students;

再使用select

命令來檢視表中的資料的變化:

mysql>select*from students;

mySQL建立資料庫和資料表

sql 的主要功能是和資料庫建立連線,進行增刪改查的操作。sql是關係型資料庫管理系統的標準語言。sql 語言的作用 使用 sql 運算元據庫時,所有的 sql 語句都以分號結束。切換資料庫時可以不用分號 在 sql 語句中,不區分大小寫,編寫 sql 語句時可以根據情況用大小寫的區別來增加可讀性。...

關於mysql建立資料庫和資料表

前提是mysql的配置正確,然後在cmd裡輸入 mysql u root p,輸入mysql的密碼進入mysql資料庫。比方說要建立的資料庫名字為work,資料表的名字為course。course裡包含teachername和classname兩個資料。輸入以下命令即可。create databas...

MySQL資料庫和資料表操作

顯示資料庫 show databases 顯示資料表 show tables 選擇資料庫 use database name 顯示表結構 desc table name create database database name drop database database name create ...