show databases;
use database名稱;
例如:use mysql;
如果沒有進入某一庫,在對庫中的資料進行訪問時, 會提示 "no database selected"
檢視當前已進入的資料庫
show tables;
語法:drop database 庫名;
例如:drop database if exists mydb1;
-- if exists 如果存在...
-- if not exists 如果存在...
基於此,我們可以考慮,在使用資料庫的時候,如果刪除的表不存在,則應該如何避免錯誤操作。
語法:create database 庫名 charset 編碼;
例如:create database mydb1 charset utf8;
如果不存在則建立mydb1;
create database if not exists mydb1 charset utf8;
語法:show create database 庫名;
例如:show create database mydb1;
語法:desc 表名;
例如:desc stu;
語法:drop table 表名;
例如:use mydb1;
drop table if exists stu;
建表的語法:
create table 表名(
列名 資料型別,
列名 資料型別,
...);
例如:drop table if exists stu;
create table stu(
id int primary key auto_increment,
name varchar(20) unique,
gender char(1) not null,
birthday date,
score double
);-- alter table stu modify id int auto_increment;
在這裡我們使用到了「鍵」,這裡我們簡單做乙個介紹,目的是讓各位博友有乙個初步的了解與認識,具體使用方法我們後續博文再分享。
主鍵約束:如果將乙個列設定為主鍵,那麼該列的值就不能重複,且不能為空。它的作用是:作為資料記錄的唯一標識。
id int primary key
另外,主鍵如果是數值型別,可以設定主鍵為"auto_increment" -- 也就是主鍵自增
設定為自增後,在資料庫中會儲存乙個變數,用於記錄當前的id。變數預設值為1,但是1被使用後,再自增1...
唯一約束:如果為乙個列新增唯一約束,那麼該列的值就不能重複,但是可以為空。
name varchar(20) unique
非空約束:如果為乙個列新增非空約束,那麼該列的值就不能為空,但是可以重複。
gender char(1) not null
外來鍵約束,這個相對複雜一定,我們目前的示例中還未用到,後續再展開。
接下來,我們來檢視一下,stu學生表的結構:
首先,檢視學生表的建表語句
show create table stu;
其次,檢視表結構
desc stu;
mysql資料庫基礎建庫與建表
新建資料庫 create database name 有分號,name為資料庫名,不能重名,首字母不能為數字和 create database name charset utf8mb4 clooate utf8mb4 general ci 查詢和選擇資料庫 show databases use na...
Oracle資料庫 建庫 建表空間,建使用者
oracle資料庫 建庫 建表空間,建使用者 oracle安裝完後,其中有乙個預設的資料庫,除了這個預設的資料庫外,我們還可以建立自己的資料庫。對於初學者來說,為了避免麻煩,可以用 database configuration assistant 嚮導來建立資料庫。建立完資料庫後,並不能立即在資料庫...
Oracle資料庫 建庫 建表空間,建使用者
oracle安裝完後,其中有乙個預設的資料庫,除了這個預設的資料庫外,我們還可以建立自己的資料庫。對於初學者來說,為了避免麻煩,可以用 database configuration assistant 嚮導來建立資料庫。建立完資料庫後,並不能立即在資料庫中建表,必須先建立該資料庫的使用者,並且為該使...