MySql數據庫基礎操作

–顯示服務器上的數據庫
mysql>show databases;

–創建一個數據庫
mysql>create database m;
–創建數據庫,並設置字符集
create database hive DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;

–刪除一個數據庫
mysql>drop database m;

–選擇一個數據庫
mysql>use test;

–查看當前數據庫中存在什麼表
mysql>show tables;

–設置數據庫字符集
SET character_set_client=’utf8’;
SET character_set_connection=’utf8’;
SET character_set_results=’utf8’;

–給表添加主鍵
mysql>alter table mytable add primary key(name);

–給表添加聯合主鍵
Alter table cms_laucher_prodcls_item drop primary key ;
Alter table cms_laucher_prodcls_item add constraint primary key(laucher_id,prodcls_id,language_id);

–複製表
mysql>create table newtable select * from mytable;
–部分複製
mysql>create table newtable select id,name from mytable;

–查看錶結構(四種方式)
mysql>describe mytable;
mysql>desc mytable;
mysql>show columns in mytable;
mysql>show columns from mytable;

–修改表結構
mysql>alter table mytable add[change,rename,drop]…要更改的內容
實例:
mysql>alter table mytable add column address varchar(80) not null;
–刪除
mysql>alter table mytable drop address (restrict約束);
–修改
mysql>alter table mytable change sex sex char(2) not null;
mysql>alter table wms_interface_ip_access modify sex int
mysql>alter table students modify id int not null auto_increment;
修改字段名
alter table 表名 change column 舊字段名 新字段名 [約束條件];

–表重命名(兩種方式)
mysql>rename table old_name to new_name;
mysql>alter table old_name rename new_name;

–表字段設置默認值
alter table表名alter column字段名drop default; (若本身存在默認值,則先刪除)

alter table 表名 alter column 字段名 set default 默認值;(若本身不存在則可以直接設定)

****** mysql的正則表達式 **********

–找出name以a-d爲開頭的人的信息
mysql>select * from mytable where name regexp ‘^[a-d]’;

****** mysql的一些函數 **********
1、字符串鏈接—–concat()
mysql>select concat(name,’=>’,sex) from mytable;

2、數學函數
avg,sum,max,min,count

3、文本處理函數
trim,locate,upper,lower,substring

4、運算符
+ - * \

5.時間函數
date(),curtime(),day(),year(),now()….

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章